See you~(二维树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1892

See you~

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


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
 

 

Source
题意:标准的二维梳妆数组
这个题,为了让输入和输出更快一点,因为要输入输出的数据量很大所以可以使用输入输出外挂 。开始赋值的时候因为每个格子都是1,而树状数组(x,y)表示的又是从(lowbit(x),lowbit(y))到(x,y)的面积内的书,所以可以用求面积的方法初始化树状数组
下面是代码:
  1 #include<cstdio>
  2 #include <iostream>
  3 #include<cstring>
  4 #include<algorithm>
  5 
  6 using namespace std;
  7 #define N 1010
  8 inline int lowbit(int i)
  9 {
 10     return i&(-i);
 11 }
 12 
 13 int mp[N][N];
 14 inline int add(int x , int y , int a)
 15 {
 16     for(int i = x ; i < N ;i+=lowbit(i))
 17         for(int j = y ; j < N ;j+=lowbit(j))
 18             mp[i][j]+=a;
 19 }
 20 inline int sum (int x , int y)
 21 {
 22     int ans = 0 ;
 23     for(int i = x ; i>0 ; i-=lowbit(i))
 24         for(int j = y ; j>0 ; j-=lowbit(j))
 25             ans+=mp[i][j];
 26     return ans;
 27 }
 28 inline int Sum(int x1,int y1,int x2,int y2)
 29 {
 30     int ans = sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1);
 31     return ans;
 32 }//写成内敛函数会更快一点
 33 inline int in_int()//输入外挂读入一个数字
 34 {
 35     char ch;
 36     int res = 0;
 37     while(ch = getchar()) if(ch >= '0' && ch <= '9') break;
 38     while(ch >= '0' && ch <= '9') 
 39     {
 40         res = res*10+ch-'0';
 41         ch = getchar();
 42     }
 43     return res;
 44 }
 45 
 46 inline char in_ch()//输入外挂读入一个字符
 47 {
 48     char res = getchar();
 49     while(res < 'A' || res > 'Z') res = getchar();
 50     return res;
 51 }
 52 
 53 void out(int x)//递归的输出外挂其实并不会快多少
 54 {
 55     if(x > 9) out(x/10);
 56     putchar(x%10+'0');
 57 }
 58 int iniAry[N][N];
 59 
 60 int main()
 61 {
 62     for(int i = 1; i < N; i++)
 63        for(int j = 1; j < N; j++)
 64                iniAry[i][j] = lowbit(i)*lowbit(j); //因为每次的初始化都是一样的,所以可以只算一次,然后memcpy()给mp;
 65     int T;
 66     T = in_int();
 67     for(int cas = 1; cas <= T; cas++)
 68     {
 69         printf("Case %d:\n", cas);
 70         memcpy(mp, iniAry, sizeof(iniAry));
 71         int n;
 72         n = in_int();
 73         char ml;
 74         for(int i = 0 ;i < n; i++)
 75         {
 76             ml = in_ch();
 77             if(ml=='S')
 78             {
 79                 int x1, x2,y1,y2;
 80                 x1 = in_int();
 81                 y1 = in_int();
 82                 x2 = in_int();
 83                 y2 = in_int();
 84                 x1++, y1++, x2++, y2++;
 85                 if(x1 > x2) swap(x1, x2);
 86                 if(y1 > y2) swap(y1, y2);
 87                 int sum = Sum(x1,y1,x2,y2);
 88                 out(sum);
 89                 puts("");
 90             }
 91             else if(ml == 'A')
 92             {
 93                 int x , y, a;
 94                 x = in_int();
 95                 y = in_int();
 96                 x++, y++;
 97                 a = 1*in_int();
 98                 add(x,y,a);
 99             }
100             else if(ml == 'D')
101             {
102                 int x, y, m;
103                 x = in_int();
104                 y = in_int();
105                 x++, y++;
106                 m = in_int();
107                 int tm = Sum(x, y, x, y);
108                 if(tm < m) m = tm;
109                 if(m > 0) add(x, y, -m);
110             }
111             else if(ml =='M')
112             {
113                 int x1 , x2, y1, y2, m;
114                 x1 = in_int();
115                 y1 = in_int();
116                 x2 = in_int();
117                 y2 = in_int();
118                 x1++, y1++, x2++, y2++;
119                 m = in_int();
120                 int tm = Sum(x1, y1, x1, y1);
121                 if(tm < m) m = tm; 
122                 if(m > 0)
123                 {
124                     add(x1,y1,-m);
125                     add(x2,y2,m);    
126                 }
127             }
128         }
129     }
130     return 0;
131 }

 

posted on 2015-08-10 11:25  若流芳千古  阅读(242)  评论(0编辑  收藏  举报

导航