AcWing 798. 差分矩阵(Java)
原题链接
简单
作者:
火球大的脸盆
,
2022-07-07 20:41:34
,
所有人可见
,
阅读 89
import java.io.*;
public class Main {
public static int n, m, q, x1, y1, x2, y2, c;
public static int[][] a = new int[1005][1005], b = new int[1005][1005];
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 IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] first_line = br.readLine().split(" ");
n = Integer.parseInt(first_line[0]);
m = Integer.parseInt(first_line[1]);
q = Integer.parseInt(first_line[2]);
for (int i = 1; i <= n; i++) {
String[] second_line = br.readLine().split(" ");
for (int j = 1; j <= m; j++) {
a[i][j] = Integer.parseInt(second_line[j - 1]);
insert(i, j, i, j, a[i][j]);
}
}
while (0 != q--) {
String[] third_line = br.readLine().split(" ");
x1 = Integer.parseInt(third_line[0]);
y1 = Integer.parseInt(third_line[1]);
x2 = Integer.parseInt(third_line[2]);
y2 = Integer.parseInt(third_line[3]);
c = Integer.parseInt(third_line[4]);
insert(x1, y1, x2, y2, c);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + b[i][j];
bw.write(a[i][j] + " ");
bw.flush();
}
bw.write("\n");
bw.flush();
}
}
}
这题已经用了最快的输入输出,居然1w多ms。Java的输入输出真的比C++差远了哇