1. 程式人生 > >B. Karen and Coffee

B. Karen and Coffee

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.

  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 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.

  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個區間,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 ;

}