1. 程式人生 > >Codeforces 341D Iahub and Xors (二維樹狀陣列 差分 推薦)

Codeforces 341D Iahub and Xors (二維樹狀陣列 差分 推薦)

D. Iahub and Xors

time limit per test 1 second

memory limit per test 256 megabytes

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 as ai, j.

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

Write a program to perform two following operations:

  1. Query(x0, y0, x1, y1): print the xor sum of the elements of the submatrix (x0, y0, x1, y1).
  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 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 x0, y0, x1, 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 x0, y0, x1, y1, v.

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 ≤ n, 1 ≤ y0 ≤ y1 ≤ n.

Output

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

Examples

input

Copy

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

Copy

3
2

Note

After the first 3 operations, the matrix will 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.

題目連結:http://codeforces.com/contest/341/problem/D

題目大意:給一個n*n的矩陣,初始值全為0,有兩種操作,1表示查詢以(x0, y0)和(x1, y1)為對頂點的矩陣中所有值的異或和,2表示將以(x0, y0)和(x1, y1)為對頂點的矩陣中的所有值都異或v

題目分析:先考慮一維的情況,讓點i維護[1,i]的字首異或和,對於(l, r)這段區間

1)更新:通過後綴異或和的方式,更新l和r+1兩個點  =》update(l, v);update(r+1, v);

2)查詢:利用字首異或和(a^a=0) =》query(l-1)^query(r)

需要特別注意的一點是,異或操作與加法不同,a+a=2a而a^a=0,因此對於區間(l, r),更新時真正影響的其實只有l,l+2,l+4...這些,因此需要分奇偶討論,所以需要兩個樹狀陣列來維護。

將其拓展到二維即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e3 + 5;
int n, m;
ll xorsum[2][2][MAX][MAX];

int lowbit(int x) {
    return x & (-x);
}

void update(int x, int y, ll val) {
    for (int i = x; i <= n; i += lowbit(i)) {
        for (int j = y; j <= n; j += lowbit(j)) {
            xorsum[x & 1][y & 1][i][j] ^= val;
        }
    }
}

ll query(int x, int y) {
    ll ans = 0;
    for (int i = x; i > 0; i -= lowbit(i)) {
        for (int j = y; j > 0; j -= lowbit(j)) {
            ans ^= xorsum[x & 1][y & 1][i][j];
        }
    }
    return ans;
}

int main() {
    int tp, x0, y0, x1, y1;
    ll v;
    scanf("%d %d", &n, &m);
    while (m--) {
        scanf("%d", &tp);
        if (tp == 1) {
            scanf("%d %d %d %d", &x0, &y0, &x1, &y1);
            ll ans = 0;
            ans ^= query(x0 - 1, y0 - 1);
            ans ^= query(x0 - 1, y1);
            ans ^= query(x1, y0 - 1);
            ans ^= query(x1, y1);
            printf("%I64d\n", ans);
        } else {
            scanf("%d %d %d %d %I64d", &x0, &y0, &x1, &y1, &v);
            update(x0, y0, v);
            update(x0, y1 + 1, v);
            update(x1 + 1, y0, v);
            update(x1 + 1, y1 + 1, v);
        }
    }
}