二维树状数组,比较简单
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int llen = 1124;
int tree[llen][llen];
int len;
void init() {
for(int i = 0; i <= len; i++) {
for(int j = 0; j <= len; j++) {
tree[i][j] = 0;
}
}
}
int lowbit(int x) {
return x & -x;
}
void update(int x, int y, int val) {
for(int i = x; i <= len; i += lowbit(i)) {
for(int j = y; j <= len; j += lowbit(j)) {
tree[i][j] += val;
}
}
}
int query(int x, int y) {
int sum = 0;
for(int i = x; i > 0; i -= lowbit(i)) {
for(int j = y; j > 0; j -= lowbit(j)) {
sum += tree[i][j];
}
}
return sum;
}
int main() {
int cmd;
while(scanf("%d", &cmd) != EOF) {
int x, y, a;
int l, b, r, t;
if(0 == cmd) {
scanf("%d", &len);
init();
}
else if(1 == cmd) {
scanf("%d%d%d", &x, &y, &a);
x++; y++;
update(x, y, a);
}
else if(2 == cmd) {
scanf("%d%d%d%d", &l, &b, &r, &t);
l++; b++; r++; t++;
int ans = query(r, t) - query(r, b-1) - query(l-1, t) + query(l-1, b-1);
printf("%d\n", ans);
}
else if(3 == cmd) {
continue;
}
}
return 0;
}