why
how
理论
1.如何构造前缀和矩阵
2.如何计算子矩阵的合
总矩阵减去一个L,再加回左上角。
通过左上右下来确定子矩阵方位
代码
1.记住理论的两张图作为记忆线索
2.x行,y列。数组[行][列]。
3.通过左上坐标,右下坐标来确定子矩阵方位
package lanqiaobei.二分和前缀;
import java.util.Scanner;
public class juzhen {
static int[][] raw;
static int[][] add;
static int q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int q = sc.nextInt();
raw = new int[x+1][y+1];
add = new int[x+1][y+1];
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
raw[i][j] = sc.nextInt();
/*构造前缀和数组*/
add[i][j] = add[i-1][j] + add[i][j-1] - add[i-1][j-1] + raw[i][j];
}
}
for (int i = 0; i < q; i++) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int child = add[x2][y2] - add[x1-1][y2] - add[x2][y1-1] - add[x1-1][y1-1];
System.out.println(child);
}
}
}