来自FallDream的博客,未经允许,请勿转载,谢谢。
------------------------------------------------------
要求支持两种操作1)给点(x,y)加上一个权值v 2)查询矩阵(x1,y1,x2,y2)的权值和。操作数最多200000,强制在线。
大力kdtree
#include<iostream> #include<cstdio> #include<algorithm> #define MN 200000 using namespace std; inline int read() { int x = 0 , f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar();} while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();} return x * f; } int n=0,last,F,m; struct P{ int d[2],mn[2],mx[2],l,r,sum,num; P(){} P(int x1,int y1,int x2,int y2){mn[0]=x1;mx[0]=x2;mn[1]=y1;mx[1]=y2;} P(int x,int y){d[0]=x;d[1]=y;} int&operator[](int x){return d[x];} bool operator <(const P&b)const{return d[F]<b.d[F];} bool operator == (const P&b)const{return d[0]==b.d[0]&&d[1]==b.d[1];} inline bool in(const P&b)const{return b.mn[0]>=mn[0]&&b.mx[0]<=mx[0]&&b.mn[1]>=mn[1]&&b.mx[1]<=mx[1];} inline bool out(const P&b)const{return mn[0]>b.mx[0]||mx[0]<b.mn[0]||mn[1]>b.mx[1]||mx[1]<b.mn[1];} inline bool in(const int&x,const int&y)const{return x>=mn[0]&&x<=mx[0]&&y>=mn[1]&&y<=mx[1];} // void print(){cout<<mn[0]<<" "<<mx[0]<<" "<<mn[1]<<" "<<mx[1]<<endl;} }t[MN+5]; struct tree{ P p[MN+5],T; void update(int x) { int l=p[x].l,r=p[x].r; for(int i=0;i<2;i++) { if(l) p[x].mn[i]=min(p[x].mn[i],p[l].mn[i]), p[x].mx[i]=max(p[x].mx[i],p[l].mx[i]); if(r) p[x].mn[i]=min(p[x].mn[i],p[r].mn[i]), p[x].mx[i]=max(p[x].mx[i],p[r].mx[i]); } p[x].sum=p[l].sum+p[r].sum+p[x].num; } int build(int lt,int rt,bool now) { if(lt>rt)return 0; int mid=lt+rt>>1; F=now;nth_element(t+lt,t+mid,t+rt+1); p[mid]=t[mid]; p[mid].mn[0]=p[mid].mx[0]=p[mid][0]; p[mid].mn[1]=p[mid].mx[1]=p[mid][1]; p[mid].l=build(lt,mid-1,now^1); p[mid].r=build(mid+1,rt,now^1); update(mid); return mid; } int query(int x) { if(!x)return 0; if(T.in(p[x])) return p[x].sum; if(T.out(p[x])) return 0; int sum=0; if(T.in(p[x].d[0],p[x].d[1])) sum+=p[x].num; sum+=query(p[x].l)+query(p[x].r); return sum; } void ins(int&x,int v,bool now) { if(!x){ x=++n;p[x]=T; for(register int i=0;i<2;i++) p[x].mn[i]=p[x].mx[i]=p[x][i]; p[x].sum=p[x].num=v;p[x].l=p[x].r=0;return; } if(p[x]==T){ p[x].sum+=v;p[x].num+=v;return;} if(T[now]<p[x][now]) ins(p[x].l,v,now^1); else ins(p[x].r,v,now^1); update(x); } }tree; int rt; int main() { read(); for(register int i=1;;) { int opt=read();if(opt>2)break; if(opt==1) { int x=read()^last,y=read()^last,v=read()^last;tree.T=P(x,y); tree.ins(rt,v,0); if(++i==10000) { for(int j=1;j<=n;j++) t[j]=tree.p[j]; rt=tree.build(1,n,0);i=0; } } else { int x1=read()^last,y1=read()^last,x2=read()^last,y2=read()^last; tree.T=P(x1,y1,x2,y2); printf("%d ",last=tree.query(rt)); } } return 0; }