AcWing 798. 差分矩阵(Java版本)
原题链接
简单
作者:
恒心
,
2021-01-24 20:21:58
,
所有人可见
,
阅读 261
import java.util.*;
import java.io.*;
class Main{
public static void main(String[] args) throws Exception{
int N = 1010;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] nums = bf.readLine().split(" ");
int n = Integer.parseInt(nums[0]);
int m = Integer.parseInt(nums[1]);
int q = Integer.parseInt(nums[2]);
int[][] a = new int[N][N];
int[][] b = new int[N][N];
for(int i = 1; i <= n; ++i){
nums = bf.readLine().split(" ");
for(int j = 1; j <= m; ++j){
a[i][j] = Integer.parseInt(nums[j - 1]);
insert(b, i, j, i, j , a[i][j]);
}
}
for(int i = 1; i <= q; ++i){
nums = bf.readLine().split(" ");
int x1 = Integer.parseInt(nums[0]);
int y1 = Integer.parseInt(nums[1]);
int x2 = Integer.parseInt(nums[2]);
int y2 = Integer.parseInt(nums[3]);
int c = Integer.parseInt(nums[4]);
insert(b, 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];
System.out.print(b[i][j] + " ");
}
System.out.printf("\n");
}
}
public static void insert(int[][] b, 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;
}
}