AcWing_798 差分矩阵(二维差分、模板)

AcWing

798. 差分矩阵

#include <bits/stdc++.h>
using namespace std;
int n, m, q;
const int N = 1010, M = 1010;
int b[N][M];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++){
        int temp;
        cin >> temp;
        b[i][j] += temp, b[i + 1][j] -= temp, b[i][j + 1] -= temp, b[i + 1][j + 1] += temp;
    }
    while(q --){
        int x1, y1, x2, y2, c;
        cin >> x1 >> y1 >> x2 >> y2 >> c;
        b[x1][y1] += c, b[x2 + 1][y1] -= c, b[x1][y2 + 1] -= c, b[x2 + 1][y2 + 1] += c;
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            b[i][j] = b[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
            cout << b[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}