CodeForces 341DIahub and Xors(二维树状数组)

来源:互联网 发布:plsql导出sql乱码 编辑:程序博客网 时间:2024/05/17 22:52

思路:典型的二维异或树状数组,有一个结论是在x位置放置了一个元素,只会对x+2,x+4等位置产生影响


#include<bits/stdc++.h>using namespace std;#define LL long longconst int maxn = 1005;LL lowbit(LL x){return x&(-x);}LL c[2][2][maxn][maxn];int n,m;void update(int x1,int y1,LL v){    for(int i = x1;i<=maxn;i+=lowbit(i))for(int j = y1;j<=maxn;j+=lowbit(j))c[x1&1][y1&1][i][j]^=v;}LL query(int x1,int y1){LL ans = 0;for(int i=x1;i;i-=lowbit(i))for(int j = y1;j;j-=lowbit(j))ans^=c[x1&1][y1&1][i][j];return ans;}int main(){    scanf("%d%d",&n,&m);for(int i = 1;i<=m;i++){        int op,x1,y1,x2,y2;scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);if(op==1){LL ans = 0;ans^=query(x1-1,y1-1);ans^=query(x1-1,y2);ans^=query(x2,y1-1);ans^=query(x2,y2);            printf("%lld\n",ans);}else{LL v;            scanf("%lld",&v);update(x1,y1,v);update(x1,y2+1,v);update(x2+1,y1,v);update(x2+1,y2+1,v);} }}


Description

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

You are given a matrix a with n rows and n columns. Initially, all values of the matrix are zeros. Both rows and columns are 1-based, that is rows are numbered 1, 2, ..., n and columns are numbered 1, 2, ..., n. Let's denote an element on the i-th row and j-th column asai, j.

We will call a submatrix (x0, y0, x1, y1) such elements ai, j for which two inequalities hold: x0 ≤ i ≤ x1y0 ≤ j ≤ y1.

Write a program to perform two following operations:

  1. Query(x0y0x1y1): print the xor sum of the elements of the submatrix (x0, y0, x1, y1).
  2. Update(x0y0x1y1v): each element from submatrix (x0, y0, x1, y1) gets xor-ed by value v.

Input

The first line contains two integers: n (1 ≤ n ≤ 1000) and m (1 ≤ m ≤ 105). 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 the i-th operation from the input is a query, the first number from i-th line will be 1. It will be followed by four integers x0y0x1,y1. If the i-th operation is an update, the first number from the i-th line will be 2. It will be followed by five integers x0y0x1y1v.

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

Output

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

Sample Input

Input
3 52 1 1 2 2 12 1 3 2 3 22 3 1 3 3 31 2 2 3 31 2 2 3 2
Output
32

Hint

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

1 1 21 1 23 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.



0 0