AcWing 796. JAVA:子矩阵的和
原题链接
简单
作者:
ARM
,
2020-08-03 16:25:20
,
所有人可见
,
阅读 386
java 代码
import java.io.*;
import java.lang.*;
class Main{
public static void main(String[] args)throws Exception{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] input = buf.readLine().split(" ");
int n = Integer.valueOf(input[0]);
int m = Integer.valueOf(input[1]);
int q = Integer.valueOf(input[2]);
int[][] mat = new int[n + 1][m + 1];
for(int i = 1; i <= n; ++i){//构建前缀矩阵
String[] info = buf.readLine().split(" ");
for(int j = 1; j <= m; ++j)
mat[i][j] = Integer.valueOf(info[j - 1]) + mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
}
for(int i = 0; i < q; ++i){
String[] info = buf.readLine().split(" ");
int x1 = Integer.valueOf(info[0]);
int y1 = Integer.valueOf(info[1]);
int x2 = Integer.valueOf(info[2]);
int y2 = Integer.valueOf(info[3]);
System.out.println(mat[x2][y2] - mat[x1 - 1][y2] - mat[x2][y1 - 1] + mat[x1 - 1][y1 - 1]);
}
}
}