POJ-2155 Matrix (two-dimensional tree array + interval change + single-point evaluation or two-dimensional segment tree + interval update + single-point evaluation)

Source: Internet
Author: User
Tags stdin x2 y2

POJ-2155Matrix
Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %i64d &%i64u

Submit Status

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
The title is to give you a matrix, the most beginning is 0, and then give you a top-left corner of the coordinates, the lower right corner of the coordinates, the area of 0, and 0, and then give you a point to let you find out whether he is the result of 1 or 0.
You can use the interval changes in the tree array and the single point evaluation. Just here is a two-dimensional tree-like array, the principle is the same.
If it changes, it can be so, if it starts [1....x2][1....y2] as an even number (this is considered a prefix and)
 first add [1....x2][1....y2] 1 to an odd number, then add [1...x1-1][y2] 1 to even, add [1....x2][y1] 1 to even, and they overlap [1....x1-1 ][1....X2-1] Part was changed three times. It's still odd, so there's a plus 1. Turn him into an even number, so that except [X1....x2][y1....y2] has changed. The rest of the place is still unchanged. 
/*author:2486memory:4304 kbtime:547 mslanguage:g++result:accepted*/#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int maxn = $ + 5;int c[maxn][maxn];int N, X, M, x, y, x1, x2, y1, y 2;char op[10];int lowbits (int x) {return x & (-X);}            void Add (int x,int y) {for (int i = x; I <= n;i + = lowbits (i)) {for (int j = Y;j <= n;j + = Lowbits (j)) {        C[I][J] + +;    }}}int query (int x,int y) {int ret = 0;        for (int i = x;i > 0;i-= lowbits (i)) {for (int j = y;j > 0;j-= Lowbits (j)) {ret + = C[i][j]; }} return ret;}    int main () {//freopen ("D://imput.txt", "R", stdin);    scanf ("%d", &x);        while (X-) {memset (c, 0, sizeof (c));        scanf ("%d%d", &n, &m);            while (M-) {scanf ("%s", op);                if (op[0] = = ' C ') {scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);                X2 + +;     y2 + +;           Add (x1, y1);                Add (x1, y2);                Add (x2, y1);            Add (x2, y2);                } else{scanf ("%d%d", &x, &y);    printf ("%d\n", query (x, y) & 1);//sum represents the point of order} if (x) printf ("\ n"); } return 0;}


Line segment Tree Code:
/*author:2486memory:63688 kbtime:1579 mslanguage:g++result:accepted*/#include <cstdio> #include <cstring > #include <algorithm> #include <vector> #include <queue>using namespace std; #define Lson RT < < 1, l, Mid#define Rson RT << 1|1, Mid + 1, r#define root 1, 1, nconst int maxn = + 5;int N, T, case, X1, y1  , x2, y2, ret, x, Y;char op[5];int sum[maxn << 2][maxn << 2];void update_r (int cr, int y1, int y2, int rt, int        l, int R) {if (Y1 <= l && r <= y2) {Sum[cr][rt] =!sum[cr][rt];    return;    } int mid = (L + r) >> 1;    if (y1 <= mid) Update_r (CR, Y1, Y2, Lson); if (Y2 > Mid) Update_r (CR, Y1, Y2, Rson);} void Update_c (int x1, int y1, int x2, int y2, int rt, int l, int r) {if (X1 <= l && r <= x2) {upd        Ate_r (RT, y1, Y2, root);    Return    } int mid = (L + r) >> 1;    if (x1 <= mid) Update_c (x1, y1, x2, y2, Lson); if (X2 > Mid) Update_c (x1, y1, x2, y2, Rson);}    void Query_r (int cr, int y1, int y2, int rt, int l, int r) {if (Sum[cr][rt]) ret + +;    if (Y1 <= l && r <= y2) {return;    } int mid = (L + r) >> 1;    if (y1 <= mid) Query_r (CR, Y1, Y2, Lson); if (Y2 > Mid) Query_r (CR, Y1, Y2, Rson);} void Query_c (int x1, int y1, int x2, int y2, int rt, int l, int r) {query_r (rt, y1, Y2, root);//Choose to overwrite the number of subtrees if (x1 &l    t;= l && r <= x2) {return;    } int mid = (L + r) >> 1;    if (x1 <= mid) Query_c (x1, y1, x2, y2, Lson); if (X2 > Mid) Query_c (x1, y1, x2, y2, Rson);}    int main () {//freopen ("D://imput.txt", "R", stdin);    scanf ("%d", &case);        while (case-) {memset (sum, 0, sizeof (sum));        scanf ("%d%d", &n, &t);            while (T--) {scanf ("%s", op);                if (op[0] = = ' C ') {scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);            Update_c (x1, y1, x2, y2, root);        } else {        scanf ("%d%d", &x, &y);                ret = 0;                Query_c (x, y, x, y, root);            printf ("%d\n", ret & 1);    }} if (case) printf ("\ n"); } return 0;}


POJ-2155 Matrix (two-dimensional tree array + interval change + single-point evaluation or two-dimensional segment tree + interval update + single-point evaluation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.