POJ2155 Matrix <树套树/二维树状数组>

Problem

Matrix

Time Limit: 3000MS3000MS
Memory Limit: 65536K65536K

Description

Given an N×NN\times N matrix AA, whose elements are either 00 or 11. A[i,j]A[i, j] means the number in the ithi^{th} row and jthj^{th} column. Initially we have A[i,j]=0(1i,j<N)A[i, j] = 0 (1 \le i, j\le< N).
We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1,y1)(x_1, y_1) and lower-right corner is (x2,y2)(x_2, y_2), we change all the elements in the rectangle by using “not” operation (if it is a ‘00’ then change it into ‘11’ otherwise change it into ‘00’). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.

  1. CC x1x_1 y1y_1 x2x_2 y2y_2 (1x1x2n,1y1y2n1\le x_1\le x_2\le n, 1\le y_1\le y_2\le n) changes the matrix by using the rectangle whose upper-left corner is (x1,y1)(x_1, y_1) and lower-right corner is (x2,y2)(x_2, y_2).
  2. QQ xx yy (1x,yn1\le x, y\le n) querys A[x,y]A[x, y].

Input

The first line of the input is an integer $X (X \le 10) $representing the number of test cases. The following X blocks each represents a test case.
The first line of each block contains two numbers NN and TT (2N10002 \le N \le 1000, 1T500001 \le T \le 50000) representing the size of the matrix and the number of the instructions. The following TT lines each represents an instruction having the format “QQ xx yy” or “CC x1x_1 y1y_1 x2x_2 y2y_2”, which has been described above.

Output

For each querying output one line, which has an integer representing A[x,y]A[x, y].
There is a blank line between every two continuous test cases.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
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
2
3
4
1
0
0
1

标签:线段树套线段树/二维树状数组

Solution

此题最简单的方法是二维树状数组,但因为二维树状数组没太大用,所以练习线段树的树套树。
此题用作树套树的模板题再合适不过。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX_N 1000
using namespace std;
int n, m;
int tr[(MAX_N<<2)+5][(MAX_N<<2)+5];
void modify_y(int v1, int v2, int s, int t, int l, int r) {
if (s >= l && t <= r) {
tr[v1][v2] ^= 1;
return;
}
int mid = s+t>>1;
if (l <= mid) modify_y(v1, v2<<1, s, mid, l, r);
if (r >= mid+1) modify_y(v1, v2<<1|1, mid+1, t, l, r);
}
void modify_x(int v, int s, int t, int x1, int y1, int x2, int y2) {
if (s >= x1 && t <= x2) {
modify_y(v, 1, 1, n, y1, y2);
return;
}
int mid = s+t>>1;
if (x1 <= mid) modify_x(v<<1, s, mid, x1, y1, x2, y2);
if (x2 >= mid+1) modify_x(v<<1|1, mid+1, t, x1, y1, x2, y2);
}
int query_y(int v1, int v2, int s, int t, int pos) {
if (s == t) return tr[v1][v2];
int mid = s+t>>1;
if (pos <= mid) return tr[v1][v2]^query_y(v1, v2<<1, s, mid, pos);
else return tr[v1][v2]^query_y(v1, v2<<1|1, mid+1, t, pos);
}
int query_x(int v, int s, int t, int x, int y) {
if (s == t) return query_y(v, 1, 1, n, y);
int mid = s+t>>1;
if (x <= mid) return query_y(v, 1, 1, n, y)^query_x(v<<1, s, mid, x, y);
else return query_y(v, 1, 1, n, y)^query_x(v<<1|1, mid+1, t, x, y);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(tr, 0, sizeof(tr));
scanf("%d%d", &n, &m);
while (m--) {
char ch;
cin >> ch;
if (ch == 'C') {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
modify_x(1, 1, n, x1, y1, x2, y2);
}
if (ch == 'Q') {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query_x(1, 1, n, x, y));
}
}
printf("\n");
}
return 0;
}