# bzoj3262: 陌上花开

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #define maxn 100005
7 #define maxk 200005
8 using namespace std;
9
10 int n,k,m,sum[maxk],ans[maxn],sum_[maxn],ans_[maxn];
11 struct date{
12     int x,y,z;
13 }fl[maxn];
14 struct note{
15     int y,z,sum,id;
16 }qs[maxn];
17
18 bool comp1(date x,date y){
19     if (x.x!=y.x) return x.x<y.x;
20     if (x.y!=y.y) return x.y<y.y;
21     return x.z<y.z;
22 }
23
24 bool comp2(note x,note y){
25     if (x.y==y.y) return x.z<y.z;
26     return x.y<y.y;
27 }
28
29 int lowbit(int x){
30     return x&(-x);
31 }
32
34     for (int i=x;i<=k;i+=lowbit(i)) sum[i]+=y;
35 }
36
37 int query(int x){
38     int temp=0;
39     for (int i=x;i>0;i-=lowbit(i)){
40         temp+=sum[i];
41     }
42     return temp;
43 }
44
45 void cdq_solve(int l,int r){
46     if (l==r) return;
47     int mid=(l+r)/2;
48     cdq_solve(l,mid),cdq_solve(mid+1,r);
49     sort(qs+l,qs+mid+1,comp2),sort(qs+mid+1,qs+r+1,comp2);
50     int i=l,j=mid+1,temp=0;
51     while (j<=r){
53         else ans_[qs[j].id]+=query(qs[j].z),j++;
54     }
56 }
57
58 int main(){
59     scanf("%d%d",&n,&k),m=0;
60     for (int i=1;i<=n;i++) scanf("%d%d%d",&fl[i].x,&fl[i].y,&fl[i].z);
61     sort(fl+1,fl+n+1,comp1);
62 //    for (int i=1;i<=n;i++) printf("%d %d %d\n",fl[i].x,fl[i].y,fl[i].z);
63     memset(sum,0,sizeof(sum));
64     memset(ans,0,sizeof(ans));
65     memset(ans_,0,sizeof(ans_));
66     for (int i=1;i<=n;i++){
67         if (fl[i].x==fl[i-1].x&&fl[i].y==fl[i-1].y&&fl[i].z==fl[i-1].z){
68             sum_[m]=++qs[m].sum;
69         }else{
70             qs[++m].sum=1,qs[m].y=fl[i].y,qs[m].z=fl[i].z,qs[m].id=m;
71             sum_[m]=qs[m].sum;
72         }
73     }
74     cdq_solve(1,m);
75     for (int i=1;i<=m;i++) ans[ans_[i]+sum_[i]-1]+=sum_[i];
76     for (int i=0;i<n;i++) printf("%d\n",ans[i]);
77     return 0;
78 }
View Code

cdq分治+树状数组
posted @ 2016-05-19 20:23  oyzx~  阅读(213)  评论(1编辑  收藏  举报