B. Karen and Coffee
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
To stay woke and attentive during classes, Karen needs some coffee!
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?
Input
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.
Output
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
Examples
Input
Copy
3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100
Output
Copy
3 3 0 4
Input
Copy
2 1 1 1 1 200000 200000 90 100
Output
Copy
0
Note
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个区间,q个查询区间,问每次查询时,该查询区间内有多少个点至少被k个区间覆盖。
样例1:第一行三个整数的意义是:3个区间 至少被2个区间覆盖 查询四次,第二三四行分别是三个区间。剩下四行为查询的区间,比如92 94就是在92到94中(包含92和94),多少个数被上面区间覆盖了2次(即k次)或者2次以上。在92到94的区间内92 93 94都被覆盖两次,故三个,输出3。
思路:
数据量是2*10^5,暴力的方法肯定会超时。
准备两个一维数组,cnt[] 和 sum[] 来处理每一个满足数 i (范围 [1, 200000] )
cnt[i] 数组:第 i 个数被区间包含的次数
sum[i] 数组: 前 i 个数被 k 个包含的前缀和。
所以对于 q 个提问,sum[b] - sum[a-1] 就是答案了(因为sum[b]是0到b中符合条件的总数,sum[a-1](包含两个端点)是0到a中符合条件的总数,两者相减就是a到b中符合条件的总数)。
这里最巧妙之处就是cnt[i] 要怎么统计出来。如果 [li, ri] 范围的数都遍历一次,绝对会超时, 处理两个点其实就可以统计出来了,分别是 cnt[li], cnt[ri+1]。 对于每一次询问,进行:cnt[li]++, cnt[ri+1]-- 处理。然后 q 个问题之后,再统一遍历多一次,利用前一个数 cnt[i-1] 就能统计出当前数 cnt[i] 了。
代码:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std ;
typedef long long LL;
const int MAX = 200010 ;
int cnt[MAX] ;// 第i个数被几个区间包含
int sum[MAX] ;// 前i个数被>=k个区间包含的前缀和 ;
int main()
{
int n , k , q ;
cin >> n >> k >>q ;
for(int i = 1 ;i<= n ;i++)
{
int b , e ;
scanf("%d%d",&b,&e);
cnt[b]++ ;
cnt[e+1]-- ;
}
for(int i = 1 ; i<200010 ;i++)
{
cnt[i]+=cnt[i-1] ;
if(cnt[i]>=k)
sum[i]++ ;
sum[i]+=sum[i-1] ;
}
while(q--)
{
// 查询
int b ,e ;
scanf("%d%d",&b,&e);
printf("%d\n",sum[e]-sum[b-1]);
}
return 0 ;
}