Acwing798. 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 sub-matrix Coordinates of upper left corner and lower right corner.

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

Please output the matrix after all operations.

input format

The first line contains integers n,m,q.

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

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

output format

A total of n rows, each row of m integers, represents 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≤value of elements in matrix≤1000

Input sample:

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

Sample output:

2 3 4 1
4 3 4 1
2 2 2 2
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e3 + 10;
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]);      //构建差分数组
        }
    }
    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++)
        {
            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++)
        {
            printf("%d ", b[i][j]);
        }
        printf("\n");
    }
    return 0;
}

 

If it is extended to two dimensions, we need to add c to the value of each element in the selected sub-matrix of the two-dimensional array, whether it can also achieve the time complexity of O(1). The answer is yes, consider two-dimensional difference.

The a[][] array is the prefix and array of the b[][] array, then b[][] is the differential array of a[][]

Original array: a[i][j]

Let's construct the difference array: b[i][j]

Make a[i][j] in the a array is the sum of the rectangular elements enclosed by the upper left corner (1,1) to the lower right corner (i,j) of the b array.

How to construct the b array?

Let's think backwards.

The purpose of constructing a two-dimensional difference array is to make the operation of adding c to each element in the selected neutron matrix in the original two-dimensional array a, which can be optimized from the time complexity of O(n*n) to O (1)

It is known that the selected sub-matrix in the original array a is a rectangular area surrounded by (x1, y1) as the upper left corner and (x2, y2) as the lower right corner;

Always remember that array a is the prefix and array of array b. For example, the modification of b[i][j] of array b will affect every number in array a from a[i][j] and beyond. .

Assuming that we have constructed the b array, analogous to the one-dimensional difference, we perform the following operations
to add c to the value of each element in the selected sub-matrix

b[x1][y1] + = c;

b[x1,][y2+1] - = c;

b[x2+1][y1] - = c;

b[x2+1][y2+1] + = c;

Performing the above operations on the b array each time is equivalent to:

for(int i=x1;i<=x2;i++)
  for(int j=y1;j<=y2;j++)
    a[i][j]+=c;

Guess you like

Origin blog.csdn.net/weixin_52030368/article/details/129126929