Kdtree和平衡树有些类似,只不过维护的是高维向量.我们可以模仿平衡树的方法,维护Kdtree每颗子树所代表的点集包含的信息,然后这题就解决了.
在这一题里树会很不平衡,要插入若干次后重构整棵树,代码和build函数类似.
SB错:123行的scanf写在了if的后面,呵呵.
AC code:
#include <cstdio>
#include <algorithm>
using namespace std;
const int K=2;
const int N=500010;
int n,tot,la,cnt,flg;
struct Poi{
int d[K];
};
struct nod{
Poi poi;
int val,sum;
int mxd[K],mnd[K];
nod *ch[2];
friend bool operator<(nod x,nod y){
return x.poi.d[flg]<y.poi.d[flg];
}
}pool[N];
struct Kdtree{
nod *root;
Kdtree(){
root=NULL;
}
void update(nod *p){
p->sum=p->val;
if(p->ch[0]!=NULL) p->sum+=p->ch[0]->sum;
if(p->ch[1]!=NULL) p->sum+=p->ch[1]->sum;
for(int i=0;i<K;i++){
p->mxd[i]=p->mnd[i]=p->poi.d[i];
if(p->ch[0]!=NULL){
p->mxd[i]=max(p->mxd[i],p->ch[0]->mxd[i]);
p->mnd[i]=min(p->mnd[i],p->ch[0]->mnd[i]);
}
if(p->ch[1]!=NULL){
p->mxd[i]=max(p->mxd[i],p->ch[1]->mxd[i]);
p->mnd[i]=min(p->mnd[i],p->ch[1]->mnd[i]);
}
}
}
void insert(nod **p,int x,int y,int v,int flag){
if(*p==NULL){
*p=&pool[++tot];
(*p)->val=(*p)->sum=v;
(*p)->poi.d[0]=(*p)->mxd[0]=(*p)->mnd[0]=x;
(*p)->poi.d[1]=(*p)->mxd[1]=(*p)->mnd[1]=y;
return ;
}
if((flag&&y<=(*p)->poi.d[1])||((!flag)&&x<=(*p)->poi.d[0])) insert(&(*p)->ch[0],x,y,v,(flag+1)%K);
else insert(&(*p)->ch[1],x,y,v,(flag+1)%K);
update(*p);
}
bool getpoi(nod *p,int x,int y,int flag){
if(p==NULL) return 0;
if(p->poi.d[0]==x&&p->poi.d[1]==y) return 1;
if((flag&&y<=p->poi.d[1])||((!flag&&x<=p->poi.d[0]))) return getpoi(p->ch[0],x,y,(flag+1)%K);
else return getpoi(p->ch[1],x,y,(flag+1)%K);
}
void add(nod *p,int x,int y,int v,int flag){
if(p->poi.d[0]==x&&p->poi.d[1]==y){
p->val+=v;p->sum+=v;
return ;
}
if((flag&&y<=p->poi.d[1])||((!flag)&&x<=p->poi.d[0])) add(p->ch[0],x,y,v,(flag+1)%K);
else add(p->ch[1],x,y,v,(flag+1)%K);
update(p);
}
int getsum(nod *p,int x1,int y1,int x2,int y2){
if(x1>x2||y1>y2) return 0;
if(x1<=p->mnd[0]&&x2>=p->mxd[0]&&y1<=p->mnd[1]&&y2>=p->mxd[1]) return p->sum;
int v=(x1<=p->poi.d[0]&&x2>=p->poi.d[0]&&y1<=p->poi.d[1]&&y2>=p->poi.d[1])?p->val:0;
if(p->ch[0]!=NULL) v+=getsum(p->ch[0],max(x1,p->ch[0]->mnd[0]),max(y1,p->ch[0]->mnd[1]),min(x2,p->ch[0]->mxd[0]),min(y2,p->ch[0]->mxd[1]));
if(p->ch[1]!=NULL) v+=getsum(p->ch[1],max(x1,p->ch[1]->mnd[0]),max(y1,p->ch[1]->mnd[1]),min(x2,p->ch[1]->mxd[0]),min(y2,p->ch[1]->mxd[1]));
return v;
}
void rebuild(nod **p,int L,int R,int flag){
if(L>R){
*p=NULL;
return ;
}
if(L==R){
*p=&pool[L];
(*p)->poi=pool[L].poi;
(*p)->val=(*p)->sum=pool[L].val;
for(int i=0;i<K;i++) (*p)->mxd[i]=(*p)->mnd[i]=pool[L].poi.d[i];
(*p)->ch[0]=(*p)->ch[1]=NULL;
return ;
}
int M=(L+R)>>1;
flg=flag;
nth_element(pool+L,pool+M,pool+R+1);
*p=&pool[M];
rebuild(&(*p)->ch[0],L,M-1,(flag+1)%K);
rebuild(&(*p)->ch[1],M+1,R,(flag+1)%K);
update(*p);
}
}T;
int main(){
scanf("%d",&n);
while(1){
int t;
scanf("%d",&t);
if(t==3) break;
else if(t==1){
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
x^=la;y^=la;v^=la;
if(!T.getpoi(T.root,x,y,0)){
T.insert(&T.root,x,y,v,0);
cnt++;
if(!(cnt%5000)) T.rebuild(&T.root,1,tot,0);
}
else T.add(T.root,x,y,v,0);
}
else{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(T.root==NULL){
printf("0\n");
continue;
}
x1^=la;y1^=la;x2^=la;y2^=la;
la=T.getsum(T.root,max(x1,T.root->mnd[0]),max(y1,T.root->mnd[1]),min(x2,T.root->mxd[0]),min(y2,T.root->mxd[1]));
printf("%d\n",la);
}
}
return 0;
}