796. 子矩阵的和

67 阅读2分钟

796. 子矩阵的和 - AcWing题库

Submatrix Sum

  • Definition: Submatrix sum is the total sum of elements within a specific submatrix of a larger matrix.
  • Calculation: If the matrix is matrix, the submatrix sum is the cumulative sum of elements within a designated region of the matrix.
  • Application: Submatrix sums are often utilized in optimization algorithms, especially when dealing with problems involving two-dimensional arrays.

Question

Content

输入一个 n 行 m 列的整数矩阵,再输入 q 个询问,每个询问包含四个整数 x1, y1 , x2 , y2,表示一个子矩阵的左上角坐标和右下角坐标。

对于每个询问输出子矩阵中所有数的和。

输入格式

第一行包含三个整数 n,m,q。

接下来 n 行,每行包含 m 个整数,表示整数矩阵。

接下来 q 行,每行包含四个整数 x1, y1 , x2 , y2,表示一组询问。

输出格式

共 q 行,每行输出一个询问的结果。

数据范围

1 ≤ n,m ≤ 1000, 1 ≤ q ≤ 200000, 1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m, −1000 ≤ 矩阵内元素的值 ≤ 1000

输入样例:

3 4 3
1 7 2 4
3 6 2 8
2 1 2 3
1 1 2 2
2 1 3 4
1 3 3 4

输出样例:

17
27
21

Solution

Java

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int q = scanner.nextInt();

        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int[][] sums = new int[n + 1][m + 1];
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                sums[i][j] = sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1]
                        + matrix[i - 1][j - 1];
            }
        }

        while (q-- > 0) {
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();

            int sum = sums[x2][y2] - sums[x2][y1 - 1] - sums[x1 - 1][y2]
                    + sums[x1 - 1][y1 - 1];

            System.out.println(sum);
        }

        scanner.close();
    }

}

Summary

Efficiently computes and prints submatrix sums based on prefix sums for given matrix and queries.