# 【bzoj3262】陌上花开

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

CDQ分治的裸题，三维偏序。

ci放入树状数组中即可，然后bi查询一下，有多少个。

 1 #include<cstring>
2 #include<cmath>
3 #include<cstdio>
4 #include<iostream>
5 #include<algorithm>
6
7 #define ll long long
8 #define NN 2000007
9 using namespace std;
11 {
12     int x=0,f=1;char ch=getchar();
13     while(ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
14     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
15     return f*x;
16 }
17
18 int n,m,tr[NN],ans[NN];
19 struct Node
20 {
21     int a,b,c,s,ans;
22 }a[NN],p[NN];
23
24 inline int lowbit(int x)
25 {
26     return x&(-x);
27 }
28 inline void updata(int x,int num)
29 {
30     for (int i=x;i<=m;i+=lowbit(i))
31         tr[i]+=num;
32 }
33 inline int query(int x)
34 {
35     int res=0;
36     for (int i=x;i>=1;i-=lowbit(i))
37         res+=tr[i];
38     return res;
39 }
40 inline bool cmp1(Node x,Node y)
41 {
42     if (x.a==y.a&&x.b==y.b) return x.c<y.c;
43     if (x.a==y.a) return x.b<y.b;
44     return x.a<y.a;
45 }
46 inline bool cmp2(Node x,Node y)
47 {
48     if (x.b==y.b) return x.c<y.c;
49     return x.b<y.b;
50 }
51 void cdq(int l,int r)
52 {
53
54     if (l==r) return;
55     int mid=(l+r)>>1;
56     cdq(l,mid),cdq(mid+1,r);
57     sort(p+l,p+mid+1,cmp2),sort(p+mid+1,p+r+1,cmp2);
58     int i=l,j=mid+1;
59     while(j<=r)
60     {
61         while(i<=mid&&p[i].b<=p[j].b)
62         {
63             updata(p[i].c,p[i].s);
64             i++;
65         }
66         p[j].ans+=query(p[j].c);
67         j++;
68     }
69     for (int j=l;j<i;j++)//只能到i为止
70         updata(p[j].c,-p[j].s);
71 }
72 int main()
73 {
75     for (int i=1;i<=N;i++)
77     sort(a+1,a+N+1,cmp1);
78     int cnt=0;
79     for (int i=1;i<=N;i++)
80     {
81         cnt++;
82         if (a[i].a!=a[i+1].a||a[i].b!=a[i+1].b||a[i].c!=a[i+1].c)
83         {
84             p[++n]=a[i];
85             p[n].s=cnt;
86             cnt=0;
87         }
88     }
89     cdq(1,n);
90     for (int i=1;i<=n;i++)
91         ans[p[i].ans+p[i].s-1]+=p[i].s;
92     for (int i=0;i<N;i++)
93         printf("%d\n",ans[i]);
94 }

posted @ 2017-12-03 18:50  Kaiser-  阅读(166)  评论(0编辑  收藏  举报