#C11201. Matrix Target Search and Submatrix Sum Query
Matrix Target Search and Submatrix Sum Query
Matrix Target Search and Submatrix Sum Query
You are given an n \times n matrix of integers. Your task is to perform two operations:
- Find the earliest occurrence of a target integer by scanning the matrix row by row, left to right. The coordinates are 1-indexed. If the target does not exist in the matrix, output
-1 -1
. - Calculate the sum of a submatrix specified by its top-left and bottom-right coordinates. Formally, given the submatrix defined by (x1, y1) and (x2, y2), compute
$$ S = \sum_{i=x_1}^{x_2} \sum_{j=y_1}^{y_2} a_{ij} $$
where each
aij
is the element in thei
-th row andj
-th column.
You need to read input from standard input and print the results to standard output.
inputFormat
The input is read from standard input and follows this format:
- The first line contains an integer
n
representing the size of the matrix. - The next
n
lines each containn
space-separated integers representing the rows of the matrix. - The following line contains an integer
target
which is the value to search for in the matrix. - The last line contains four space-separated integers:
x1 y1 x2 y2
. These represent the top-left and bottom-right coordinates of the submatrix for which you need to calculate the sum.
outputFormat
Output two lines to standard output:
- The first line contains two space-separated integers representing the 1-indexed coordinates of the first occurrence of the target in the matrix. If the target is not found, print
-1 -1
. - The second line contains a single integer which is the sum of the submatrix defined by the given coordinates.
3
1 2 3
4 5 6
7 8 9
2
1 2 1 2
1 2
2
</p>