poj 2155 Matrix 二维树状数组
题意 就是给你一个 矩形 然后 执行两种操作 1 对任意一个给出的矩形的所有单元格进行取反操作;2 询问某一点是1 还是0;
方法 打死都想不到可以用树状数组来做;而且是二维的树状数组来做(听说这个题目是楼天成出的);首先需要了解翻转;
有一篇博客写得很好 http://blog.csdn.net/zxy_snow/article/details/6264135 ;
二维树状数组; 可以这么理解;如果看作是一维的树状数组,那么他的数组是一维的;二维就是把一维的结果再进行一次树状数组;
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 #define low_bit(x) (x&(-x)) 6 using namespace std; 7 8 int map[1003][1003],N; 9 10 int update( int lt,int top,int w) 11 { 12 while( lt <= N ) 13 { 14 int t = top; 15 while( t <= N ) 16 { 17 map[lt][t] += w; 18 t += low_bit(t); 19 } 20 lt += low_bit(lt); 21 } 22 } 23 24 int sum ( int lt,int top ) 25 { 26 int ans = 0; 27 while( lt > 0 ) 28 { 29 int t = top; 30 while( t > 0 ) 31 { 32 ans += map[lt][t]; 33 t -= low_bit(t); 34 } 35 lt -= low_bit(lt); 36 } 37 if( ans%2 ) return 1; 38 return 0; 39 } 40 41 int main( ) 42 { 43 int i,j,X,T,lt,rt,x,y,top,hid; 44 char str[3]; 45 { 46 scanf("%d%d",&N,&T); 47 for( i = 0; i <= N; i++ ) 48 for( j = 0; j <= N; j++ ) 49 map[i][j] = 0; 50 for( i = 1; i <= T; i++ ) 51 { 52 scanf("%s",&str); 53 if( str[0] == 'C' ) 54 { 55 scanf("%d%d%d%d",<,&top,&rt,&hid); 56 update( lt,top,1 ); 57 update( lt,hid + 1,1 ); 58 update( rt + 1,top,1 ); 59 update( rt + 1,hid + 1,1 ); 60 } 61 else 62 { 63 scanf("%d%d",&x,&y); 64 printf("%d\n",sum(x,y)); 65 } 66 } 67 printf("\n"); 68 } 69 }