[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=3132
[算法]
二维树状数组
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 2050 int n,m,a,b,c,d; int delta; int bit[5][MAXN][MAXN]; char op[5]; inline int lowbit(int x) { return x & (-x); } inline void modify1(int x,int y,int val) { for (register int i = x; i <= n; i += lowbit(i)) { for (register int j = y; j <= m; j += lowbit(j)) { bit[1][i][j] += val; } } } inline void modify2(int x,int y,int val) { for (register int i = x; i <= n; i += lowbit(i)) { for (register int j = y; j <= m; j += lowbit(j)) { bit[2][i][j] += val; } } } inline void modify3(int x,int y,int val) { for (register int i = x; i <= n; i += lowbit(i)) { for (register int j = y; j <= m; j += lowbit(j)) { bit[3][i][j] += val; } } } inline void modify4(int x,int y,int val) { for (register int i = x; i <= n; i += lowbit(i)) { for (register int j = y; j <= m; j += lowbit(j)) { bit[4][i][j] += val; } } } inline int query1(int x,int y) { int ret = 0; for (register int i = x; i >= 1; i -= lowbit(i)) { for (register int j = y; j >= 1; j -= lowbit(j)) { ret += bit[1][i][j]; } } return ret; } inline int query2(int x,int y) { int ret = 0; for (register int i = x; i >= 1; i -= lowbit(i)) { for (register int j = y; j >= 1; j -= lowbit(j)) { ret += bit[2][i][j]; } } return ret; } inline int query3(int x,int y) { int ret = 0; for (register int i = x; i >= 1; i -= lowbit(i)) { for (register int j = y; j >= 1; j -= lowbit(j)) { ret += bit[3][i][j]; } } return ret; } inline int query4(int x,int y) { int ret = 0; for (register int i = x; i >= 1; i -= lowbit(i)) { for (register int j = y; j >= 1; j -= lowbit(j)) { ret += bit[4][i][j]; } } return ret; } inline void Modify(int x,int y,int delta) { modify1(x,y,delta); modify2(x,y,x * delta); modify3(x,y,y * delta); modify4(x,y,x * y * delta); } inline int Query(int x,int y) { int v1 = query1(x,y),v2 = query2(x,y),v3 = query3(x,y),v4 = query4(x,y); int ans1 = x * (v1 * y - v3 + v1); int ans2 = v2 * y - v4 + v2; int ans3 = v1 * y - v3 + v1; return ans1 - ans2 + ans3; } int main() { scanf("%s%d%d",&op,&n,&m); while (scanf("%s",&op) != EOF) { if (op[0] == 'L') { scanf("%d%d%d%d%d",&a,&b,&c,&d,&delta); Modify(a,b,delta); Modify(a,d + 1,-delta); Modify(c + 1,b,-delta); Modify(c + 1,d + 1,delta); } else { scanf("%d%d%d%d",&a,&b,&c,&d); printf("%d\n",Query(c,d) - Query(a - 1,d) - Query(c,b - 1) + Query(a - 1,b - 1)); } } return 0; }