BZOJ3262: 陌上花开
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3262
CDQ分治。不就是从二维变成三维了。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define inf 1<<30 6 #define maxn 100005 7 #define maxk 200005 8 using namespace std; 9 int n,k,cnt,tot,t[maxk],sum[maxn]; 10 struct fuck{int a,b,c,s,ans;}e[maxn],q[maxn]; 11 int read(){ 12 int x=0,f=1; char ch; 13 for(ch=getchar();ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=-1; 14 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; 15 return x*f; 16 } 17 bool comp(fuck x,fuck y){ 18 if(x.b==y.b) return x.c<y.c; 19 return x.b<y.b; 20 } 21 bool cmp(fuck x,fuck y){ 22 if(x.a==y.a&&x.b==y.b) return x.c<y.c; 23 if(x.a==y.a) return x.b<y.b; 24 return x.a<y.a; 25 } 26 void change(int x,int y){for(int j=x;j<=k;j+=j&-j) t[j]+=y;} 27 int query(int x){int y=0; for(int j=x;j;j-=j&-j) y+=t[j]; return y;} 28 void solve(int l,int r){ 29 if(l==r) return; 30 int mid=(l+r)>>1; 31 solve(l,mid); solve(mid+1,r); 32 sort(q+l,q+mid+1,comp); sort(q+mid+1,q+r+1,comp); 33 int i=l,j=mid+1,last=0; 34 while(j<=r){ 35 while(i<=mid&&q[i].b<=q[j].b) change(q[i].c,q[i].s),last=i++; 36 q[j].ans+=query(q[j].c); j++; 37 } 38 for(int i=l;i<=last;i++) change(q[i].c,-q[i].s); 39 } 40 int main(){ 41 n=read(); k=read(); 42 for(int i=1;i<=n;i++){e[i].a=read(); e[i].b=read(); e[i].c=read(); e[i].ans=0;} 43 sort(e+1,e+n+1,cmp); 44 for(int i=1;i<=n;i++){ 45 cnt++; 46 if(e[i].a!=e[i+1].a||e[i].b!=e[i+1].b||e[i].c!=e[i+1].c){ 47 q[++tot]=e[i]; q[tot].s=cnt; cnt=0; 48 } 49 } 50 solve(1,tot); 51 for(int i=1;i<=tot;i++) sum[q[i].ans+q[i].s-1]+=q[i].s; 52 for(int i=0;i<n;i++) printf("%d\n",sum[i]); 53 } 54