码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-26 22:08:35      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:within   input   思路   idt   c++   perfect   for   sam   must   

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.

Example
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 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本时尚杂志,每本杂志都给出了咖啡的最优的冲泡温度区间 l , r 。如果有k本杂志提到了这个温度,那么这个温度就是好的。问q个询问中有多少个温度是好的?

 

解题思路:把 l ~ maxx的区间值赋值为1,把 r ~ maxx的区间值赋值为-1。然后询问所有温度是否大于等于 k,求出这个些温度的前缀和。每个询问就是对应前缀和的差。

 

AC代码:

 

技术分享
 1 #include <iostream>
 2 #include<bits/stdc++.h>
 3 //if(~i)//当i不是-1时满足条件
 4 using namespace std;
 5 const int maxn=1e5*2+5;
 6 int n,k,q;
 7 int lazy[maxn<<2],tmp[maxn];
 8 int build(int l,int r,int t)
 9 {
10     lazy[t]=0;
11     if(l==r)
12         return 0;
13     int mid=(l+r)/2;
14     build(l,mid,t*2);
15     build(mid+1,r,t*2+1);
16     return 0;
17 }
18 int push_up(int t)
19 {
20     if(lazy[t])
21     {
22         //printf("%d==========%d\n",l,r);
23         lazy[t*2]+=lazy[t];
24         lazy[t*2+1]+=lazy[t];
25         lazy[t]=0;
26     }
27 }
28 int ins(int L,int R,int l,int r,int t,int val)
29 {
30     if(L<=l&&r<=R)
31     {
32         lazy[t]+=val;
33         return 0;
34     }
35     push_up(t);
36     int mid=(l+r)/2;
37     if(L<=mid) ins(L,R,l,mid,t*2,val);
38     if(R>mid) ins(L,R,mid+1,r,t*2+1,val);
39     return 0;
40 }
41 int query(int d,int l,int r,int t)
42 {
43     if(l==r&&l==d)
44     {
45         return lazy[t];
46     }
47     push_up(t);
48     int mid=(l+r)/2;
49     if(d<=mid) return query(d,l,mid,t*2);
50     else if(mid<d) return query(d,mid+1,r,t*2+1);
51 }
52 int main()
53 {
54 
55     while(~scanf("%d%d%d",&n,&k,&q))
56     {
57         build(1,200005,1);
58         int l,r;
59         for(int i=0; i<n; i++)
60         {
61             scanf("%d%d",&l,&r);
62             ins(l,200002,1,200005,1,1);
63             ins(r+1,200002,1,200005,1,-1);
64         }
65         tmp[0]=0;
66         for(int i=1;i<=200002;i++)
67         {
68             if(query(i,1,200005,1)>=k) tmp[i]=tmp[i-1]+1;
69             else tmp[i]=tmp[i-1];
70         }
71         for(int i=0; i<q; i++)
72         {
73             scanf("%d%d",&l,&r);
74             printf("%d\n",tmp[r]-tmp[l-1]);
75         }
76     }
77     return 0;
78 }
View Code

 

 

 

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

标签:within   input   思路   idt   c++   perfect   for   sam   must   

原文地址:http://www.cnblogs.com/wang-ya-wei/p/7436382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!