poj 2155 Matrix(二维树状数组)
http://poj.org/problem?id=2155
二维树状数组【区间更新,单点查询】,一道简单题!
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 const int maxn = 1005; 8 struct BIT2D { 9 int size; 10 bool val[maxn][maxn]; 11 12 void init() { 13 memset(val, false, sizeof(val)); 14 } 15 16 void addArea(int x, int y) { 17 for (; x > 0; x -= (-x) & x) { 18 for (int j = y; j > 0; j -= (-j) & j) { 19 val[x][j] = !val[x][j]; 20 } 21 } 22 } 23 24 bool getPoint(int x, int y) { 25 bool ans = false; 26 27 for (; x <= size; x += (-x) & x) { 28 for (int j = y; j <= size; j += (-j) & j) { 29 ans ^= val[x][j]; 30 } 31 } 32 33 return ans; 34 } 35 36 void print() { 37 for (int i = 1; i <= size; i++) { 38 for (int j = 1; j <= size; j++) { 39 printf("%d ", val[i][j]); 40 } 41 puts(""); 42 } 43 puts("~~~"); 44 } 45 } bit; 46 47 void deal(int op) { 48 char buf[3]; 49 int x1, x2, y1, y2; 50 51 bit.init(); 52 while (op--) { 53 scanf("%s", buf); 54 if (buf[0] == 'Q') { 55 scanf("%d%d", &x1, &y1); 56 // printf("hell?? %d %d\n", x1, y1); 57 printf("%d\n", bit.getPoint(x1, y1)); 58 } else { 59 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 60 bit.addArea(x1 - 1, y1 - 1); 61 bit.addArea(x1 - 1, y2); 62 bit.addArea(x2, y1 - 1); 63 bit.addArea(x2, y2); 64 } 65 // bit.print(); 66 } 67 puts(""); 68 } 69 70 int main() { 71 int T; 72 73 // freopen("in", "r", stdin); 74 scanf("%d", &T); 75 while (T--) { 76 int op; 77 78 scanf("%d%d", &bit.size, &op); 79 deal(op); 80 } 81 82 return 0; 83 }
——written by Lyon