二维树状数组板子?qwq
注意下标不要从0开始
代码如下
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #define MogeKo qwq using namespace std; #define x1 qwqw #define x2 qwqwq #define y1 qwqwqw #define y2 qwqwqwq const int maxn = 1500; int n,m,k,x1,x2,y1,y2; int c[maxn][maxn]; int lowbit(int x) { return x & (-x); } void update(int x,int y,int k) { for(int i = x; i <= n; i += lowbit(i)) for(int j = y; j <= n; j += lowbit(j)) c[i][j] += k; } int query(int x,int y) { int ret = 0; for(int i = x; i; i -= lowbit(i)) for(int j = y; j; j -= lowbit(j)) ret += c[i][j]; return ret; } int main() { scanf("%d",&n); while(1) { scanf("%d",&m); if(m == 1) { scanf("%d%d%d",&x1,&y1,&k); update(x1+1,y1+1,k); } else if(m == 2) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("%d ",query(x2+1,y2+1)-query(x2+1,y1)-query(x1,y2+1)+query(x1,y1)); } else if(m == 3) break; } return 0; }