标签:poj 2155 martrix 树状数组 二维树状数组
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19113 | Accepted: 7193 |
Description
Input
Output
Sample Input
1 2 10 C 2 1 2 2 Q 2 2 C 2 1 2 1 Q 1 1 C 1 1 2 1 C 1 2 1 2 C 1 1 2 2 Q 1 1 C 1 1 2 1 Q 2 1
Sample Output
1 0 0 1
Source
题意:给你个矩阵里面开始全是0,然后给你两种指令:1:‘C x1,y1,x2,y2’就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0,;2:‘Q x1 y1‘,输出a[x1][y1]的值。
CODE:#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N = 1010; int n, arr[N][N]; int lowbit ( int x ) { return x & ( -x ); } void update ( int i, int j, int val ) { while ( i <= n ) { int tmpj = j; while ( tmpj <= n ) { arr[i][tmpj] += val; tmpj += lowbit ( tmpj ); } i += lowbit ( i ); } } int Sum ( int i, int j ) { int ans = 0; while ( i > 0 ) { int tmpj = j; while ( tmpj > 0 ) { ans += arr[i][tmpj]; tmpj -= lowbit ( tmpj ); } i -= lowbit ( i ); } return ans; } int main() { //freopen("input.txt","r",stdin); int t, q; scanf ( "%d", &t ); char op[3]; while ( t-- ) { scanf ( "%d%d", &n, &q ); memset ( arr, 0, sizeof ( arr ) ); int x1, y1, x2, y2; while ( q-- ) { scanf ( "%s", op ); if ( op[0] == 'C' ) { scanf ( "%d%d%d%d", &x1, &y1, &x2, &y2 ); update ( x2 + 1, y2 + 1, 1 ); update ( x2 + 1, y1, 1 ); update ( x1, y2 + 1, 1 ); update ( x1, y1, 1 ); } else if ( op[0] == 'Q' ) { scanf ( "%d%d", &x1, &y1 ); printf ( "%d\n", Sum ( x1, y1 ) % 2 ); } } if ( t ) printf ( "\n" ); } return 0; }
标签:poj 2155 martrix 树状数组 二维树状数组
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/41555793