To stay woke and attentive during classes, Karen needs some coffee!
![](https://i-blog.csdnimg.cn/blog_migrate/2b51f422c276cf30a445759f3dcd4550.png)
Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".
She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.
Karen thinks that a temperature is admissible if at least k recipes recommend it.
Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?
The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.
The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive.
The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100
3 3 0 4
2 1 1 1 1 200000 200000 90 100
0
In the first test case, Karen knows 3 recipes.
- The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
- The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
- The third one recommends brewing the coffee between 97 and 99 degrees, inclusive.
A temperature is admissible if at least 2 recipes recommend it.
She asks 4 questions.
In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.
In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.
In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.
In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.
In the second test case, Karen knows 2 recipes.
- The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
- The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees.
A temperature is admissible if at least 1 recipe recommends it.
In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.
题目大意是给出n种咖啡的合适温度区间,然后给出一个询问区间,问这个区间上有多少个数,这个数属于大于等于k个咖啡的合适区间
首先想法线段树维护区间的min,然后妥妥的TLE
然后我觉得可以考虑主席树?把每个合适温度区间看成区间+1,相当于求区间的大于等于k的有多少个数
最后看的题解,发现超级简单......根本不需要这种动态的结构
首先考虑数组的区间修改,对于区间x~y区间+1就是对数组arr[x]++,arr[y+1]--。这个实际上是把整个数组差分了,差分后的arr[i]=原来的arr[i]-arr[i-1],差分后区间修改是O(1)
的,令arr[0]=0,然后求arr[i]上的值就是∑arr[ij 1<=j<=i
这样的O(1)修改后,需要O(n)的时间来求一个温度被多少个合适温度区间包含,因此我们对这个数组求一个前缀和。也就是令arr[i]=∑ 原arr[j] 1<=j<=i,这样的话,就是O(1)求出该温度被多少个合适温区间包含。
然后假如一个温度被大于等于k个温度区间包含,就置为1,否则置0。然后再求一遍前缀和,这样可以求出一段区间上有多少个温度,其中每个温度被大于等于k个温度区间包含。
把每个子问题转化为0或1后求和,我之前也见过一次,很巧妙的。
#include <Iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxm=2E5+10;
int arr[maxm],ps[maxm],n,k,q;
int main(){
ios_base::sync_with_stdio(0);
cin>>n>>k>>q;
for(int i=0,x,y;i<n;++i){
cin>>x>>y;
++arr[x],--arr[1+y];
}
for(int i=1;i<=maxm;++i){
arr[i]+=arr[i-1];
if(arr[i]>=k)ps[i]=1;
}
for(int i=1;i<=maxm;++i)
ps[i]+=ps[i-1];
int x,y;
while(q--){
cin>>x>>y;
cout<<ps[y]-ps[x-1]<<endl;
}
return 0;
}