线段树+离散化
View Code
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 8 const int maxn = 100005; 9 int add[maxn<<2]; 10 int sum[maxn<<2]; 11 int x[maxn*3]; 12 int li[maxn],ri[maxn]; 13 int q[maxn]; 14 15 void PushUp(int rt) 16 { 17 sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 18 } 19 void PushDown(int rt) 20 { 21 if(add[rt]) 22 { 23 add[rt<<1] += add[rt]; 24 add[rt<<1|1] += add[rt]; 25 sum[rt<<1] += add[rt]; 26 sum[rt<<1|1] += add[rt]; 27 add[rt] = 0; 28 } 29 } 30 void build(int l,int r,int rt) 31 { 32 add[rt] = 0; 33 sum[rt] = 0; 34 if(l == r)return ; 35 int m = (l + r) >> 1; 36 build(lson); 37 build(rson); 38 } 39 void update(int L,int R,int l,int r,int rt) 40 { 41 if(L <= l && r <= R) 42 { 43 add[rt]++; 44 sum[rt]++; 45 return ; 46 } 47 PushDown(rt); 48 int m = (l + r) >> 1; 49 if(L <= m)update(L,R,lson); 50 if(m < R)update(L,R,rson); 51 PushUp(rt); 52 } 53 int query(int k,int l,int r,int rt) 54 { 55 if(l == k && r == k) 56 { 57 return sum[rt]; 58 } 59 PushDown(rt); 60 int ret = 0; 61 int m = (l + r) >> 1; 62 if(k <= m)ret += query(k,lson); 63 else ret += query(k,rson); 64 return ret; 65 } 66 int find(int key,int n) 67 { 68 int l = 0, r = n - 1; 69 while(l <= r) 70 { 71 int m = (l + r) >> 1; 72 if(x[m] == key)return m; 73 else if(x[m] < key)l = m + 1; 74 else r = m - 1; 75 } 76 return -1; 77 } 78 int main() 79 { 80 #ifndef ONLINE_JUDGE 81 freopen("in","r",stdin); 82 #endif 83 int t; 84 scanf("%d",&t); 85 for(int cas=1;cas<=t;cas++) 86 { 87 int n,m; 88 int nn=0; 89 scanf("%d%d",&n,&m); 90 for(int i=0;i<n;i++) 91 { 92 scanf("%d%d",&li[i],&ri[i]); 93 x[nn++] = li[i]; 94 x[nn++] = ri[i]; 95 } 96 for(int i=0;i<m;i++) 97 { 98 scanf("%d",&q[i]); 99 x[nn++]=q[i]; 100 } 101 sort(x,x+nn); 102 int num = 1; 103 for(int i=1;i<nn;i++) 104 { 105 if(x[i] != x[i-1])x[num++] = x[i]; 106 } 107 printf("Case #%d:\n",cas); 108 build(0,num,1); 109 for(int i=0;i<n;i++) 110 { 111 int l=find(li[i],num); 112 int r=find(ri[i],num); 113 update(l,r,0,num,1); 114 } 115 for(int i=0;i<m;i++) 116 { 117 int k= find(q[i],num); 118 printf("%d\n",query(k , 0, num,1)); 119 } 120 } 121 }