1. 程式人生 > >CodeForces 816B Karen and Coffee(前綴和,大量查詢)

CodeForces 816B Karen and Coffee(前綴和,大量查詢)

pre div 準備 nes 個數 contain 端點 -i integer

CodeForces 816B Karen and Coffee(前綴和,大量查詢)

Description

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

題意:

給出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 個包含的前綴和。

第一組樣例就是

技術分享

所以對於 k 個提問,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<stdio.h>
#include<iostream>
using namespace std;
int a[200010];
int c[200010];
int main()
{
    int k,n,q;
    int b,e;
    cin>>k>>n>>q;
    for(int i=0; i<k; i++)//輸入查詢
    {
        cin>>b>>e;
        a[b]++;
        a[e+1]--;
    }
    for(int i=1; i<200010; i++)//更新cnt數組
    {
        a[i]+=a[i-1];
        if(a[i]>=n)
            c[i]++;
    }
    for(int i=1; i<200010; ++i)//更新sum數組
        c[i]+=c[i-1];
    for(int i=0; i<q; i++)//輸出結果
    {
        int l,r;
        scanf("%d%d",&l,&r);
        cout<<c[r]-c[l-1]<<endl;
    }
}

  

CodeForces 816B Karen and Coffee(前綴和,大量查詢)