To stay woke and attentive during classes, Karen needs some coffee!ios
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".c++
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.ide
Karen thinks that a temperature is admissible if at least k recipes recommend it.ui
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?spa
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.code
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.blog
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.three
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.ip
3 2 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100
3
3
0
4
2 1 1
1 1
200000 200000
90 100
0
In the first test case, Karen knows 3 recipes.ci
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.
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 }