题目描述
需要注意的有两点:
1. 输入如果是Scanner,则时间不够用,类似地,矩阵前缀和也是那样,需要用到BufferReader
2. System.out.println也不可以作为输出,即使一样也会报错。这个不知道是不是bug,而改成BufferWriter
Java 代码
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String[] str1 = reader.readLine().split(" ");
int n = Integer.parseInt(str1[0]);
int m = Integer.parseInt(str1[1]);
int q = Integer.parseInt(str1[2]);
int[][] b = new int[1010][1010];
for (int i = 1; i <= n; i++) {
String[] str2 = reader.readLine().split(" ");
for (int j = 1; j <= m; j++) {
int x = Integer.parseInt(str2[j - 1]);
insert(i, j, i, j, x, b);
}
}
for(int i = 0; i < q;i++){
String[] str3 = reader.readLine().split(" ");
int x1 = Integer.parseInt(str3[0]);
int y1 = Integer.parseInt(str3[1]);
int x2 = Integer.parseInt(str3[2]);
int y2 = Integer.parseInt(str3[3]);
int c = Integer.parseInt(str3[4]);
insert(x1, y1, x2, y2, c, b);
}
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];
writer.write(b[i][j] + " ");
}
writer.write("\n");
}
writer.flush();
}
static void insert(int i1, int j1, int i2, int j2, int x, int[][] b) {
b[i1][j1] += x;
b[i1][j2 + 1] -= x;
b[i2 + 1][j1] -= x;
b[i2 + 1][j2 + 1] += x;
}
}