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

本文详细解析了CodeForces上D.IahubandXors问题,介绍了一个n*n矩阵上的操作,包括查询指定子矩阵的元素异或和与更新子矩阵元素值的算法实现。通过使用树状数组维护前缀异或和,解决了一维和二维情况下的查询和更新操作。

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

 

<think>我们之前提到,单树状数组使用差分实现区间修改和单点查询。具体来说: 1. 我们维护一个差分数组d,其中d[i] = a[i] - a[i-1](a为原数组)。 2. 区间修改[l, r]增加k:转化为在差分数组上d[l] += k, d[r+1] -= k。 3. 单点查询a[i]:实际上就是求差分数组d的前缀和,即a[i] = d[1] + d[2] + ... + d[i]。 因此,单树状数组差分树状数组)实现的查询操作实际上是: - 区间修改:O(log n) - 单点查询:O(log n) 注意:这里不能直接支持区间查询(除非遍历每个点,但那样是O(n log n)),所以需要双树状数组来实现高效的区间查询。 总结:单树状数组差分实现的查询是“单点查询”,即查询原数组中某个位置的值。</think>### 单树状数组差分实现的查询本质 单树状数组通过差分实现的查询操作,本质上是**高效计算原数组的单点值**。具体技术解析如下: #### 核心原理 1. **差分数组定义**: - 设原数组为 $a[1..n]$ - 构造差分数组 $d[i] = a[i] - a[i-1]$(其中 $a[0]=0$) - 树状数组 `t[]` 实际存储的是 $d[i]$ 2. **查询操作的本质**: ```cpp int query(int x) { // 查询a[x]的值 int res = 0; while(x > 0) { res += t[x]; // 实际累加的是d[1]到d[x] x -= lowbit(x); } return res; // 返回a[x] = d[1]+d[2]+...+d[x] } ``` - **数学本质**:$a[x] = \sum_{i=1}^{x} d[i]$ - **物理意义**:通过差分数组的前缀和还原原数组单点值 #### 操作特性对比 | 操作类型 | 差分树状数组 | 原始数组 | |----------------|----------------------------|------------------| | **区间修改** | $O(\log n)$ (修改两点) | $O(n)$ | | **单点查询** | $O(\log n)$ (前缀和计算) | $O(1)$ | | **空间占用** | $O(n)$ | $O(n)$ | #### 典型应用场景 1. **高频区间修改+低频单点查询**: - 实时监控系统中的批量阈值调整 - 游戏引擎中的区域状态更新 2. **算法优化**: ```cpp // 区间[l,r]加k update(l, k); // d[l] += k update(r+1, -k); // d[r+1] -= k // 查询a[i] int val = query(i); ``` 3. **竞赛常见应用**: - 解决「区间增减+单点求值」类问题(如Codeforces EDU Segment Tree Part1 基础题) ### 技术演进图 ``` 基础树状数组(单点修改+前缀和查询) ↓ 差分优化树状数组(区间修改+单点查询)← 您当前询问的技术 ↓ 双树状数组(区间修改+区间查询) ← 高级扩展 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值