# BZOJ3262:陌上花开(CDQ分治)

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

## Code

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5 #define N (200000+1000)
6 using namespace std;
7
8 int n,k,emm,top,c[N],ans[N];
9
10 int lowbit(int x){return x&-x;}
11 int add(int x,int delta){for (; x<=k; x+=lowbit(x)) c[x]+=delta;}
12 int sum(int x){int sum=0; for (; x; x-=lowbit(x)) sum+=c[x]; return sum;}
13
14 struct Node
15 {
16     int a,b,c,size,ans;
17     bool operator < (const Node &A) const
18     {
19         if (a==A.a && b==A.b) return c<A.c;
20         if (a==A.a) return b<A.b;
21         return a<A.a;
22     }
23 }a[N],t[N];
24
25 void CDQ(int l,int r)
26 {
27     if (l==r) return;
28     int mid=(l+r)>>1;
29     CDQ(l,mid); CDQ(mid+1,r);
30     int i=l,j=mid+1,p=l;
31     while (i<=mid || j<=r)
32     {
33         if (j>r || i<=mid && a[i].b<=a[j].b) add(a[i].c,a[i].size),t[p++]=a[i++];
34         else a[j].ans+=sum(a[j].c),t[p++]=a[j++];
35     }
36     for (int i=l; i<=mid; ++i) add(a[i].c,-a[i].size);
37     for (int i=l; i<=r; ++i) a[i]=t[i];
38 }
39
40 int main()
41 {
42     scanf("%d%d",&n,&k); emm=n;
43     for (int i=1; i<=n; ++i)
44         scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c),a[i].size=1;
45     sort(a+1,a+n+1);
46     top=1;
47     for (int i=2; i<=n; ++i)
48     {
49         if (a[i].a==a[top].a && a[i].b==a[top].b && a[i].c==a[top].c)
50             a[top].size++;
51         else a[++top]=a[i];
52     }
53     n=top;
54     CDQ(1,n);
55     for (int i=1; i<=n; ++i) ans[a[i].ans+a[i].size-1]+=a[i].size;
56     for (int i=0; i<emm; ++i) printf("%d\n",ans[i]);
57 }
posted @ 2018-07-20 07:21  Refun  阅读(187)  评论(0编辑  收藏  举报