题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892
See you~
Problem Description
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.
Input
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.
Output
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.
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
1 /*AC代码*/ 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std; 7 8 const int size = 1010; 9 int a[size+1][size+1]; 10 11 int lowbit(int i) 12 { 13 return i & (-i); 14 } 15 16 void add(int x, int y, int num) 17 { 18 for(int i = x; i <= size; i += lowbit(i)) 19 for(int j = y; j <= size; j += lowbit(j)) 20 a[i][j] += num; 21 } 22 23 int getSum(int x, int y) 24 { 25 int tot = 0; 26 for(int i = x; i > 0; i -= lowbit(i)) 27 for(int j = y; j > 0; j -= lowbit(j)) 28 tot += a[i][j]; 29 return tot; 30 } 31 32 void init() 33 { 34 memset(a, 0, sizeof(a)); 35 for(int i = 1; i < size; ++i) 36 for(int j = 1; j < size; ++j) 37 add(i, j, 1); 38 } 39 40 41 42 int main() 43 { 44 int T, n, x1, x2, y1, y2, n1; 45 char cmd[3]; 46 scanf("%d", &T); 47 for(int cnt = 1; cnt <= T; ++cnt) 48 { 49 init(); 50 scanf("%d", &n); 51 printf("Case %d:\n", cnt); 52 while(n--) 53 { 54 scanf("%s", cmd); 55 if(cmd[0] == ‘A‘) 56 { 57 scanf("%d%d%d", &x1, &y1, &n1); 58 add(x1+1, y1+1, n1); 59 } 60 else if(cmd[0] == ‘S‘) 61 { 62 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 63 ++x1; 64 ++x2; 65 ++y1; 66 ++y2; 67 if(x1 > x2) 68 swap(x1, x2); 69 if(y1 > y2) 70 swap(y1, y2); 71 int temp; 72 temp=getSum(x2,y2)-getSum(x1-1,y2)-getSum(x2,y1-1)+getSum(x1-1,y1-1); 73 printf("%d\n",temp); 74 } 75 else if(cmd[0] == ‘D‘) 76 { 77 scanf("%d%d%d",&x1,&y1,&n1); 78 ++x1; 79 ++y1; 80 int temp; 81 temp=getSum(x1,y1)-getSum(x1-1,y1)-getSum(x1,y1-1)+getSum(x1-1,y1-1); 82 if(n1 > temp) 83 n1 = temp; 84 add(x1, y1, -n1); 85 } 86 else if(cmd[0] == ‘M‘) 87 { 88 scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n1); 89 ++x1; 90 ++x2; 91 ++y1; 92 ++y2; 93 int temp; 94 temp=getSum(x1,y1)-getSum(x1-1,y1)-getSum(x1,y1-1)+getSum(x1-1,y1-1); 95 if(n1 > temp) 96 n1 = temp; 97 add(x1, y1, -n1); 98 add(x2, y2, n1); 99 } 100 } 101 } 102 return 0; 103 }
HDU 1892 See you~ (二维树状数组),布布扣,bubuko.com