描述
Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. 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 into our training room, a lot of books are in my eyes. And
every time the books are moving from one place to another one. Now give
you the position of the books at the early of the day. And the moving
information of the books the day, your work is to tell me how many books
are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a
book can only stayed in one grid. The length and the width of the room
are 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.
输入
In
the first line of the input file there is an Integer T(1<=T<=10),
which means the number of test cases in the input file. Then N test
cases are followed.
For each test case, in the first line there is an Integer
Q(1<Q<=100,000), means the queries of the case. Then followed by Q
queries.
There are 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 the two 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 that position, move away all of them.
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 that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
输出
At the beginning of each case, output "Case X:" where X is the index of the test case, then followed by the "S" queries.
For each "S" query, just print out the total number of books in that area.
样例输入
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
样例输出
Case 1:
1
3
Case 2:
1
4
题意
1000*1000的矩阵上每个格点放了1本书
Q个操作
1.查询[X1,Y1]-[X2,Y2]总共放了几本书
2.在[X1,Y1]增加n1本书
3.在[X1,Y1]移除n1本书,若不够则全移走
4.把[X1,Y1]上的n1本书移到[X2,Y2]上,若不够则全移到[X2,Y2]
题解
二维树状数组单点修改,区间查询
1.区间查询分成4块,([1,1]-[X1,Y1])+([1,1]-[X2,X2])-([1,1]-[X2+1,Y1])-([1,1]-[X1,Y2+1])
2.直接单点更新
3.区间查询单点,这里X2=X1,Y2=Y1,查出来的与n1比个小即为需要移走的数
4.同三
这里操作1,并没有严格[X1,Y1]<[X2,Y2],所以需要交换
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int N=1234; 5 const int n=1005; 6 7 struct BIT2{ 8 int sum[N][N]; 9 void init() 10 { 11 memset(sum,0,sizeof sum); 12 for(int i=0;i<=n;i++) 13 for(int j=0;j<=n;j++) 14 update(i,j,1); 15 } 16 int lowbit(int x){return x&(-x);} 17 int update(int x,int y,int w) 18 { 19 x++,y++; 20 for(int i=x;i<=n;i+=lowbit(i)) 21 for(int j=y;j<=n;j+=lowbit(j)) 22 sum[i][j]+=w; 23 } 24 int query(int x,int y) 25 { 26 x++,y++; 27 int ans=0; 28 for(int i=x;i>0;i-=lowbit(i)) 29 for(int j=y;j>0;j-=lowbit(j)) 30 ans+=sum[i][j]; 31 return ans; 32 } 33 }T; 34 int main() 35 { 36 int t,q,x1,y1,x2,y2,n1,o=1; 37 char op[3]; 38 scanf("%d",&t); 39 while(t--) 40 { 41 printf("Case %d:\n",o++); 42 T.init(); 43 scanf("%d",&q); 44 for(int i=0;i<q;i++) 45 { 46 scanf("%s",op); 47 if(op[0]==‘S‘) 48 { 49 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 50 if(x1>x2)swap(x1,x2); 51 if(y1>y2)swap(y1,y2); 52 int ans=T.query(x2,y2)+T.query(x1-1,y1-1)-T.query(x2,y1-1)-T.query(x1-1,y2); 53 printf("%d\n",ans); 54 } 55 if(op[0]==‘A‘) 56 { 57 scanf("%d%d%d",&x1,&y1,&n1); 58 T.update(x1,y1,n1); 59 } 60 if(op[0]==‘D‘) 61 { 62 scanf("%d%d%d",&x1,&y1,&n1); 63 int ans=T.query(x1,y1)+T.query(x1-1,y1-1)-T.query(x1,y1-1)-T.query(x1-1,y1); 64 T.update(x1,y1,-min(n1,ans)); 65 } 66 if(op[0]==‘M‘) 67 { 68 scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1); 69 int ans=T.query(x1,y1)+T.query(x1-1,y1-1)-T.query(x1,y1-1)-T.query(x1-1,y1); 70 T.update(x1,y1,-min(n1,ans)); 71 T.update(x2,y2,min(n1,ans)); 72 } 73 } 74 } 75 return 0; 76 }
原文地址:https://www.cnblogs.com/taozi1115402474/p/9496025.html