Codeforces Round #419 (Div. 2)_Karen and Coffee

来源:互联网 发布:诸神黄昏神翼进阶数据 编辑:程序博客网 时间:2024/06/05 14:54

惯例,先上题:

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, nk (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
3 2 491 9492 9797 9992 9493 9795 9690 100
output
3304
input
2 1 11 1200000 20000090 100
output
0

翻译成中文意思就是:给很多区间,只有区间的重叠次数大于等于K被询问时才能被被选择,答案输出询问的区间中可以有多少个数可以被选择。

这个题目给了2.5s,然后看一下数据量,也就知道这2.5s也不算是特别长,所以传统暴力当然不可以啦!

解决问题:

①区间重叠次数的简单计算(如果用循环++肯定超时)

②被询问时重叠数量的简单计算(当然也不能用for暴力)

对于①,我们可以只把给定区间的左端点++,右端点加1处--,然后dp一遍,就可以知道区间重复多少次了。

对于②,我们可以把大于等于K的区域做dp,然后询问时右端点减去左端点就可以了。

上代码:

#include<cstdio>#include<iostream>using namespace std;const int N = 2e5+5;int a[N];int main(){int n,k,q;int l,r;scanf("%d %d %d",&n,&k,&q);for(int i = 1;i<= n;i++){scanf("%d %d",&l,&r);a[l]++;//起点++a[r+1]--;//重点后一个--,可以截止区间}for(int i = 1;i<= N;i++)//这里为了简便,直接从1到N,其实可以从区间最小到区间最大+1a[i]+= a[i-1];//计算每个点的重叠次数for(int i = 1;i<= N;i++){a[i]= a[i-1]+(a[i]>= k);//把大于等于K的点做一个累加}while(q--){scanf("%d %d",&l,&r);printf("%d\n",a[r]-a[l-1]);//前面累加了,只需末减初就可以了}return 0;}

如有不清楚,欢迎指教。

原创粉丝点击