# bzoj3262陌上花开 cdq分治

## 3262: 陌上花开

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 2794  Solved: 1250
[Submit][Status][Discuss]

10 3
3 3 3
2 3 3
2 3 1
3 1 1
3 1 2
1 3 1
1 1 2
1 2 2
1 3 2
1 2 1

3
1
3
0
1
0
1
0
0
1

## HINT

1 <= N <= 100,000, 1 <= K <= 200,000

## Source

 1 #include<bits/stdc++.h>
2 #define N 100005
3 using namespace std;
4 int n,m,pa,pb,acnt,bcnt,tot,cnt[N],pos[N],c[N<<1];
5 struct info{int x,y,z,id,ans;}q[N],a[N],b[N];
6 bool cmp1(info a,info b){
7     if(a.x==b.x){
8         if(a.y==b.y)return a.z<b.z;
9         return a.y<b.y;
10     }
11     return a.x<b.x;
12 }
13 bool cmp2(info a,info b){return a.y<b.y;}
15     while(p<=m){
16         c[p]+=v;
17         p+=p&-p;
18     }
19 }
20 int query(int p){
21     int ret=0;
22     while(p){
23         ret+=c[p];
24         p-=p&-p;
25     }
26     return ret;
27 }
28 void cdq(int l,int r){
29     if(l>=r)return;
30     int mid=(l+r)>>1;
31     cdq(l,mid);
32     acnt=bcnt=0;
33     for(int i=l;i<=mid;++i)a[++acnt]=q[i];
34     for(int i=mid+1;i<=r;++i)b[++bcnt]=q[i];
35     sort(a+1,a+1+acnt,cmp2);
36     sort(b+1,b+1+bcnt,cmp2);
37     pa=pb=1;tot=0;
38     while(pb<=bcnt){
39         while(pa<=acnt&&a[pa].y<=b[pb].y){
41             pos[++tot]=a[pa].z;
42             ++pa;
43         }
44         q[b[pb].id].ans+=query(b[pb].z);
45         ++pb;
46     }
48     cdq(mid+1,r);
49 }
50 int main(){
51     scanf("%d%d",&n,&m);
52     for(int i=1;i<=n;++i)
53     scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].z);
54     sort(q+1,q+1+n,cmp1);
55     for(int i=1;i<=n;++i)q[i].id=i;
56     cdq(1,n);
57     for(int i=n;i;--i)
58     if(q[i].x==q[i+1].x&&q[i].y==q[i+1].y&&q[i].z==q[i+1].z)
59     q[i].ans=max(q[i].ans,q[i+1].ans);
60     for(int i=1;i<=n;++i)++cnt[q[i].ans];
61     for(int i=0;i<n;++i)printf("%d\n",cnt[i]);
62     return 0;
63 }

If you live in the echo,
your heart never beats as loud.

posted @ 2017-12-28 09:56  _wsy  阅读(188)  评论(0编辑  收藏  举报