Codeforces Round #198 (Div. 1) D. Iahub and xors Two-dimensional tree array *

Source: Internet
Author: User

D. Iahub and XORs

Iahub does not like background stories and so he'll tell you exactly what's this problem asks your for.

You are given a matrix  a  with  n  rows and  n  columns. Initially, all values of the matrix is zeros. Both rows and columns are 1-based, which is rows are numbered 1, 2, ...,   N  and Co Lumns is numbered 1, 2, ...,   n . Let ' s denote a element on The  i -th row and  J -th column As  a i , J .

We'll call a Submatrix (x0, y0, x1, y1) Such elements Ai, J for which II inequalities hold: x0≤ ix 1, y0≤ jy1.

Write a program to perform, following operations:

  1. Query ( x 0,  y 0,  x 1,  y 1): Print the XOR sum of the elements of the Submatrix  ( x 0, y 0, x 1, y 1).
  2. Update (x0, y0, x1, y1, v): Each element from Submatrix (x0, y0, x1, y1) gets Xor-ed by Value v.
Input

The first line contains integers: n (1≤ n ≤1000) and m (1≤ m ≤ 5). The number m represents the number of operations you need to perform. Each of the next m lines contains five or six integers, depending on operation type.

If theI-th operation from the input was a query, the first number from i -th line would be 1. It'll be followed by four Integers  x 0,  y 0,  x 1,  y 1. If the i -th operation is a update, the first number from the  i -th line would be 2. It'll be followed by five Integers  x 0,  y 0,  x 1,  y 1,  v .

It is guaranteed this for each update operation, the following inequality holds: 0≤ v < 262. It is guaranteed, and the operation, the following inequalities hold: 1≤ x0≤ x1≤ n, 1≤ y0≤ y1≤ n.

Output

For each query operation, output to a new line the result.

Examples input
3 5
2 1 1 2 2 1
2 1 3 2 3 2
2 3 1 3 3 3
1 2 2) 3 3
1 2 2) 3 2
Output
3
2
Note

After the first 3 operations, the matrix would look like this:


1 1 2
1 1 2
3 3 3

The fourth operation asks us to compute 1 XOR 2 XOR 3 XOR 3 = 3.

The fifth operation asks us to compute 1 XOR 3 = 2.

Test instructions

Give you a nxn matrix; all positions have a value of 0 at the beginning;

M operations: Two operations, one is the value of a rectangular region is different or V, the other is to find a rectangular region of the XOR.

Exercises

To maintain (x, y) XOR, the XOR of the rectangular region can be calculated according to the xor of the four locations. Because the XOR operation is special, in an area of the XOR or operation, then the region of a point (x, y) of the XOR and or should be different or up V, or 0. In this way, we can use a two-dimensional tree array, only a single-point update to complete the update of the rectangular region of the operation, in addition, because the region is updated with parity, so it is based on parity to maintain 4 cases of tree array.

#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF = 1e18+1LL;Const DoublePi = ACOs (-1.0);Const intN = 1e3+Ten, M = 1e5+ -, mod = 1e9+7, INF =2e9;intN,m,mp[n][n]; LL c[4][n][n];int Get(intXinty) {intres =0; if(x&1) Res + =1; if(y&1) Res + =2; returnRes;} LL Ask (intXinty) {LL s=0; intWH =Get(x, y);  for(inti = x; I i-= I & (-i)) for(intj = y; J J-= J & (-J)) s^=C[wh][i][j]; returns;}voidUpdateintXintY,ll v) {        intWH =Get(x, y);  for(inti = x; i < N; i+= i& (-i)) for(intj = y; J < N; J + = j& (-j)) C[wh][i][j] ^=v;}intMain () {scanf ("%d%d",&n,&m);  for(inti =1; I <= m; ++i) {intOp,x1,x2,y1,y2;        LL v; scanf ("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2); if(OP = =1) {LL a=Ask (X2,y2); LL b=0, C =0, d =0; if(Y1 >1) b = Ask (x2,y1-1); if(X1 >1) C = Ask (x1-1, y2); if(X1 >1&& y1 >1) d = Ask (x1-1, y1-1); printf ("%i64d\n", a^b^d^c); } Else{scanf ("%i64d",&v); Update (x2+1, y2+1, V); Update (x2+1, y1,v); Update (X1,y2+1, V);        Update (X1,Y1,V); }    }    return 0;}

Codeforces Round #198 (Div. 1) D. Iahub and xors Two-dimensional tree array *

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.