P3810 【模板】三維偏序(陌上花開) cdq分治
阿新 • • 發佈:2021-07-20
模板題而已
不會cdq的看這裡:cdq分治:從分治到套娃
一維排序,二維cdq,三維樹狀陣列
#include<algorithm> #include<iostream> #include<cstdio> using namespace std; int n1,n2,k; const int N=100010,K=200010; int ans[N],tr[K]; struct treAKer{int a,b,c,num,ans;}a[N],b[N]; int my_a(const treAKer &a,const treAKer &b) { if(a.a==b.a) { if(a.b==b.b)return a.c<b.c; return a.b<b.b; } return a.a<b.a; } int my_b(const treAKer &a,const treAKer &b) { if(a.b==b.b)return a.c<b.c; return a.b<b.b; } int lowbit(int x){return x&(-x);} int ask(int x) { int ans=0; for(;x;x-=lowbit(x))ans+=tr[x]; return ans; } void change(int x,int v){for(;x<=k;x+=lowbit(x))tr[x]+=v;} void cdq(int l,int r) { if(l==r)return; int mid=(l+r)>>1; cdq(l,mid);cdq(mid+1,r);sort(b+l,b+mid+1,my_b);sort(b+mid+1,b+r+1,my_b); int i=l,j=mid+1; for(;j<=r;++j) { while(i<=mid&&b[i].b<=b[j].b)change(b[i].c,b[i].num),++i; b[j].ans+=ask(b[j].c); } for(int k=l;k<=i-1;++k)change(b[k].c,-b[k].num); } int main() { cin>>n1>>k; for(int i=1;i<=n1;++i)scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c); sort(a+1,a+n1+1,my_a); for(int i=1,now=0;i<=n1;++i) { ++now; if(a[i].a!=a[i+1].a||a[i].b!=a[i+1].b||a[i].c!=a[i+1].c)b[++n2]=a[i],b[n2].num=now,now=0; } cdq(1,n2); for(int i=1;i<=n2;++i)ans[b[i].ans+b[i].num-1]+=b[i].num; for(int i=0;i<n1;++i)printf("%d\n",ans[i]); return 0; }