HDU 3584 Cube

三维的树状数组:对单点更新,跟二维的一样:

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[124][124][124];

int lowbit( int x )

{

    return x & (-x);    

}

void modify( int x, int y , int z ,int n )

{

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

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

            for( int k = z ; k <= n ; k += lowbit(k) )

                 c[i][j][k] ++;

}

int Sum( int x , int y , int z ,int n )

{

    int sum = 0;

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

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

             for( int k = z ; k >0 ; k -= lowbit( k ) )

                  sum += c[i][j][k];

    return  sum;    

}

int main(  )

{

    int opt,n,m,x1,x2,y1,y2,z1,z2;

    while( scanf( "%d %d",&n,&m )==2 )

    {

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

        for( int i = 0 ; i < m ; i++ )

        {

             scanf( "%d",&opt );

             if( opt ==1 )

             {

                 scanf( "%d %d %d",&x1,&y1,&z1 );

                 scanf( "%d %d %d",&x2,&y2,&z2 );

                 modify( x1 , y1 ,z1 ,n );

                 modify( x1 , y1, z2 + 1 , n );

                 modify( x1 , y2+1,z1 ,n );

                 modify( x2 + 1, y1 ,z1 ,n);

                 modify( x1 , y2 + 1 , z2 + 1,n );

                 modify( x2 +1 , y2 + 1 , z1,n );

                 modify( x2 +1 , y1 , z2 + 1,n );

                 modify( x2 + 1, y2 + 1, z2 +1,n );        

             }    

             else 

             {

                    scanf( "%d %d %d",&x1,&y1,&z1 );

                    printf( "%d\n",Sum( x1, y1 ,z1 ,n )%2 );

             }

        }     

    }

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(cube)