本文共 1455 字,大约阅读时间需要 4 分钟。
二维树状数组
#include #include #include #include #include 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; } }}
转载于:https://www.cnblogs.com/jifahu/p/5449578.html