HDU ACM 1892 See you~(二维树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=1892

 

树状数组拓展到二维

 

  1 #include <iostream>
  2 using namespace std;
  3 const int MAX = 1000 + 10;
  4 int a[MAX][MAX];
  5 int c[MAX][MAX];
  6 int LowBit(int lb){
  7     return lb & (-lb);
  8 }
  9 void Updata(int x,int y,int value){
 10     int i,j;
 11     for(i=x;i<MAX;i+=LowBit(i)){
 12         for(j=y;j<MAX;j+=LowBit(j)){
 13             c[i][j]+=value;
 14         }
 15     }
 16 }
 17 
 18 int CalSum(int x,int y){
 19     int i,j;
 20     int sum = 0;
 21     for(i=x;i>0;i-=LowBit(i)){
 22         for(j=y;j>0;j-=LowBit(j)){
 23             sum += c[i][j];
 24         }
 25     }
 26     return sum;
 27 }
 28 void IsSwap(int &a,int &b){
 29     if(a > b){
 30         swap(a,b);
 31     }
 32 }
 33 int main(){
 34     int T;
 35     int Case = 0;
 36     while(cin>>T){
 37         while(T--){
 38             Case++;
 39             cout<<"Case "<<Case<<":"<<endl;
 40             int n;
 41             cin>>n;
 42             int i,j;
 43             memset(a,0,sizeof(a));
 44             memset(c,0,sizeof(c));
 45             for(i=1;i<MAX;i++){
 46                 for(j=1;j<MAX;j++){
 47                     a[i][j] = 1;
 48                     Updata(i,j,1);
 49                 }
 50             }
 51             char Type;
 52             for(i=0;i<n;i++){
 53 
 54                 cin>>Type;
 55                 int x1,y1;
 56                 int x2,y2;
 57                 int value;
 58                 if(Type == 'S'){//查询    
 59                     cin>>x1>>y1;
 60                     x1++;
 61                     y1++;
 62                     cin>>x2>>y2;
 63                     x2++;
 64                     y2++;
 65                     IsSwap(x1,x2);
 66                     IsSwap(y1,y2);
 67                     cout<<CalSum(x2,y2) - CalSum(x2,y1-1) - CalSum(x1-1,y2) + CalSum(x1-1,y1-1)<<endl;
 68                 }
 69                 if(Type == 'A'){//增加
 70                     cin>>x1>>y1>>value;
 71                     x1++;
 72                     y1++;
 73                     a[x1][y1] += value;
 74                     Updata(x1,y1,value);
 75                 }
 76                 if(Type == 'D'){//删除
 77                     cin>>x1>>y1>>value;
 78                     x1++;
 79                     y1++;
 80                     if(a[x1][y1] < value){
 81                         value = a[x1][y1];
 82                     }
 83                     a[x1][y1] -= value;
 84                     Updata(x1,y1,-value);
 85                 }
 86                 if(Type == 'M'){//移动
 87                     cin>>x1>>y1>>x2>>y2>>value;
 88                     x1++;
 89                     y1++;
 90                     x2++;
 91                     y2++;
 92                     if(a[x1][y1] < value){
 93                         value = a[x1][y1];
 94                     }
 95                     a[x1][y1] -= value;
 96                     a[x2][y2] += value;
 97                     Updata(x1,y1,-value);
 98                     Updata(x2,y2,value);
 99                 }
100             }
101         }
102     }
103     return 0;
104 }

 

posted @ 2012-12-02 16:12  zx雄  阅读(281)  评论(0编辑  收藏  举报