
二. 理论

三. 代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N=1e3+10;
int n, m, q;
class Difference
{
public:
int b[N][N];//原数组,原数组的差分数组
public:
Difference()
{
initIO();
fill(b[0], b[0]+N, 0);
}
void initIO()
{
// 关闭输入输出缓存,使效率提升
ios::sync_with_stdio(false);
// 解除cin和cout的默认绑定,来降低IO的负担使效率提升
cin.tie(nullptr);
cout.tie(nullptr);
}
//求出差分数组b,相当于对每个b加一次a,假定最初a全为0
void initD(int x, int y, int v)
{
add(x, y, x, y, v);
}
//(x1, y1)为左上角到(x2, y2)右下角都加上c
void add(int x1, int y1, int x2, int y2, int c)
{
b[x1][y1]+=c;
b[x1][y2+1]-=c;
b[x2+1][y1]-=c;
b[x2+1][y2+1]+=c;
}
//多个区间加完后,对b求前缀和,得到新a
void getPa()
{
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
b[i][j]+=b[i][j-1]+b[i-1][j]-b[i-1][j-1];
}
}d;
int main()
{
cin>>n>>m>>q;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
int x;
cin>>x;//构建原数组的差分数组
d.initD(i, j, x);
}
while(q--)
{
int x1, y1, x2, y2, x;//区间加操作
cin>>x1>>y1>>x2>>y2>>x;
d.add(x1, y1, x2, y2, x);
}
d.getPa();//差分数组的前缀和得到新a
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
cout<<d.b[i][j]<<" ";
cout<<endl;
}
}