798. Difference Matrix

Input an integer matrix with n rows and m columns, and then input q operations, each operation contains five integers x1, y1, x2, y2, c, where (x1, y1) and (x2, y2) represent a submatrix. The upper left and lower right coordinates.

Each operation adds c to the value of each element in the selected submatrix.

Please output the matrix after you have done all the operations.

Input format
The first line contains the integers n,m,q.

The next n lines, each containing m integers, represent a matrix of integers.

Next q lines, each line contains 5 integers x1, y1, x2, y2, c, representing an operation.

Output format
A total of n lines, each line contains m integers, indicating the final matrix after all operations are completed.

Data range
1≤n,m≤1000,
1≤q≤100000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤c≤1000,
−1000≤The value of the elements in the
matrix≤1000Input Example:
3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1
Example output:
2 3 4 1
4 3 4 1
2 2 2 2

在这里插入代码片

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N][N], b[N][N];

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()
{
    
    
    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]);
        }
    }
    
    int x1, y1, x2, y2, c;
    while(q--) {
    
    
        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++) {
    
    
            b[i][j] = b[i][j] + b[i-1][j] + b[i][j-1] - b[i-1][j-1];
        }
    }
    
    for(int i=1; i<=n; i++) {
    
    
        for(int j=1; j<=m; j++) {
    
    
            cout << b[i][j] << " ";
        }
        cout << endl;
    }
    
    return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324123563&siteId=291194637