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;
}
果然大佬就是大佬。。自己就是太菜了。。。做題太少。。。。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章