2018 Four school Joint week race-second game B. Xor and problem (two-dimensional tree array)

Source: Internet
Author: User

Xor and Problem timelimit:1000ms memorylimit:256mb64-bit integer IO format:%i64dProblem Description

There is now a matrix of n rows n columns. In the initial state, all values in the matrix are 0. The line encoding is from 1 to N, and the column encoding is also from 1 to N.

aI,?J. is the number in column J of line I. Sub-matrix (x0,y0,x1,y1) is byaI,?J. (x0?≤?I≤?x1,y0?≤?J≤?y1) Composition of the Matrix.
Now you need to do the following two actions:
1. Enquiry(x0,? y 0,? x 1,? y 1): Output sub-matrix ( x 0,? Y 0,? X 1,? Y 1) xor.
       2. Update ( x 0, y 0, x 1, y 1, v ): x 0,? Y 0,? X 1,? Each number in the Y 1) is different or previous v

Input

The first line contains two numbers: N (1<=n<=1000), M (1<=m<=100000) m is the number of times the operation is required.

For the next M-line:
If the first number of the row is 1, indicating the query operation, followed byx0,y 0, x1, y1.
If the first number of the row is 2, indicating the update operation, followed by x0, y0, x1, y 1, v.
Data guarantee:1?≤? x0?≤? x1?≤? n, 1?≤? Y0?≤? Y1?≤? n ,0?≤? V? <?262

Output

For each query operation, the output corresponds to a single line of results.

Sampleinput
2 3 2 1 1 2 2 12 1 1 1 1 21 1 1 2 2
Sampleoutput
2 Note: After the first operation get 1 1 1 1 after the second operation to get 3 1 1 1 ask: 3xor1xor1xor1=2


Analysis: First of all, the subject is the interval update, and the operation of the interval query.

According to the nature of XOR, an odd number of a is different or equal to a, an even number a dissimilarity or equal to 0,

If the(x0,? Y0,? x1,? Y1) Interval XOR or a V, then the effect of the subsequent interval xor or SUM, is nothing more than two, equal to the pre-update XOR,
Either on the original XOR or on the basis of an XOR or a v. so you can consider the impact directly on the single point, and then turn into a single point of update.

If the operation is performed in a one-dimensional interval, for example, there are now arrays B1,b2,b3,b4,b5....bn
For the interval [2,n] xor the previous V, then query the interval [1,m].

If M is an odd number, which is different from the parity of the left endpoint at the time of update,
In the different or process, will continue to cancel each other, and finally for the beginning of the B1^B2^B3. ^BM.
If M is even, that is, the parity of the left endpoint at the time of the update, the above results will be different or upper V;

So when processing, it can be used as a single point of update, you can put the left endpoint B2 into two states,
The state under M is odd query and the state under M is even query, respectively B2 and b2^v;

Similarly, if an update operation is performed on the interval [A, a,], it is equivalent to doing two operations, making [A,n] XOR or once, and then [B+1,n] Xor
If the query operation is performed on the interval [a, b], the equivalent of [1,b]^[1,a-1]

Two-dimensional case, is the same reason, just to distinguish the parity of two coordinates, so used to 4 states to store.
Now the solution becomes a single-point update and interval query, you can directly use a two-dimensional tree array.
This moves the interval update to a single-point update, similar to the interval operation of the differential tree array for addition and subtraction.

The code is as follows:
#include <cstdio>#include<iostream>#include<algorithm>using namespaceStd;typedefLong LongLL;Const intmaxn=1010; LL c[2][2][MAXN][MAXN];intn,m; LL Val;intLowbit (intx) { returnx&-x;}
voidAddintAintb) { for(inti=a;i<=n;i+=lowbit (i)) for(intj=b;j<=n;j+=Lowbit (j)) C[a&1][b&1][i][j]^=Val;} LL sum (intAintb) { intaa=a&1,bb=b&1; LL ans=0; for(inti=a;i>=1; i-=lowbit (i)) for(intj=b;j>=1; j-=Lowbit (j)) {ans^=c[a&1][b&1][i][j]; } returnans;}
intMain () {scanf ("%d%d",&n,&m); intX1,x2,y1,y2,op; LL ans;
for(intI=1; i<=m;i++) {scanf ("%d",&op);
if(op==1) {scanf ("%d%d%d%d",&x1,&y1,&x2,&y2); Ans=sum (x2,y2) ^sum (x2,y1-1) ^sum (x1-1, y2) ^sum (x1-1, y1-1); printf ("%i64d\n", ans); }
Else{scanf ("%d%d%d%d%i64d",&x1,&y1,&x2,&y2,&val); Add (X1,y1), add (X2+1, y1), add (x1,y2+1), Add (x2+1, y2+1); }
} return 0;}



2018 Four school Joint week race-second game B. Xor and problem (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.