HDU 1892 See you~

一道二维的树状数组题:这里要注意初始化,

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

#include<string>

#define LL long long

using namespace std;

int c[1024][1024];

int lowbit( int x )

{

    return x & ( -x );

}

void modify( int x, int y , int A  )

{

    for( int i = x ; i <= 1001 ; i += lowbit( i ) )

         for( int j = y ; j <= 1001 ; j += lowbit( j ) )

              c[i][j] += A;    

}

int Sum( int x, int y  )

{

   int sum = 0;

   for( int i = x ; i > 0 ; i -= lowbit( i ) )

        for( int j = y ; j > 0 ; j -= lowbit( j ) )

             sum += c[i][j];

   return sum;    

}

void Init(  )

{

   memset( c , 0 , sizeof( c ) );

   for( int i = 1 ; i <= 1001 ; i ++ )

        for( int j = 1 ; j<= 1001 ; j ++ )

            c[i][j] = lowbit(i)*lowbit(j);

}

int main(  )

{

    int T,x1,y1,x2,y2,n,A,sum;     

    char str[3];

    while( scanf( "%d",&T )==1 )

    {

         for( int i = 1 ; i <=T ;i ++ )

         {              

              Init(  );

              scanf( "%d",&n );

              printf( "Case %d:\n",i );

              for( int j = 0 ; j < n ; j ++ )

              {

                   scanf( "%s",str );

                   switch( str[0] )

                   {

                     case 'S': scanf( "%d %d %d %d",&x1,&y1,&x2,&y2 );

                               if( x1 > x2 ) swap( x1 ,x2 ); if( y1 > y2 ) swap( y1 ,y2 );

                               sum = Sum(x2+1,y2+1)+Sum(x1,y1)-Sum(x1,y2+1)-Sum(x2+1,y1);

                                 printf( "%d\n",sum );

                                 break;

                     case 'A': scanf( "%d %d %d",&x1,&y1 ,&A );

                                modify( x1+1 ,y1+1 ,A  ); break;

                     case 'D': scanf( "%d %d %d",&x1,&y1,&A );

                                sum = Sum(x1+1,y1+1)+Sum(x1,y1)-Sum(x1,y1+1)-Sum(x1+1,y1 );

                                A = min( sum ,A );

                                modify( x1+1 , y1+1, -A ); break;

                     case 'M':  scanf( "%d %d %d %d %d",&x1,&y1,&x2,&y2,&A );

                                sum = Sum(x1+1,y1+1)+Sum(x1,y1)-Sum(x1,y1+1)-Sum(x1+1,y1);

                                A = min( sum ,A );

                                modify( x1 + 1, y1 + 1, -A );

                                modify( x2 +1 , y2 + 1, A ); break;

                   }        

              }        

         }    

    }

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(HDU)