It tells you that the initial value of a matrix is 0 and there are two operations. 1. It tells you X1, Y1, X2, y2 reverses all values in the rectangle from the upper left corner to the lower right corner. 1 is changed to 0, 0 is changed to 1.
The second operation is to query the value of a vertex as 0 or 1.
Analysis: the two-dimensional label method only needs to mark the four corners of each matrix. The basic idea is the same as that of the tree in front of science and technology.
For example:
For example, tell you the lower right corner of the upper left corner (x1, Y1) (X2, Y2)
For this rectangle, we only need to mark (x1, Y1), (X1 + 1, Y2), (X2 + 1, Y1), (X2 + 1, y2 + 1)
For each query, the value of this position is the sum of all the numbers in the matrix from this position to (1, 1 ).
Code:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 1005; 7 8 int lowbit(int x) { 9 return x & ( - x );10 }11 12 int c[maxn][maxn];13 int n;14 15 void add(int i, int j, int value) {16 int xx = j;17 while(i <= n) {18 j = xx;19 while(j <= n) {20 c[i][j] += value;21 j += lowbit(j);22 }23 i += lowbit(i);24 }25 }26 27 int Sum(int i, int j) {28 int xx = j;29 int sum = 0;30 while(i > 0) {31 j = xx;32 while(j > 0) {33 sum += c[i][j];34 j -= lowbit(j);35 }36 i -= lowbit(i);37 }38 return sum;39 }40 41 int main() {42 int t;43 int m;44 char cc;45 int x1, y1, x2, y2;46 bool flag = false;47 scanf("%d",&t);48 while(t--) {49 if(flag) puts("");50 flag = true;51 scanf("%d %d",&n, &m);52 memset(c, 0, sizeof(c));53 for(int i = 1; i <= m; i++) {54 scanf("\n%c %d %d", &cc, &x1, &y1);55 if(cc == ‘C‘) {56 scanf("%d %d",&x2, &y2);57 add(x1, y1, 1);58 add(x1, y2 + 1, 1);59 add(x2 + 1, y1, 1);60 add(x2 + 1, y2 + 1, 1);61 } else {62 printf("%d\n", Sum(x1, y1) % 2 );63 }64 }65 }66 return 0;67 }68 View code
Poj2155matrix [Two-tree array]