AcWing 796. 子矩阵的和
原题链接
简单
作者:
不知名的fE
,
2024-11-20 20:19:45
,
所有人可见
,
阅读 2
import java.util.*;
public class Main {
static final int N = 1010;
static int[][] a = new int[N][N], s = new int[N][N];
static int n, m, q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); m = sc.nextInt(); q = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= n; i++) a[i] = toArr(sc.nextLine().split(" "));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
while (q -- > 0) {
String[] str = sc.nextLine().split(" ");
int x1 = toInt(str[0]), y1 = toInt(str[1]), x2 = toInt(str[2]), y2 = toInt(str[3]);
System.out.println(s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]);
}
}
static int toInt(String str) {
return Integer.parseInt(str);
}
static int[] toArr(String[] str) {
int[] res = new int[N];
for (int i = 1; i <= m; i++) res[i] = Integer.parseInt(str[i - 1]);
return res;
}
}