POJ 2155 Matrix "Two-dimensional tree array +yy (interval update, single point query)"

Source: Internet
Author: User
Tags x2 y2

Title Link: http://poj.org/problem?id=2155

Matrix
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 32950 Accepted: 11943
Descriptiongiven 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 whose 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]. Inputthe 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. Outputfor 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

Source

POJ Monthly,lou Tiancheng

Test Instructions Summary:

There is a two-dimensional matrix of n*n with an initial value of 0, which has a T-operation, and each operation has one choice:

C: Modify the values of the matrix (x1, y1) to the upper-left corner (x2, y2) to the lower-right corner (0 and 1 interchange)

Q: The value of the query (x, y) is 0 or 1;

Problem Solving Ideas:

Involves multiple interval modification and interval query priority consideration segment tree and tree array, this problem is clever in the use of the prefix of the tree array and the change of the interval modification, one-dimensional need to mark two points and two dimensions need to mark four points, one point for the effect, the other three points to eliminate the effect, Because the tree array maintains the prefix and, for a two-dimensional tree array, the query is the upper-left corner (x, y) and the matrix of the lower-right corner.

The second is that we can use the parity of the number of changes to determine the value of this point is 0/1;

For example: N = 3;c:x1 = 1, y1 = 1, x2 = 2, y2 = 2;

1 1
(x, Y)
1 1

(because we only need to know the parity, so the point outside the matrix plus 1 to eliminate the effect)

AC Code:

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6 #definell long long int;7 #defineINF 0x3f3f3f3f8 using namespacestd;9 Const intMAXN = 1e3+Ten;Ten  One intMMP[MAXN][MAXN]; A intN, T; -  - intLowbit (intx) the { -     returnx& (-x); - } -  + voidAddintXintYintvalue) - { +      for(inti = x; I <= N; i + =lowbit (i)) A      for(intj = y; J <= N; J + =Lowbit (j)) atmmp[i][j]+=value; - } -  - intSumintXinty) - { -     intres =0; in      for(inti = x; i >0; I-=lowbit (i)) -      for(intj = y; J >0; J-=Lowbit (j)) tores+=Mmp[i][j]; +     returnRes; - } the  * voidInit () $ {Panax Notoginseng      for(inti =0; I <= N; i++) -          for(intj =0; J <= N; J + +) theMMP[I][J] =0; + } A  the intMain () + { -     intt_case; $     Charcom[3]; $     intx, y, x1, x2, y1, y2; -scanf"%d", &t_case); -      while(t_case--) the     { -scanf"%d%d", &n, &T);Wuyi init (); the          while(t--) -         { Wuscanf"%s", &com); -             if(com[0] =='C') About             { $scanf"%d%d%d%d", &x1, &y1, &AMP;X2, &y2); -Add (x1, y1,1); -Add (x2+1, Y1,1); -Add (x1, y2+1,1); AAdd (x2+1, y2+1,1); +             } the             Else if(com[0] =='Q') -             { $scanf"%d%d", &x, &y); the                 intres =0; theres =sum (x, y); the                //printf ("Res:%d\n", res); the                 if(res%2) printf ("1\n"); -                 Elseprintf"0\n"); in             } the         } thePuts""); About     } the     return 0; the}
View Code

POJ 2155 Matrix "Two-dimensional tree array +yy (interval update, single point query)"

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.