二维树状数组
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <algorithm> using namespace std; int n; int c[1030][1030]; int lowbit(int k) { return k&(-k); } void add(int x,int y,int num) { for(int i = x;i <= n;i += lowbit(i)) for(int j = y;j <= n;j += lowbit(j)) c[i][j] += num; } void sub(int x,int y,int num) { for(int i = x;i >= 0;i -= lowbit(i)) for(int j = y;j >= 0;j -= lowbit(j)) c[i][j] -= num; } int cal(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 += c[i][j]; return sum; } int main() { int first,op,size; scanf("%d%d",&first,&n); { size = n; memset(c,0,sizeof(c)); for(int i = 1;i <= size;i++) for(int j = 1;j <= size;j++) add(i,j,first); int op; while(scanf("%d",&op) != EOF) { if(op == 1) { int x,y,a; scanf("%d%d%d",&x,&y,&a); x++,y++; add(x,y,a); } else if(op == 2) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x1++,y1++,x2++,y2++; int sum = cal(x2,y2) + cal(x1-1,y1-1) - cal(x2,y1-1) - cal(x1-1,y2); printf("%d\n",sum); } else break; } } }
时间: 2024-10-13 14:35:59