[cdq分治][树状数组] Bzoj P3262 陌上花开

Description

有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),用三个整数表示。
现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。
定义一朵花A比另一朵花B要美丽,当且仅Sa>=Sb,Ca>=Cb,Ma>=Mb。
显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。
 

Input

第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值。
以下N行,每行三个整数si, ci, mi (1 <= si, ci, mi <= K),表示第i朵花的属性

Output

包含N行,分别表示评级为0...N-1的每级花的数量。

Sample Input

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

Sample Output

3
1
3
0
1
0
1
0
0
1

 

题解

  • 这显然就是个三维偏序问题
  • 就先排序,cdq套树状数组就好了

代码

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N=200001;
 7 struct node{ int x,y,z,id; }a[N];
 8 int c[N<<2],k,n,b[N],bj[N],f[N];
 9 int lowbit(int x){ return x&(-x); }
10 void add(int x,int v){ while(x<=k) c[x]+=v,x+=lowbit(x); }
11 int sum(int x)
12 {
13     int ans=0;
14     while (x) ans+=c[x],x-=lowbit(x);
15     return ans;
16 }
17 bool cmp1(const node &a,const node &b)
18 {
19     if (a.x!=b.x) return a.x<b.x;
20     if (a.y!=b.y) return a.y<b.y;
21     return a.z<b.z;
22 }
23 bool cmp2(const node &a,const node &b)
24 {
25     if (a.y!=b.y) return a.y<b.y;
26     if (a.z!=b.z) return a.z<b.z;
27     return a.x<b.x;
28 }
29 void cdq(int l,int r)
30 {
31     if (l==r) return;
32     int mid=(l+r)>>1,flag;
33     cdq(l,mid),cdq(mid+1,r);
34     sort(a+l,a+r+1,cmp2);
35     for (int i=l;i<=r;i++) (a[i].x<=mid)?add(a[i].z,1),flag=i:b[a[i].id]+=sum(a[i].z);
36     for (int i=l;i<=r;i++) if (a[i].x<=mid) add(a[i].z,-1);
37 }
38 int main()
39 {
40     scanf("%d%d",&n,&k);
41     for (int i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z),a[i].id=i;
42     sort(a+1,a+1+n,cmp1);
43     for (int i=1;i<=n;)
44     {
45         int j=i+1;
46         while (j<=n&&a[j].x==a[i].x&&a[j].y==a[i].y&&a[j].z==a[i].z) j++;
47         while (i<j) bj[a[i].id]=a[j-1].id,i++;
48     }
49     for(int i=1;i<=n;i++) a[i].x=i;
50     cdq(1,n);
51     for(int i=1;i<=n;i++) f[b[bj[a[i].id]]]++;
52     for(int i=0;i<n;i++) printf("%d\n",f[i]);
53 }

 

posted @ 2019-07-07 11:39  BEYang_Z  阅读(172)  评论(0编辑  收藏  举报