输入一个 n 行 m 列的整数矩阵,再输入 q 个操作,每个操作包含五个整数 x1,y1,x2,y2,c,其中 (x1,y1) 和 (x2,y2)表示一个子矩阵的左上角坐标和右下角坐标。
每个操作都要将选中的子矩阵中的每个元素的值加上 c。
请你将进行完所有操作后的矩阵输出。
#include <iostream> using namespace std; const int N = 1010; int a[N][N], b[N][N]; int n, m, q; void insert (int x1, int y1, int x2, int y2, int c) { b[x1][y1] += c; b[x1][y2 + 1] -= c; b[x2 + 1][y1] -= c; b[x2 + 1][y2 + 1] += c; } int main() { scanf ("%d%d%d", &m, &n, &q); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { scanf ("%d", &a[i][j]); insert(i, j, i, j, a[i][j]); } } while (q--) { int x1, y1, x2, y2, c; scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c); insert(x1, y1, x2, y2, c); } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { a[i][j] = a[i - 1][j] + a[i][j - 1] + b[i][j] - a[i - 1][j - 1]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { printf ("%d ", a[i][j]); } puts(""); } return 0; }
标签:x1,int,矩阵,差分,x2,y1,y2 From: https://www.cnblogs.com/leetothemoon/p/16939768.html