#C11744. Submatrix Sum Query

    ID: 41094 Type: Default 1000ms 256MiB

Submatrix Sum Query

Submatrix Sum Query

You are given a matrix with ( n ) rows and ( m ) columns and a query defined by indices ( i_1, j_1, i_2, j_2 ) (all indices are 1-indexed). Your task is to compute the sum of the submatrix that has its top-left corner at ( (i_1, j_1) ) and its bottom-right corner at ( (i_2, j_2) ). Formally, you need to calculate: [ S = \sum_{i=i_1}^{i_2} \sum_{j=j_1}^{j_2} A_{ij} ] where ( A_{ij} ) is the element in the ( i^{th} ) row and ( j^{th} ) column of the matrix.

Note that the matrix indices in the input are 1-based. Make sure you convert them appropriately for internal computations if needed.

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains two integers ( n ) and ( m ) representing the number of rows and columns of the matrix, respectively.
  • The next ( n ) lines each contain ( m ) integers separated by spaces, representing the matrix elements.
  • The last line contains four integers ( i_1 ), ( j_1 ), ( i_2 ), ( j_2 ) which define the top-left and bottom-right corners of the submatrix whose sum is to be computed.

Constraints: All indices are 1-based.

outputFormat

Output a single integer to the standard output (stdout) which is the sum of the elements in the specified submatrix.## sample

3 3
1 2 3
4 5 6
7 8 9
1 1 2 2
12