Codeforces 341D Iahub and Xors (二维树状数组 差分 推荐)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tc_To_Top/article/details/83011677

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);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Tc_To_Top/article/details/83011677