题目:
输入一个 n 行 m 列的整数矩阵,再输入 q 个操作,每个操作包含五个整数 x1,y1,x2,y2,c,其中 (x1,y1) 和 (x2,y2) 表示一个子矩阵的左上角坐标和右下角坐标。
每个操作都要将选中的子矩阵中的每个元素的值加上 c。
请你将进行完所有操作后的矩阵输出。
输入格式
第一行包含整数 n,m,q。
接下来 n 行,每行包含 m 个整数,表示整数矩阵。
接下来 q 行,每行包含 5 个整数 x1,y1,x2,y2,c,表示一个操作。
输出格式
共 n 行,每行 m 个整数,表示所有操作进行完毕后的最终矩阵。
数据范围
1≤n,m≤1000,
1≤q≤100000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤c≤1000,
−1000≤矩阵内元素的值≤1000
输入样例:
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
输出样例:
2 3 4 1
4 3 4 1
2 2 2 2
import java.io.*;
import java.util.*;
public class Main{
public static int N = 1010;
public static int[][] nums = new int[N][N];
public static int[][] b = new int[N][N]; //虚拟的差分矩阵
//矩阵b的插入操作
public static 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;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
String[] arrs1 = br.readLine().split(" ");
int n = Integer.parseInt(arrs1[0]);
int m = Integer.parseInt(arrs1[1]);
int q = Integer.parseInt(arrs1[2]);
for(int i = 1;i <= n;i ++) {
String[] arrs2 = br.readLine().split(" ");
for(int j = 1;j <= m;j ++) {
nums[i][j] = Integer.parseInt(arrs2[j-1]);
//System.out.print(nums[i][j]);
}
}
//构造差分矩阵b
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= m;j ++) {
insert(i, j, i, j, nums[i][j]);
}
}
while(q -- > 0) {
String[] arrs3 = br.readLine().split(" ");
int x1 = Integer.parseInt(arrs3[0]);
int y1 = Integer.parseInt(arrs3[1]);
int x2 = Integer.parseInt(arrs3[2]);
int y2 = Integer.parseInt(arrs3[3]);
int c = Integer.parseInt(arrs3[4]);
insert(x1, y1, x2, y2, c);
}
//求差分矩阵b的前缀和
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= m;j ++) {
nums[i][j] = nums[i-1][j] + nums[i][j-1] - nums[i-1][j-1] + b[i][j];
}
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= m;j ++) {
w.write(nums[i][j] + " ");
}
w.write("\n");
}
w.flush();
}
}