2 条题解
-
0
C95 二维树状数组+差分 CF341D Iahub and Xors
#include <bits/stdc++.h> // 二维树状数组+差分 O(mlognlogn) using namespace std; #define LL long long #define lowb(x) x & -x int n, m; int op, a, b, c, d; LL v, ans; LL s[1001][1001][2][2]; // 4个二维树状数组 void change(int x, int y, LL v) { // 向后修 for (int i = x; i <= n; i += lowb(i)) for (int j = y; j <= n; j += lowb(j)) s[i][j][x & 1][y & 1] ^= v; } LL query(int x, int y) { // 向前查 LL t = 0; for (int i = x; i; i -= lowb(i)) for (int j = y; j; j -= lowb(j)) t ^= s[i][j][x & 1][y & 1]; return t; } int main() { scanf("%d%d", &n, &m); while (m--) { scanf("%d%d%d%d%d", &op, &a, &b, &c, &d); if (op == 2) { // 修改: 差分 scanf("%lld", &v); change(a, b, v); change(a, d + 1, v); change(c + 1, b, v); change(c + 1, d + 1, v); } else { // 查询: 区间异或和 ans = query(c, d) ^ query(c, b - 1) ^ query(a - 1, d) ^ query(a - 1, b - 1); printf("%lld\n", ans); } } return 0; } -
0
C95 二维树状数组+差分 CF341D Iahub and Xors
#include <bits/stdc++.h> // 二维树状数组+差分 O(mlognlogn) using namespace std; #define LL long long #define lowb(x) x & -x int n, m; int op, a, b, c, d; LL v, ans; LL s[1001][1001][2][2]; // 4个二维树状数组 void change(int x, int y, LL v) { // 向后修 for (int i = x; i <= n; i += lowb(i)) for (int j = y; j <= n; j += lowb(j)) s[i][j][x & 1][y & 1] ^= v; } LL query(int x, int y) { // 向前查 LL t = 0; for (int i = x; i; i -= lowb(i)) for (int j = y; j; j -= lowb(j)) t ^= s[i][j][x & 1][y & 1]; return t; } int main() { scanf("%d%d", &n, &m); while (m--) { scanf("%d%d%d%d%d", &op, &a, &b, &c, &d); if (op == 2) { // 修改: 差分 scanf("%lld", &v); change(a, b, v); change(a, d + 1, v); change(c + 1, b, v); change(c + 1, d + 1, v); } else { // 查询: 区间异或和 ans = query(c, d) ^ query(c, b - 1) ^ query(a - 1, d) ^ query(a - 1, b - 1); printf("%lld\n", ans); } } return 0; }
- 1
信息
- ID
- 1886
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 68
- 已通过
- 10
- 上传者