差分矩阵为b,原矩阵是a,
1 对确定的i, j, 元素a[i][j]的值,等于b[x][y]的和, 其中1 <= x <= i; 1 <= y <= j
2 对确定的i, j, 执行b[i][j]+=c的意义是将a[x][y]全部加上c,其中i <= x <= n, j <= y <= m;
#include <iostream>
using namespace std;
const int s = 1e3+10;
int a[s][s], b[s][s];
void insert(int x, int y, int z, int w, int c) {
b[x][y] += c;
b[z+1][y] -=c;
b[x][w+1] -=c;
b[z+1][w+1] += c;
}
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j<= m; j++)
insert(i, j, i, j, a[i][j]);
}
while (q--) {
int x, y, z, w, c;
cin >> x >> y >> z >> w >> c;
insert(x, y, z, w, c);
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++) {
b[i][j] += b[i-1][j] + b[i][j-1] - b[i-1][j-1];//前缀和
cout << b[i][j] << ' ';
}
cout << endl;
} return 0;
}
谢谢大佬。其他都没讲清楚,总算你把我讲明白了