AcWing 796. 子矩阵的和--java
原题链接
简单
作者:
Myo
,
2023-02-10 21:18:39
,
所有人可见
,
阅读 163
import java.io.*;
public class Main {
static final int N = 1010;
static int[][] a = new int[N][N];
static int[][] s = new int[N][N];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] tmp = br.readLine().split(" ");
int n = Integer.parseInt(tmp[0]);
int m = Integer.parseInt(tmp[1]);
int t = Integer.parseInt(tmp[2]);
for (int i = 1; i <= n; i++) {
String[] str = br.readLine().split(" ");
for (int j = 1; j <= m; j++) {
a[i][j] = Integer.parseInt(str[j - 1]);
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
// S[x2, y2] - S[x1 - 1, y2] - S[x2, y1 - 1] + S[x1 - 1, y1 - 1]
while (t-- > 0) {
String[] str1 = br.readLine().split(" ");
int x1 = Integer.parseInt(str1[0]);
int y1 = Integer.parseInt(str1[1]);
int x2 = Integer.parseInt(str1[2]);
int y2 = Integer.parseInt(str1[3]);
System.out.println(s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
}
}
}