각 칸에 대해 해당 행과 열에 있는 모든 값의 합을 출력합니다. 각 행의 합과 각 열의 합을 한 번씩 구한 뒤, 모든 칸의 답을 계산합니다. 해당 칸의 값은 행의 합과 열의 합에 모두 포함되므로, 한 번 빼서 중복 계산을 바로잡습니다.
rowSum[i] + colSum[j] - grid[i][j]
큰 합이 int 범위를 넘지 않도록 값과 합은 long으로 저장합니다. 격자를 읽고 합을 계산하는 데 O(HW) 시간이 걸립니다. 격자에 O(HW) 공간을 사용하고, 행·열 합 배열에 추가로 O(H + W) 공간을 사용합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner {
private final InputStream input;
private final byte[] buffer = new byte[1 << 16];
private int length = 0;
private int pointer = 0;
FastScanner(InputStream input) {
this.input = input;
}
private int read() throws IOException {
if (pointer == length) {
length = input.read(buffer);
pointer = 0;
if (length == -1) return -1;
}
return buffer[pointer++];
}
long nextLong() throws IOException {
int c;
do {
c = read();
} while (c <= ' ' && c != -1);
long value = 0;
while (c > ' ') {
value = value * 10 + c - '0';
c = read();
}
return value;
}
}
public static void main(String[] args) throws IOException {
FastScanner input = new FastScanner(System.in);
int h = (int) input.nextLong();
int w = (int) input.nextLong();
long[][] grid = new long[h][w];
long[] rowSum = new long[h];
long[] colSum = new long[w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
grid[i][j] = input.nextLong();
rowSum[i] += grid[i][j];
colSum[j] += grid[i][j];
}
}
StringBuilder output = new StringBuilder();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (j > 0) output.append(' ');
output.append(rowSum[i] + colSum[j] - grid[i][j]);
}
output.append('\n');
}
System.out.print(output);
}
}