传送门:HihoCoder 1336
题解
树状数组在二维上的扩展, 水题
要注意的是sum的时候要拆分, 因为sum是从左上角(1, 1)到(x, y)这个矩阵的sum, 所有要进行小划分
树状数组, 下标不能为0
AC code:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define lowbit(x) (x & (-x))
#define LL long long
#define debug 0
const int maxn(1005);
const int mod(1e9 + 7);
int c[maxn][maxn];
int n, q;
void add(int x, int y, int v) {
for (int i = x; i <= n; i += lowbit(i)) {
for (int j = y; j <= n; j += lowbit(j)) {
c[i][j] += v;
}
}
}
LL getSum(int x, int y) {
LL res = 0;
for (int i = x; i; i -= lowbit(i)) {
for (int j = y; j; j -= lowbit(j)) {
res += c[i][j];
}
}
return res;
}
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
int x1, y1, x2, y2, v;
char s[10];
while (cin >> n >> q) {
memset(c, 0, sizeof(c));
//memset(ans, 0, sizeof(ans));
while(q--) {
cin >> s;
if (s[0] == 'A') {
cin >> x1 >> y1 >> v;
add(x1 + 1, y1 + 1, v);//传参要注意lowbit边界
}
else {
cin >> x1 >> y1 >> x2 >> y2;
LL ans = getSum(x2 + 1, y2 + 1) - getSum(x2 + 1, y1) - getSum(x1, y2 + 1) + getSum(x1, y1);//划分sum
ans %= mod;
if (ans < 0) ans += mod;//取模处理
cout << ans << endl;
}
}
}
return 0;
}