문제링크
import java.io.BufferedReader
import java.io.BufferedWriter
private lateinit var bufferedReader: BufferedReader
private lateinit var bufferedWriter: BufferedWriter
fun main() {
bufferedReader = System.`in`.bufferedReader()
bufferedWriter = System.out.bufferedWriter()
val (n, m) = bufferedReader
.readLine()
.split(" ")
.map { it.toInt() }
val table = Array(n + 1) { mutableListOf(0) }
repeat(n) { index ->
val elements = bufferedReader
.readLine()
.split(" ")
.map { it.toInt() }
table[index + 1].addAll(elements)
}
val sum = Array(n + 1) { Array(n + 1) { 0 } }
for (i in 1..n) {
for (j in 1..n) {
sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + table[i][j]
}
}
repeat(m) {
val (x1, y1, x2, y2) = bufferedReader
.readLine()
.split(" ")
.map { it.toInt() }
bufferedWriter.write("${sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]}\n")
}
bufferedReader.close()
bufferedWriter.close()
}