Description
You have a n*n board with an integer in each grid, all at 0 at the beginning, and now you need to maintain two operations:
Command |
Parameter limits |
Content |
1 x y A |
1<=x,y<=n,a is a positive integer |
Add the numbers in the lattice x, Y, plus a |
2 x1 y1 x2 y2 |
1<=x1<= x2<=n 1<=y1<= y2<=n |
Output x1 y1 x2 y2 the number within the rectangle and |
3 |
No |
Terminating programs |
Input
Enter the first line of the file with a positive integer n.
Next, one action per line. Each command, in addition to the first number,
The answer to the Last_ans or the last output is the initial last_ans=0.
Output
For each 2 operation, output a corresponding answer.
Sample Input
4
1 2 3 3
2 1 1) 3 3
1 1 1 1
2 1 1) 0 7
3
Sample Output
3
5
HINT
Data size and conventions
1<=n<=500000, no more than 200,000 operations, memory limit 20M, ensure that the answer in the range of int and decoded data is still valid.
Sample explanation See OJ2683
New data set, but not re-tested--2015.05.24
I finally had a!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.
Because the memory is very tight, we cannot use the tree to set the tree, but the k-d tree is still possible.
Considering that the person may have k-d the shape of the tree, we can reconstruct the whole tree on a regular basis.
#include <cstdio>#include<cctype>#include<queue>#include<cstring>#include<algorithm>#defineRep (s,t) for (int i=s;i<=t;i++)#defineren for (int i=first[x];i!=-1;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=200010;intN,d,lastans;structPoint {intd[2],mx[2],mn[2],v,l,r,sum; BOOL operator== (Constpoint& ths)Const{returnd[0]==ths.d[0]&&d[1]==ths.d[1];} BOOL operator< (Constpoint& ths)Const{returnd[d]<ths.d[d];}} P[MAXN];int inch(int& X1,int& Y1,int& X2,int& Y2,int& X1,int& Y1,int& X2,int& Y2) {returnx1<=x1&&x2>=x2&&y1<=y1&&y2>=Y2;}int out(int& X1,int& Y1,int& X2,int& Y2,int& X1,int& Y1,int& X2,int& Y2) {returnx1>x2| | x2<x1| | y1>y2| | y2<Y1;}structKd_tree {point t[maxn],now; intrt,cnt; Kd_tree () {t[0].mn[0]=t[0].mn[1]=2e9;t[0].mx[0]=t[0].mx[1]=-2e9;} voidMaintain (intk) {intL=t[k].l,r=T[K].R; Rep (0,1) T[k].mn[i]=min (T[k].d[i],min (t[l].mn[i],t[r].mn[i)), t[k].mx[i]=Max (T[k].d[i],max (t[l].mx[i],t[r].mx[i)); T[k].sum=t[l].sum+t[r].sum+t[k].v; } voidInsertint& K,intd) {if(!k) k=++cnt,t[k].d[0]=t[k].mx[0]=t[k].mn[0]=now.d[0],t[k].d[1]=t[k].mx[1]=t[k].mn[1]=now.d[1]; if(now==T[k]) {T[K].V+=now.v;t[k].sum+=now.v; return; } if(Now.d[d]<t[k].d[d]) insert (t[k].l,d^1); ElseInsert (t[k].r,d^1); Maintain (k); } intQueryintKintX1,intY1,intX2,inty2) { if(!k)return 0; if(inch(x1,y1,x2,y2,t[k].mn[0],t[k].mn[1],t[k].mx[0],t[k].mx[1]))returnt[k].sum; if( out(x1,y1,x2,y2,t[k].mn[0],t[k].mn[1],t[k].mx[0],t[k].mx[1]))return 0; intret=0; if(inch(x1,y1,x2,y2,t[k].d[0],t[k].d[1],t[k].d[0],t[k].d[1])) ret+=t[k].v; RET+=query (t[k].l,x1,y1,x2,y2) +query (T[K].R,X1,Y1,X2,Y2); returnret; } intBuildintLintRintd) {if(L>r)return 0; intMid=l+r>>1;D =d;nth_element (p+l,p+mid,p+r+1); T[mid]=p[mid];t[mid].l=build (l,mid-1, d^1); T[mid].r=build (mid+1, r,d^1); Maintain (mid);returnmid; }}t;intMain () {n=read ();inttot=0; while(1) { inttp=read (); if(tp==3) Break; Else if(tp==1) { intX=read () ^lastans,y=read () ^Lastans; t.now.d[0]=x; t.now.d[1]=y; T.NOW.V=t.now.sum=read () ^Lastans; T.insert (T.rt,0); if(++tot==7000) {Rep (1, t.cnt) p[i]=T.t[i]; T.rt=t.build (1, t.cnt,0); tot=0; } } Else { intX=read () ^lastans,y=read () ^lastans,x2=read () ^lastans,y2=read () ^Lastans; printf ("%d\n", lastans=t.query (t.rt,x,y,x2,y2)); } } return 0;}
View Code
BZOJ4066 Simple Questions