Describe
now I am leaving Hust ACM. In the past II and half years, I learned so many knowledge on algorithm and programming, and I met so many good friend S. I want to say sorry to Mr, Yin, I must leave now ~~>.<~~. I am very sorry, we could not advanced to the world finals last year.
when coming to our training, a lot of books is in my eyes. And every time the books is moving from one place to another one. Now give your position of the books at the early of the day. And the moving information of the books the day, your work was to tell me how many books was stayed in some rectangles.
To make the problem easier, we divide the same as different grids and a book can only stayed in one grid. The length and the width of the is less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put It on one position.
Input
The first line of the input file there was an Integer T (1<=t<=10), which means the number of test cases in the INP UT file. Then N test cases is followed.
For each test case, in the first line there is a Integer Q (1<q<=100,000), means the queries of the case. Then followed by Q queries.
There is 4 kind of queries, sum, add, delete and move.
For example:
S x1 y1 x2 y2 means you should tell me the total books of the rectangle used (x1,y1)-(x2,y2) as the diagonal, including th e-Points.
A x1 y1 N1 means I put N1 books on the position (X1,Y1)
D x1 y1 N1 means I move away N1 books on the position (x1,y1), if less than N1 books at this position, move away all of th Em.
M x1 y1 x2 y2 n1 means you move N1 books from (x1,y1) to (x2,y2), if less than N1 books at that position, move away all of them.
Make sure in first, there is a book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
Output
At the beginning of all case, output "case x:" where x is the index of the "the" and then followed by the "S" queries.
For each "S" query, just print out the total number of books in the.
Sample input
2
3
S 1 1 1 1
A 1 1 2
S 1 1 1 1
3
S 1 1 1 1
A 1 1 2
S 1 1 1 2
Sample output
Case 1:
1
3
Case 2:
1
4
Test instructions
1000*1000 1 books per lattice point on the matrix.
Q Operations
1. query [X1,y1]-[x2,y2] A total of a few books
2. Add N1 book to [X1,y1]
3. Remove N1 book in [X1,y1], if not enough, remove it completely
4. Move the N1 book on [X1,y1] to [x2,y2], if not enough, move to [X2,y2]
Exercises
Two-dimensional tree array single point modification, interval query
1. Interval query is divided into 4 blocks ([1,1]-[x1,y1]) + ([1,1]-[x2,x2])-([1,1]-[x2+1,y1])-([1,1]-[x1,y2+1])
2. Direct single point update
3. Interval Inquiry single point, here X2=x1,y2=y1, isolated with N1 than a small is required to remove the number
4. The same three
Operation 1 Here, and there is no strict [x1,y1]<[x2,y2], so need to exchange
Code
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intn=1234;5 Const intn=1005;6 7 structbit2{8 intSum[n][n];9 voidInit ()Ten { Onememset (SUM,0,sizeofsum); A for(intI=0; i<=n;i++) - for(intj=0; j<=n;j++) -Update (I,J,1); the } - intLowbit (intx) {returnx& (-x);} - intUpdateintXintYintW) - { +x++,y++; - for(inti=x;i<=n;i+=lowbit (i)) + for(intj=y;j<=n;j+=Lowbit (j)) Asum[i][j]+=W; at } - intQueryintXinty) - { -x++,y++; - intans=0; - for(intI=x;i>0; i-=lowbit (i)) in for(intJ=y;j>0; j-=Lowbit (j)) -ans+=Sum[i][j]; to returnans; + } - }t; the intMain () * { $ intt,q,x1,y1,x2,y2,n1,o=1;Panax Notoginseng Charop[3]; -scanf"%d",&t); the while(t--) + { Aprintf"Case %d:\n", o++); the t.init (); +scanf"%d",&q); - for(intI=0; i<q;i++) $ { $scanf"%s", op); - if(op[0]=='S') - { thescanf"%d%d%d%d",&x1,&y1,&x2,&y2); - if(x1>x2) Swap (X1,X2);Wuyi if(y1>y2) swap (y1,y2); the intAns=t.query (x2,y2) +t.query (x1-1, y1-1)-t.query (x2,y1-1)-t.query (x1-1, y2); -printf"%d\n", ans); Wu } - if(op[0]=='A') About { $scanf"%d%d%d",&x1,&y1,&N1); - t.update (X1,Y1,N1); - } - if(op[0]=='D') A { +scanf"%d%d%d",&x1,&y1,&N1); the intAns=t.query (x1,y1) +t.query (x1-1, y1-1)-t.query (x1,y1-1)-t.query (x1-1, y1); -T.update (x1,y1,-min (N1,ans)); $ } the if(op[0]=='M') the { thescanf"%d%d%d%d%d",&x1,&y1,&x2,&y2,&N1); the intAns=t.query (x1,y1) +t.query (x1-1, y1-1)-t.query (x1,y1-1)-t.query (x1-1, y1); -T.update (x1,y1,-min (N1,ans)); in t.update (X2,y2,min (N1,ans)); the } the } About } the return 0; the}
TOJ 2725 See you~ (Two-dimensional tree array single point update interval query)