Time limit:50 Sec Memory limit:128 MB
submit:1071 solved:428 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 file The first line of a positive integer n. Next, one action per line. Output for each 2 operation, outputs a corresponding answer. Sample Input4
1 2 3 3
2 1 1) 3 3
1 2 2 2
2 2 2) 3 4
3
Sample OUTPUT3
5
hint1<=n<=500000, no more than 200,000 operations, memory limit of 20M.
For 100% of the data, the A in operation 1 is not more than 2000. Source
CDQ Divided treatment
The 4th time to review the previous copy of the code, suddenly epiphany.
Personally, this method of division and treatment is similar to the scanning line method used to make rectangular areas. Each interval modification operation is split into insert/delete, and each query action is sorted by the horizontal axis x ascending.
A one-dimensional array is used to record the y-axis of "current horizontal", and all operations are scanned from left to right, and the change is done in a differential way (differential on the Time dimension) to record the answer.
↑ The one-dimensional array can be optimized with a tree-like array, and the scan operation can be optimized with a split-rule method (the effect of the first half of the operation on the second half of the query is processed at each level).
↑ together it became a CDQ division.
________
PS1: Then I think, two or three months ago, Rlq said he developed a tree-like array of two-dimensional big data, then did not understand, and did not care too much ... Lying trough, the original is CDQ divided treatment?
If the CDQ division is two years later, it becomes the RLQ division ...%%%%%
PS2: Previously copied LCT also has reviewed the more than once, is not also soon epiphany ...
________
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn=200010;9 intRead () {Ten intx=0, f=1;CharCh=GetChar (); One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } the intN; - structopt{ - intFlag; - intx,y,w; + intT; - intID; +}a[mxn*Ten],b[mxn*Ten]; A intCnt=0; at intCMP (opt q,opt e) { - if(q.x==e.x) { - if(Q.Y==E.Y)returnq.flag<E.flag; - returnq.y<e.y; - } - returnq.x<e.x; in } - intANS[MXN]; to intt[mxn*5]; + voidAddintXintV) { while(x<=n) {t[x]+=v;x+=x&-x;}return;} - intSumintx) { the intres=0; * while(x) {res+=t[x];x-=x&-x;} $ returnRes;Panax Notoginseng } - voidSolveintLintR) { the if(L>=R)return; + intI,j,mid= (l+r) >>1; A intL1=l,l2=mid+1; the for(i=l;i<=r;i++){ + if(a[i].flag==1&& a[i].t<=mid) Add (A[I].Y,A[I].W); - Else if(a[i].flag==2&& a[i].t>mid) ans[a[i].id]+=sum (A[I].Y) *A[I].W; $ } $ for(i=l;i<=r;i++) - if(a[i].flag==1&& a[i].t<=mid) Add (a[i].y,-A[I].W); - for(i=l;i<=r;i++) the if(A[i].t<=mid) b[l1++]=A[i]; - Elseb[l2++]=A[i];Wuyi for(i=l;i<=r;i++) a[i]=B[i]; theSolve (l,mid); Solve (mid+1, R); - return; Wu } - intMain () { Aboutn=read (); $ inti,j,x,y,c,v; - intId=0; - while(1){ -C=read (); A if(c==3) Break; + if(c==1){//Modify theX=read (); Y=read (); v=read (); -a[++cnt].flag=1; a[cnt].x=x;a[cnt].y=y;a[cnt].w=v; $a[cnt].t=CNT; the } the Else{//Enquiry theX=read (); Y=read (); C=read (); v=read (); thea[++cnt].flag=2; a[cnt].x=x-1; a[cnt].y=y-1; -a[cnt].w=1; a[cnt].t=cnt;a[cnt].id=++ID; ina[++cnt].flag=2; a[cnt].x=x-1; a[cnt].y=v; thea[cnt].w=-1; a[cnt].t=cnt;a[cnt].id=ID; thea[++cnt].flag=2; a[cnt].x=c;a[cnt].y=y-1; Abouta[cnt].w=-1; a[cnt].t=cnt;a[cnt].id=ID; thea[++cnt].flag=2; a[cnt].x=c;a[cnt].y=v; thea[cnt].w=1; a[cnt].t=cnt;a[cnt].id=ID; the } + } -Sort (A +1, a+cnt+1, CMP); theSolve1, CNT);Bayi for(i=1; i<=id;i++){ theprintf"%d\n", Ans[i]); the } - return 0; -}
Bzoj2683 simple question [CDQ]