Matrix
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 17766 |
|
Accepted: 6674 |
Description
Given an n*n matrix A, whose elements is either 0 or 1. A[i, j] means the number in the I-th row and j-th column. Initially we have a[i, j] = 0 (1 <= i, J <= N).
We can change the matrix in the following. Given a rectangle whose upper-left corner is (x1, y1) and Lower-right Corner are (x2, y2), we change all the elements in th e Rectangle by using the "not" operation (if it was a ' 0 ' then change it to ' 1 ' otherwise change it into ' 0 '). To maintain the information of the matrix, you is asked to write a program to receive and execute both kinds of instructio Ns.
1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= N, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whos E upper-left Corner is (x1, y1) and Lower-right Corner are (x2, y2).
2. Q x Y (1 <= x, y <= N) querys a[x, y].
Input
The first line of the input was an integer X (x <=) representing the number of test cases. The following X blocks each represents a test case.
The first line of each block contains numbers n and T (2 <= n <=, 1 <= T <= 50000) representing the S Ize of the Matrix and the number of the instructions. The following T lines each represents an instruction have the format "Q x y" or "C x1 y1 x2 y2", which has been describe D above.
Output
For each querying output one line, which have an integer representing A[x, y].
There is a blank line between every the continuous test cases.
Sample Input
10C 2 1 2 2Q 2 2C 2 1 2 1Q 1 1C 1 1 2 1C 1 2 1 2C 1 1 2 2Q 1 1C 1 1 2 1Q 2 1
Sample Output
1001
Test instructions: For a given size andinitialized to 0The matrix. To run some commands, Q A B is the value of the view Arr[a][b] element, and C X1 Y1 X2 Y2 flips All points 0, 1 in the range of (x1, y1) (x2, y2) rectangles. The following:How to use the tree array pattern two. Segment update, point query. Update (x2, y2) indicates that all points from (1, 1) to (x2, y2) are flipped once, but this will flip some of the points out of the given range, so they need to be flipped back.
#include <stdio.h> #include <string.h> #define MAXN 1002int size, tree[maxn][maxn];int lowbit (int x) {return x & (-X); }//downward update indicates a[1] ... A[i] Each element must be + = Val, extended to the two-dimensional same-like void update (int x, int y, int val) {int temp;while (x > 0) {temp = y;while (Temp > 0) {tree[x][ Temp] + = val;temp-= lowbit (temp);} X-= Lowbit (x);}} int query (int x, int y) {int sum = 0, temp;while (x <= size) {temp = Y;while (temp <= size) {sum + = tree[x][temp];temp + = Lowbit (temp);} x + = Lowbit (x);} return sum;} int main () {//freopen ("Stdin.txt", "R", stdin); int CAs, q, A, B, C, D;char com[2];scanf ("%d", &cas), while (cas--) {scanf ("%d%d", &size, &q) memset (tree, 0, sizeof), while (q--) {scanf ("%s%d%d", com, &a, &b); if (com[0] = = ' C ') {scanf ("%d%d", &c, &d), update (c, b-1,-1), Update (A-1, D,-1), Update (A-1, b-1, 1), update (c, D, 1);} else printf ("%d\n", query (A, b) & 1);} if (CAS) printf ("\ n");} return 0;}
POJ2155 Matrix "Two-dimensional tree array" + "segment Update point query"