[题目链接]
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[][MAXN][MAXN];
char op[]; 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[][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[][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[][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[][i][j] += val;
}
}
}
inline int query1(int x,int y)
{
int ret = ;
for (register int i = x; i >= ; i -= lowbit(i))
{
for (register int j = y; j >= ; j -= lowbit(j))
{
ret += bit[][i][j];
}
}
return ret;
}
inline int query2(int x,int y)
{
int ret = ;
for (register int i = x; i >= ; i -= lowbit(i))
{
for (register int j = y; j >= ; j -= lowbit(j))
{
ret += bit[][i][j];
}
}
return ret;
}
inline int query3(int x,int y)
{
int ret = ;
for (register int i = x; i >= ; i -= lowbit(i))
{
for (register int j = y; j >= ; j -= lowbit(j))
{
ret += bit[][i][j];
}
}
return ret;
}
inline int query4(int x,int y)
{
int ret = ;
for (register int i = x; i >= ; i -= lowbit(i))
{
for (register int j = y; j >= ; j -= lowbit(j))
{
ret += bit[][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[] == 'L')
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&delta);
Modify(a,b,delta);
Modify(a,d + ,-delta);
Modify(c + ,b,-delta);
Modify(c + ,d + ,delta);
} else
{
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("%d\n",Query(c,d) - Query(a - ,d) - Query(c,b - ) + Query(a - ,b - ));
}
} return ; }