解题思路:
- 建立差分二维数组,每一行按照一维差分数组建立cf[i][j]=v[i][j]−v[i][j−1],(1≤i≤n,1≤j≤m)
- 对于区间更新q,只要对每行按一维的更新cf[i][y1]+=a,cf[i][y2+1]−=a,(x1≤i≤x2)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m, q;
cin>>n>>m>>q;
vector<vector<long long>> v(n+1, vector<long long>(m+1, 0));
vector<vector<long long>> cf(n+1, vector<long long>(m+1, 0));
for(int i = 1; i <=n; ++i){
for(int j = 1; j <= m; ++j){
scanf("%lld", &v[i][j]);
cf[i][j] = v[i][j] - v[i][j-1];
}
}
int x1, y1, x2, y2, a;
for(int i = 1; i <= q; ++i){
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&a);
for(int j = x1; j <= x2; ++j){
cf[j][y1] += a;
if(y2+1<=m) cf[j][y2+1] -= a;
}
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <=m; ++j){
v[i][j] = v[i][j-1]+cf[i][j];
if (j!= m)cout<<v[i][j]<<" ";
else cout<<v[i][j]<<endl;
}
}
return 0;
}