本蒟蒻第三篇题解,望管理给过!
思路
貌似 二维前缀和 二维树状数组 就阔以解决问题了。(线段树大佬勿喷
注意
按照我的方法,需要开启:
- 快读
- 氧气 优化;
- 臭氧 优化;
- 优化;
- 洛谷 优化;
(大佬可以尝试关闭某些优化,反正我没试过
CODE
#include <stdio.h>
#include <iostream>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(3)
#pragma GCC optimize(Ofast)
const int N = 2501;
int n, m;
int c1[N][N], c2[N][N], c3[N][N], c4[N][N];
template<typename TYPE>
inline void read(TYPE&x) {
x = 0;
int w = 1;
char c = getchar();
for(; c < '0' || c > '9'; w *= c == '-' ? -1 : 1, c = getchar());
for(; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c ^ 48), c = getchar());
x *= w;
}
inline void add(int x, int y, int k) {
for (int i = x; i <= n; i += i & -i)
for (int j = y; j <= m; j += j & -j) {
c1[i][j] += k;
c2[i][j] += k * (x - 1);
c3[i][j] += k * (y - 1);
c4[i][j] += k * (x - 1) * (y - 1);
}
}
inline int query(int x, int y) {
int ret1 = 0, ret2 = 0, ret3 = 0, ret4 = 0;
for (int i = x; i; i -= i & -i)
for (int j = y; j; j -= j & -j) {
ret1 += c1[i][j];
ret2 += c2[i][j];
ret3 += c3[i][j];
ret4 += c4[i][j];
}
return ret1 * x * y - ret2 * y - ret3 * x + ret4;
}
int main(void) {
scanf("X %d %d", &n, &m);
char op;
int a, b, c, d, delta;
while (std:: cin >> op) {
read(a), read(b), read(c), read(d);
if (op == 'L') {
read(delta);
add(c + 1, d + 1, delta);
add(c + 1, b, -delta);
add(a, d + 1, -delta);
add(a, b, delta);
}
else
printf("%d\n", query(c, d) - query(c, b - 1) - query(a - 1, d) + query(a - 1, b - 1));
}
return 0;
}(最慢一个点卡到 才过,洛谷评测姬相当卡啊
推荐树状数组题单
官方精选
用户精选
The end. Thanks.
(走过路过一定要赞过啊