Codeforces Round #419 (Div. 2) B. Karen and Coffee 预处理+技巧

. 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, 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 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100
output
3
3
0
4
input
2 1 1
1 1
200000 200000
90 100
output
0
Note

In the first test case, Karen knows 3 recipes.

  1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
  2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
  3. 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 39293 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 39394 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 4929394 and 97 degrees are all admissible.

In the second test case, Karen knows 2 recipes.

  1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
  2. 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,再给出q个区间,依次输出给出的每个q区间里至少落在k个n区间中的数的个数

分析:昨晚的cf题。。卡死我了。。。。一拿到题,上来不管三七二十一直接枚举先看看成效,不出所料TL,之后优化。。优化到死。。。。结果今天早上打开电脑看到提交的状态里边有几个大佬200ms内就a了。。。。然后就参考了大佬的代码。。果然强。。  思路:运用的递推的方式依次从每个区间的起点往后加1,这样就可以在n的复杂度内枚举出每个数出现的次数,这里要注意的是区间的最右端的后一个数应该-1,因为按照递推的方式,从区间开始到区间结束每一个数都应该加上前一个数,当这个区间结束的时候,自然就要减去一个区间。当递推的同时再用一个c数组也从开始递推,同样是加上前一个,但是同时也要判断此刻的数是否大于k如果大于,就再加一,这样就可以确定在一定范围内的数大于k的个数。分析啰嗦,直接上代码会很容易理解。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxed=200000+10;
int a[maxed],c[maxed];
int main()
{
    int n,k,q,x,y;
    scanf("%d%d%d",&n,&k,&q);
    for(int i=0;i<n;i++){
        scanf("%d%d",&x,&y);
        a[x]++;
        a[y+1]--;
    }
    for(int i=1;i<maxed;i++){
        a[i]+=a[i-1];
        c[i]+=c[i-1]+(a[i]>=k);
    }
    for(int i=0;i<q;i++){
        scanf("%d%d",&x,&y);
        printf("%d\n",c[y]-c[x-1]);//这里注意,因为c是从前面递推而来的,所以要看要看区间开始的前一个数才能判断区间开始点的状态
    }
    return 0;
}
果然大佬就是大佬。。自己就是太菜了。。。做题太少。。。。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章