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

# 题解

• 这显然就是个三维偏序问题
• 就先排序，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  阅读(147)  评论(0编辑  收藏  举报