CodeForces 816B 前缀和

Posted daybreaking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 816B 前缀和相关的知识,希望对你有一定的参考价值。

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.

Sample Input

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

Hint

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次查询,每次查询 问在查询区间中有多个数 在开始给给定的n个区间中出现次数大于等于k(题意大致是这样)。

  前缀和。

对于开始给定的区间,利用括号匹配的思想,区间的左端点看作是左括号,右端点看作是右括号。然后从前向后遍历一遍,左括号每遇到一个右括号,就同时消去这一对括号,判断该数带有几个左括号

即为在几个区间出现过。前缀和完毕。

  

  

技术分享图片
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<stack>
 9 #include<deque>
10 #include<map>
11 #include<iostream>
12 using namespace std;
13 typedef long long  LL;
14 const double pi=acos(-1.0);
15 const double e=exp(1);
16 const int N = 100009;
17 
18 struct con{
19     int x;
20     int left,right;
21 }con[2*N];
22 
23 int num[2*N];
24 
25 int main()
26 {
27     int n,k,q;
28     int i,p,j;
29     int a,b;
30     scanf("%d%d%d",&n,&k,&q);
31     for(i=1;i<=n;i++)
32     {
33         scanf("%d%d",&a,&b);
34         con[a].left++;
35         con[b+1].right++;
36     }
37     a=b=0;
38     for(i=1;i<=2*N;i++)
39     {
40         a+=con[i].left;
41         a-=con[i].right;
42         if(a>=k)
43             num[i]=1;
44         num[i]+=num[i-1];
45     }
46 //    for(i=1;i<=100;i++)
47 //    {
48 //        printf("%d ",num[i]);
49 //        if(i%10==0)
50 //            putchar(‘
‘);
51 //    }
52     for(i=1;i<=q;i++)
53     {
54         scanf("%d%d",&a,&b);
55         printf("%d
",num[b]-num[a-1]);
56     }
57 
58     return 0;
59 }
View Code

 














以上是关于CodeForces 816B 前缀和的主要内容,如果未能解决你的问题,请参考以下文章

Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)

Codeforces-295记录

CodeForces - 816B Karen and Coffee (线段树的区间插入+单点查询)

Codeforces 838A - Binary Blocks(二维前缀和+容斥)

Codeforces Round #540 (Div. 3)

Codeforces Round #650 (Div. 3) C. Social Distance (前缀和)