其他分享
首页 > 其他分享> > AcWing 798. 差分矩阵

AcWing 798. 差分矩阵

作者:互联网

题目传送门

#include <bits/stdc++.h>

using namespace std;
const int N = 1010;
int a[N][N], b[N][N];
int n, m, q;

/**
 * 功能:二维差分构建
 * @param x1 左上角横坐标
 * @param y1 左上角纵坐标
 * @param x2 右下角横坐标
 * @param y2 右下角纵坐标
 * @param c  值
 */
void insert(int x1, int y1, int x2, int y2, int c) {
    b[x1][y1] += c;
    b[x2 + 1][y1] -= c;
    b[x1][y2 + 1] -= c;
    b[x2 + 1][y2 + 1] += c;
}

int main() {
    cin >> n >> m >> q;
    //读入并构建
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j], insert(i, j, i, j, a[i][j]);

    //q次区域变化
    while (q--) {
        int x1, y1, x2, y2, c;
        cin >> x1 >> y1 >> x2 >> y2 >> c;
        insert(x1, y1, x2, y2, c);
    }
    //还原二维数组
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {//二维前缀和公式
            a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]
                    + b[i][j];
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

标签:x1,int,差分,798,x2,param,y1,y2,AcWing
来源: https://www.cnblogs.com/littlehb/p/15761161.html