#K74667. Finding the Lowest Altitudes in a Grid
Finding the Lowest Altitudes in a Grid
Finding the Lowest Altitudes in a Grid
You are given a grid of altitudes with \(n\) rows and \(m\) columns. Each cell in the grid contains an integer representing its altitude. You need to answer \(q\) queries. In each query, you are given four integers \(r_1\), \(c_1\), \(r_2\), and \(c_2\) which represent the top‐left and bottom‐right corners of a subgrid. Your task is to determine the minimum altitude within that subgrid.
Note: The rows and columns are 1-indexed. The subgrid defined by \(r_1, c_1, r_2, c_2\) includes all cells \((i, j)\) satisfying \(r_1 \le i \le r_2\) and \(c_1 \le j \le c_2\).
The mathematical formulation of a query is to compute:
[ \min_{r_1 \leq i \leq r_2,; c_1 \leq j \leq c_2} grid[i][j] ]
Solve the problem by reading from the standard input and writing your answer to the standard output.
inputFormat
The input is given via stdin in the following format:
n m row1 row2 ... rown q r1 c1 r2 c2 r1 c1 r2 c2 ... r1 c1 r2 c2
Where:
- \(n\) and \(m\) are the number of rows and columns of the grid respectively.
- Each of the next \(n\) lines contains \(m\) integers representing the altitudes of that row.
- \(q\) is the number of queries.
- Each query consists of four integers \(r_1, c_1, r_2, c_2\), denoting the indices of the top-left and bottom-right corners of the subgrid.
outputFormat
For each query, output a single line containing the minimum altitude found in the specified subgrid. The output should be written to stdout.
## sample3 3
1 2 3
4 5 6
7 8 9
2
1 1 2 2
2 2 3 3
1
5
</p>