陌上花开,缓缓归矣——赠戴氏夫人
CDQ模板
一维sort,二维归并,三维树状数组
二维归并时,维护所求值
#include<bits/stdc++.h> #define re return #define lowbit(x) (x&(-x)) #define inc(i,l,r) for(int i=l;i<=r;++i) typedef long long ll; const int maxn=200005,maxm=400005; using namespace std; template<typename T>inline void rd(T&x) { char c;bool f=0; while((c=getchar())<'0'||c>'9')if(c=='-')f=1; x=c^48; while((c=getchar())>='0'&&c<='9')x=x*10+(c^48); if(f)x=-x; } int n,m,c[maxm],ans[maxn]; struct node{ int a,b,c,w,f; bool operator<(node x)const { if(a!=x.a)re a<x.a; if(b!=x.b)re b<x.b; re c<x.c; } inline void read() { rd(a),rd(b),rd(c); } bool operator!=(node x)const { re ((a!=x.a)||(b!=x.b)||(c!=x.c)); } }h[maxn],t[maxn]; inline void add(int x,int y) { while(x<=m){c[x]+=y;x+=lowbit(x);} } inline ll sum(int x) { ll res=0; while(x){res+=c[x];x-=lowbit(x);} re res; } inline void CDQ(int l,int r) { if(l==r)re ; int mid=(l+r)>>1; CDQ(l,mid);CDQ(mid+1,r); int p=l,q=mid+1,tot=l; while(p<=mid&&q<=r) { if(h[p].b<=h[q].b)add(h[p].c,h[p].w),t[tot++]=h[p++]; else h[q].f+=sum(h[q].c),t[tot++]=h[q++]; } while(p<=mid)add(h[p].c,h[p].w),t[tot++]=h[p++]; while(q<=r)h[q].f+=sum(h[q].c),t[tot++]=h[q++]; inc(i,l,mid)add(h[i].c,-h[i].w); inc(i,l,r)h[i]=t[i]; } int main() { rd(n),rd(m); inc(i,1,n)h[i].read(); sort(h+1,h+n+1); /* int tot=0; inc(i,1,n) { if(h[i]!=h[i-1])h[++tot]=h[i]; ++h[tot].w; }*/ int tot=1; h[1].w=1; inc(i,2,n) { if(h[i]!=h[i-1])h[++tot]=h[i]; ++h[tot].w; } CDQ(1,tot); inc(i,1,n)ans[h[i].f+h[i].w-1]+=h[i].w; inc(i,0,n-1) printf("%d ",ans[i]); re 0; }