#C10549. Submatrix Sum Calculation
Submatrix Sum Calculation
Submatrix Sum Calculation
You are given a 2D matrix of integers and four integers x1, y1, x2, and y2 which represent the top-left and bottom-right coordinates of a submatrix. Your task is to compute the sum of all elements within the specified submatrix.
Note that the matrix is 0-indexed. That is, the top-left element is at coordinate (0, 0) and the bottom-right element is at coordinate (n-1, m-1), where n and m are the number of rows and columns respectively.
The submatrix sum can be expressed mathematically as:
$$S = \sum_{i=x_1}^{x_2} \sum_{j=y_1}^{y_2} A[i][j] $$where \(A\) is the given matrix.
inputFormat
The input is given through standard input (stdin) in the following format:
n m a[0][0] a[0][1] ... a[0][m-1] a[1][0] a[1][1] ... a[1][m-1] ... a[n-1][0] a[n-1][1] ... a[n-1][m-1] x1 y1 x2 y2
Where:
n
andm
are the numbers of rows and columns in the matrix respectively.- The following
n
lines each containm
integers representing the rows of the matrix. - The last line contains four integers:
x1
,y1
,x2
, andy2
, which define the submatrix corners.
You can assume that \(0 \le x_1 \le x_2 < n\) and \(0 \le y_1 \le y_2 < m\).
outputFormat
Output a single integer representing the sum of the submatrix defined by the given coordinates. The result should be printed to standard output (stdout).
## sample3 3
1 2 3
4 5 6
7 8 9
1 1 2 2
28