题意求区间内比h小的数的个数
将所有的询问离线读入之后,按H从小到大排序。然后对于所有的结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,然后就是一个区间和.
2015-07-27:专题训练到此
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #define lson l,mid,rt<<1 8 #define rson mid+1,r,rt<<1|1 9 #define root 1,n,1 10 #define mid ((l+r)>>1) 11 #define ll long long 12 #define cl(a) memset(a,0,sizeof(a)) 13 #define ts printf("***** "); 14 using namespace std; 15 const int MAXN=199999+9; 16 int sum[MAXN<<2],lsum[MAXN<<2],rsum[MAXN<<2]; 17 int n,m,tt; 18 struct Node 19 { 20 int L,R,H; 21 int id; 22 void in(int w) 23 { 24 scanf("%d%d%d",&L,&R,&H); 25 L++,R++; 26 id=w; 27 } 28 }node[MAXN]; 29 struct No 30 { 31 int val; 32 int id; 33 void in(int w) 34 { 35 scanf("%d",&val); 36 id=w; 37 } 38 }no[MAXN]; 39 bool cmp1(Node a,Node b) 40 { 41 return a.H<b.H; 42 } 43 bool cmp2(No a,No b) 44 { 45 return a.val<b.val; 46 } 47 int pushup(int rt) 48 { 49 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 50 } 51 void update(int pos,int l,int r,int rt) 52 { 53 if(l==r) 54 { 55 sum[rt]=1; 56 return; 57 } 58 if(pos<=mid) update(pos,lson); 59 else update(pos,rson); 60 pushup(rt); 61 } 62 int query(int L,int R,int l,int r,int rt) 63 { 64 if(L<=l&&r<=R) 65 { 66 return sum[rt]; 67 } 68 int ans=0; 69 if(L<=mid) ans+=query(L,R,lson); 70 if(R>mid) ans+=query(L,R,rson); 71 return ans; 72 } 73 int a[MAXN]; 74 int main() 75 { 76 int i,j,k; 77 #ifndef ONLINE_JUDGE 78 freopen("1.in","r",stdin); 79 #endif 80 scanf("%d",&tt); 81 int ca=1; 82 while(tt--) 83 { 84 scanf("%d%d",&n,&m); 85 cl(sum); 86 for(i=1;i<=n;i++) 87 { 88 no[i].in(i); 89 } 90 sort(no+1,no+n+1,cmp2); 91 for(i=1;i<=m;i++) 92 { 93 node[i].in(i); 94 } 95 sort(node+1,node+m+1,cmp1); 96 int tot=1; 97 for(i=1;i<=m;i++) 98 { 99 while(node[i].H>=no[tot].val) 100 { 101 update(no[tot].id,root); 102 tot++; 103 } 104 a[node[i].id]=query(node[i].L,node[i].R,root); 105 } 106 printf("Case %d: ",ca++); 107 for(i=1;i<=m;i++) 108 { 109 printf("%d ",a[i]); 110 } 111 } 112 }
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #define lson l,mid,rt<<1 8 #define rson mid+1,r,rt<<1|1 9 #define root 1,n,1 10 #define mid ((l+r)>>1) 11 #define ll long long 12 #define cl(a) memset(a,0,sizeof(a)) 13 #define ts printf("***** "); 14 using namespace std; 15 const int MAXN=100015; 16 int sum[MAXN<<2],o[MAXN]; 17 int n,m,t; 18 struct Node 19 { 20 int L,R,H; 21 int id; 22 void in(int i) 23 { 24 scanf("%d%d%d",&L,&R,&H); 25 L++,R++; 26 id=i; 27 } 28 }node[MAXN]; 29 struct Num 30 { 31 int val,id; 32 void in(int i) 33 { 34 scanf("%d",&val); 35 id=i; 36 } 37 }num[MAXN]; 38 bool cmp1(Num a,Num b) 39 { 40 return a.val<b.val; 41 } 42 bool cmp2(Node a,Node b) 43 { 44 return a.H<b.H; 45 } 46 void pushup(int rt) 47 { 48 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 49 } 50 void build(int l,int r,int rt) 51 { 52 sum[rt]=0; 53 if(l==r) return; 54 build(lson); 55 build(rson); 56 } 57 void update(int pos,int l,int r,int rt) 58 { 59 if(l==r) 60 { 61 sum[rt]=1; 62 return; 63 } 64 if(pos<=mid) update(pos,lson); 65 if(pos>mid) update(pos,rson); 66 pushup(rt); 67 } 68 int query(int L,int R,int l,int r,int rt) 69 { 70 if(L<=l&&R>=r) 71 { 72 return sum[rt]; 73 } 74 int ans=0; 75 if(L<=mid) ans+=query(L,R,lson); 76 if(R>mid) ans+=query(L,R,rson); 77 return ans; 78 } 79 int main() 80 { 81 int i,j,k,q,tt; 82 #ifndef ONLINE_JUDGE 83 freopen("1.in","r",stdin); 84 #endif 85 scanf("%d",&tt); 86 int ca=1; 87 while(tt--) 88 { 89 scanf("%d%d",&n,&m); 90 build(root); 91 for(i=1;i<=n;i++) 92 { 93 num[i].in(i); 94 } 95 for(i=0;i<m;i++) 96 { 97 node[i].in(i); 98 } 99 sort(num+1,num+n+1,cmp1); 100 sort(node,node+m,cmp2); 101 i=0,j=1; 102 while(i<m) 103 { 104 while(j<=n) 105 { 106 if(node[i].H<num[j].val) break; 107 update(num[j].id,root); 108 j++; 109 } 110 while(i<m) 111 { 112 if(j<=n&&node[i].H>=num[j].val) break; 113 o[node[i].id]=query(node[i].L,node[i].R,root); 114 i++; 115 } 116 } 117 printf("Case %d: ",ca++); 118 for(i=0;i<m;i++) 119 { 120 printf("%d ",o[i]); 121 } 122 } 123 return 0; 124 }