ST multiplier Inn application selection (improved group)

Heavy example! ST table application! Improve the group Getting!

 

1125: B15- multiplication - Exercise: Select Inn [ST table Application]

Time limit: 1 Sec   Memory Limit: 128 MB
submit: 35   Resolution: 12
[ submit ] [ state ] [ Discussion Board ] [proposition man: External Import]

Title Description

N Lijiang river has very unique family inn, the inn according to their position in order from 1 to n number. Each inn are carried out in accordance with a certain kind of tone decoration (k species in total, ~ k-1 represents an integer 0), and each inn has a coffee shop, every coffee shop has its own minimum consumption.
To go along with two tourists in Lijiang tourism, they like the same tone, want to try two different inn, it was decided were living in the same two-tone inn. At night, they intend to select a coffee shop drinking coffee, the coffee shop is located in requirements (including the inn they lived) between two inn both live, and the lowest consumption of coffee shops does not exceed p.
They want to know how many there are in total choice of accommodation programs, to ensure that at night you can find a minimum consumption of no more than a coffee shop p yuan-together.


Entry

Each common input data line n + 1.
The first row of three integers n, k, p, between each two integers separated by a space, respectively represent the number inn, the number of tones of the lowest and the highest value acceptable;
next n lines, i + 1-row two integers, separated by a space between, respectively, the lowest number of consumer coffee shop i i inn decorated in colors and numbers inn.


Data size:
for 30% of the data, there n≤100;
for 50% of the data, there n≤1,000;
to 100% of the data, there 2≤n≤200,000,0 <k≤50,0≤p≤100, 0≤ minimum consumption ≤100.


Export

Output only one line each, an integer, the total number of alternative solutions accommodation FIG.


The following is an explanation of the sample data:


2 people want to live in the same tone of the inn, all alternative accommodation programs include: live inn ①③, ②④, ②⑤, ④⑤, but if you choose to live 4,5 inn, then the minimum coffee shop between 4,5 inn consumption is 4, and the lowest consumer can withstand two is $ 3, it does not meet the requirements. Therefore, only the first three schemes optional.



Sample input Copy

5 2 3
0 5
1 3
0 2
1 4
1 5

Sample output Copy

3

prompt


Source / classification

 
/*
1125: B15-倍增-习题:选择客栈[ST表应用]
时间限制: 1 Sec  内存限制: 128 MB
提交: 4  解决: 2
[提交] [状态] [讨论版] [命题人:外部导入]
题目描述
丽江河边有n家很有特色的客栈,客栈按照其位置顺序从1到n编号。每家客栈都按照某一种色调进行装饰(总共k种,用整数0~k-1表示),且每家客栈都设有一家咖啡店,每家咖啡店均有各自的最低消费。
两位游客一起去丽江旅游,他们喜欢相同的色调,又想尝试两个不同的客栈,因此决定分别住在色调相同的两家客栈中。晚上,他们打算选择一家咖啡店喝咖啡,要求咖啡店位于两人住的两家客栈之间(包括他们住的客栈),且咖啡店的最低消费不超过p。
他们想知道总共有多少种选择住宿的方案,保证晚上可以找到一家最低消费不超过p元的咖啡店小聚。
  
  
输入
每组输入数据共n+1行。
第一行三个整数n,k,p,每两个整数之间用一个空格隔开,分别表示客栈的个数,色调的数目和能接受的最低消费的最高值;
接下来的n行,第i+1行两个整数,之间用一个空格隔开,分别表示i号客栈的装饰色调和i号客栈的咖啡店的最低消费。
  
  
数据规模:
对于30%的数据,有n≤100;
对于50%的数据,有n≤1,000;
对于100%的数据,有2≤n≤200,000,0<k≤50,0≤p≤100,0≤最低消费≤100。
  
  
输出
每组输出只有一行,一个整数,表示可选的住宿方案的总数。
  
  
下面是对样例数据的解释:
  
  
2人要住同样色调的客栈,所有可选的住宿方案包括:住客栈①③,②④,②⑤,④⑤,但是若选择住4、5号客栈的话,4、5号客栈之间的咖啡店的最低消费是4,而两人能承受的最低消费是3元,所以不满足要求。因此只有前3种方案可选。
  
  
  
样例输入 Copy
  
5 2 3
0 5
1 3
0 2
1 4
1 5
  
样例输出 Copy
  
3
  
提示
  
来源/分类
B15-倍增 
[提交] [状态]
GPLv2 licensed by QDOJ 2019 
  
*/
#include<bits/stdc++.h>
using namespace std;
int n,k,p;
int co[200005],mo[200005];
int d[200005][55];long long ans;
void RMQ_init(){//ST表的创建模板
    for(int i=0;i<n;i++)
        d[i][0]=mo[i];
    for(int j=1;(1<<j)<=n;j++)
        for(int i=0;i+(1<<j)-1<n;i++){
            d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
        }
}
int RMQ_min(int L,int R){//区间最小值
    int k=0;
    while((1<<(k+1))<=R-L+1)
        k++;
    return min(d[L][k],d[R-(1<<k)+1][k]);     
}
 
void solve(int id){
    int pre=0,sum1=0,sum2=0;
    for(int i=0;i<n;i++)
        if(co[i]==id)
            sum1++;
    ans+=1ll*sum1*(sum1-1)/2;
    for(int i=0;i<n;i++)
        if(co[i]==id){
         
            if(!pre){
                pre=i;
                continue;
            }
            if(RMQ_min(pre,i)>p)
                sum2++;
            else{
                ans-=1ll*sum2*(sum2+1)/2;
                sum2=0;
            }
            pre=i;
        }
     
    if(sum2)
        ans-=1ll*sum2*(sum2+1)/2;
}
int main(){
    scanf("%d%d%d",&n,&k,&p);
    for(int i=0;i<n;i++)
        scanf("%d%d",&co[i],&mo[i]);
    RMQ_init();
    for(int i=0;i<k;i++){
        solve(i);//这个函数的整体思路就是把不可能的情况快速地排除掉,可以再看一下视频来辅助理解哦
    }
    cout<<ans;
    return 0;
   }
/************************************************************** Problem: 1125 User: lijunhui Language: C++ Result: 正确 Time:226 ms Memory:46236 kb ****************************************************************/

//https://noip-1253948194.cos.ap-beijing.myqcloud.com/%E5%80%8D%E5%A2%9E-ST%E7%AE%97%E6%B3%95%E9%80%89%E6%8B%A9%E5%AE%A2%E6%A0%88.mp4

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11202210.html