@OceanEye7年前
04/21
16:09
CDQ分治做三维偏序
很裸很裸
直接看代码吧= =
[CDQ分治的思想就是一段询问有三个维度,离散化两个维度,对第三个维度我们扫一遍来计算贡献]
[通常取mid是比较优秀的]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #define p(x) ('0'<=x&&x<='9') #define INF 100000000 char cc; int C; template <class T> void read( T &x ) { x=0; cc=getchar(); C=1; while(!p(cc)) { if(cc=='-') C=-1; cc=getchar(); } while(p(cc)) { x=x*10+cc-48; cc=getchar(); } x*=C; } using namespace std; #define N 200000 #define lowbit(x) (x&-x) int Arr[N+10]; void inc(int x) { while(x<=N) { Arr[x]++; x+=lowbit(x); } } void dec(int x) { while(x<=N) { Arr[x]--; x+=lowbit(x); } } int query(int x) { int ret=0; while(x) { ret+=Arr[x]; x-=lowbit(x); } return ret; } #undef lowbit struct q { int x,y,z; int cp; }Q[N],NQ[N]; int ans[N],n,m,cnt[N]; bool comp(const q &x,const q &y) { if(x.x==y.x&&x.y==y.y) return x.z<y.z; if(x.x==y.x) return x.y<y.y; return x.x<y.x; } void pre() { read(n); read(m); for(int i=1;i<=n;i++) { read(Q[i].x); read(Q[i].y); read(Q[i].z); Q[i].cp=i; } sort(Q+1,Q+1+n,comp); } void solve(int l,int r,int L,int R) { if(l>r) return; if(L==R) { int node=l; while(node<=r) { int k=node+1; while(Q[node].x==Q[k].x&&k<=r) k++; for(int i=node;i<k;i++) inc(Q[i].y); for(;node<k;node++) ans[Q[node].cp]+=query(Q[node].y); } for(int i=l;i<=r;i++) dec(Q[i].y); return; } int mid=(L+R)>>1; for(int i=l;i<=r;i++) { if(Q[i].z<=mid) inc(Q[i].y); if(Q[i].z>mid) ans[Q[i].cp]+=query(Q[i].y); } for(int i=l;i<=r;i++) if(Q[i].z<=mid) dec(Q[i].y); int nodeL=l,nodeR=mid+1; int nl[2],nr[2]; nl[0]=nr[0]=INF; nl[1]=nr[1]=-INF; for(int i=l;i<=r;i++)if(Q[i].z<=mid) { NQ[nodeL++]=Q[i]; nl[0]=min(nl[0],Q[i].z); nl[1]=max(nl[1],Q[i].z); } nodeR=nodeL; for(int i=l;i<=r;i++) if(Q[i].z>mid) { NQ[nodeR++]=Q[i]; nr[0]=min(nr[0],Q[i].z); nr[1]=max(nr[1],Q[i].z); } for(int i=l;i<=r;i++) Q[i]=NQ[i]; solve(l,nodeL-1,nl[0],nl[1]); solve(nodeL,r,nr[0],nr[1]); } int main() { pre(); solve(1,n,1,m); //for(int i=1;i<=n;i++) printf("%d\n",ans[i]); for(int i=1;i<=n;i++) cnt[ans[i]]++; for(int i=1;i<=n;i++) printf("%d\n",cnt[i]); return 0; } |