bzoj1176: [Balkan2007]Mokia(CDQ分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1176
CDQ分治
#include<cstdio> #include<iostream> #include<algorithm> #define lowbit(x) x&-x using namespace std; #define N 160001 #define M 10001 typedef long long LL; int w; LL c[2000001]; struct node { int id; int x,y,bl,mul; bool ty; }e[N+M*4],tmp[N+M*4]; LL ans[M]; void read(int &x) { x=0; int f=1; char c=getchar(); while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); } while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } x*=f; } bool cmp(node p,node q) { if(p.x!=q.x) return p.x<q.x; return p.ty<q.ty; } void change(int x,int k) { while(x<=w) { c[x]+=k; x+=lowbit(x); } } LL query(int x) { LL sum=0; while(x) { sum+=c[x]; x-=lowbit(x); } return sum; } void solve(int l,int r) { if(l==r) return; int mid=l+r>>1; int sum=0; for(int i=l;i<=r;++i) { if(!e[i].ty && e[i].id<=mid) change(e[i].y,e[i].mul); else if(e[i].ty && e[i].id>mid) ans[e[i].bl]+=e[i].mul*query(e[i].y); } for(int i=l;i<=r;++i) { if(!e[i].ty && e[i].id<=mid) change(e[i].y,-e[i].mul); } int L=l,R=mid+1; for(int i=l;i<=r;++i) { if(e[i].id<=mid) tmp[L++]=e[i]; else tmp[R++]=e[i]; } for(int i=l;i<=r;++i) e[i]=tmp[i]; solve(l,mid); solve(mid+1,r); } int main() { int s; read(s); read(w); int ty,lx,ly,rx,ry,k; int tot=0,cnt=0; while(scanf("%d",&ty)) { if(ty==1) { e[++tot].id=tot; read(e[tot].x); read(e[tot].y); read(e[tot].mul); } else if(ty==2) { read(lx); read(ly); read(rx); read(ry); cnt++; e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=1; e[tot].ty=true; e[tot].x=rx; e[tot].y=ry; e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=1; e[tot].ty=true; e[tot].x=lx-1; e[tot].y=ly-1; e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=-1; e[tot].ty=true; e[tot].x=lx-1; e[tot].y=ry; e[++tot].id=tot; e[tot].bl=cnt; e[tot].mul=-1; e[tot].ty=true; e[tot].x=rx; e[tot].y=ly-1; } else break; } sort(e+1,e+tot+1,cmp); solve(1,tot); for(int i=1;i<=cnt;++i) cout<<ans[i]<<'\n'; }