HDU1892二维树状数组

See you~

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4729    Accepted Submission(s): 1515


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
 
Author
Sempr|CrazyBird|hust07p43
 题意:
二维坐标平面内,在x,y>=0的点中,每一个点刚开始都有一本书,有4种操作,S求x1,y1,x2,y2,两个点的横纵坐标围成的矩形内有多少书。
代码:
 1 /*由于x,y从0开始,所以把每一个x,y加一,否则会陷入死循环*/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 int t,q;
 7 int A[1005][1005];
 8 int lowbit(int x)
 9 {
10     return x&(-x);
11 }
12 void add(int x,int y,int c)
13 {
14     for(int i=x;i<=1002;i+=lowbit(i))
15     {
16         for(int j=y;j<=1002;j+=lowbit(j))
17         A[i][j]+=c;
18     }
19 }
20 int sum(int x,int y)
21 {
22     int s=0;
23     for(int i=x;i>0;i-=lowbit(i))
24     {
25         for(int j=y;j>0;j-=lowbit(j))
26         s+=A[i][j];
27     }
28     return s;
29 }
30 int ans(int x1,int y1,int x2,int y2)
31 {
32     return (sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1));
33 }
34 int main()
35 {
36     int x1,y1,x2,y2,n1;
37     char ch;
38     scanf("%d",&t);
39     for(int k=1;k<=t;k++)
40     {
41         printf("Case %d:
",k);
42         memset(A,0,sizeof(A));
43         scanf("%d",&q);
44         for(int i=1;i<1003;i++)
45         for(int j=1;j<1003;j++)
46         add(i,j,1);
47         while(q--)
48         {
49             cin>>ch;
50             if(ch=='S')
51             {
52                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
53                 if(x1>x2)     //交换,求的是一个矩形区域所以交换后结果不会变
54                 {
55                     x1=x1^x2;
56                     x2=x1^x2;
57                     x1=x1^x2;
58                 }
59                 if(y1>y2)
60                 {
61                     y1=y1^y2;
62                     y2=y1^y2;
63                     y1=y1^y2;
64                 }
65                 printf("%d
",ans(x1+1,y1+1,x2+1,y2+1));
66             }
67             else if(ch=='A')
68             {
69                 scanf("%d%d%d",&x1,&y1,&n1);
70                 add(x1+1,y1+1,n1);
71             }
72             else if(ch=='D')
73             {
74                 scanf("%d%d%d",&x1,&y1,&n1);
75                 int num=ans(x1+1,y1+1,x1+1,y1+1);
76                 add(x1+1,y1+1,-(num>n1?n1:num));
77             }
78             else
79             {
80                 scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
81                 int num=ans(x1+1,y1+1,x1+1,y1+1);
82                 add(x1+1,y1+1,-(num>n1?n1:num));
83                 add(x2+1,y2+1,num>n1?n1:num);
84             }
85         }
86     }
87     return 0;
88 }
原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5890205.html