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

129 篇文章 0 订阅
77 篇文章 8 订阅

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

 

树状数组(Fenwick Tree)是一种用于高效处理区间和查询的数据结构,常用于解一维数组的前缀和、区间更新和查询等问题。 在 Codeforces 上,树状数组常被用来解决一些与区间和查询有关的问题。它可以在 O(logn) 的时间内完成单点更新和查询,以及区间求和等操作。 下面是一个简单的示例代码,展示了如何实现一个基本的树状数组: ```cpp #include <iostream> #include <vector> using namespace std; // 获取最低位的 1 int getLowbit(int x) { return x & -x; } // 树状数组的单点更新操作 void update(vector<int>& fenwick, int index, int delta) { while (index < fenwick.size()) { fenwick[index] += delta; index += getLowbit(index); } } // 树状数组的前缀和查询操作 int query(vector<int>& fenwick, int index) { int sum = 0; while (index > 0) { sum += fenwick[index]; index -= getLowbit(index); } return sum; } int main() { int n; cin >> n; vector<int> fenwick(n + 1, 0); // 初始化树状数组 for (int i = 1; i <= n; i++) { int val; cin >> val; update(fenwick, i, val); } // 进行查询操作 int q; cin >> q; while (q--) { int type; cin >> type; if (type == 1) { int index, delta; cin >> index >> delta; update(fenwick, index, delta); } else if (type == 2) { int l, r; cin >> l >> r; int sum = query(fenwick, r) - query(fenwick, l - 1); cout << sum << endl; } } return 0; } ``` 在这个示例中,我们使用了一个长度为 n 的数组 `fenwick` 来表示树状数组。`update` 函数用于更新树状数组中的某个元素,`query` 函数用于查询树状数组中某个区间的和。 你可以根据具体问题的要求进行相应的修改和扩展。希望对你有所帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值