#C8043. Max Element in Subgrid

    ID: 51982 Type: Default 1000ms 256MiB

Max Element in Subgrid

Max Element in Subgrid

You are given a grid of dimensions \(n \times m\) and \(q\) queries. Each query is defined by four integers \(r_1, c_1, r_2, c_2\) which represent the top-left and bottom-right corners (in 1-indexed format) of a subgrid of the given grid. Your task is to find the maximum element within each subgrid.

Note: The subgrid includes all elements whose row index is between \(r_1\) and \(r_2\) (inclusive) and whose column index is between \(c_1\) and \(c_2\) (inclusive).

Input Example:
For a grid of 4 rows and 4 columns with 3 queries, the grid and queries might look like:

4 4 3
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 1 2 2
2 2 3 3
1 1 4 4

The expected output for the above input is:

6
11
16

inputFormat

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

  1. The first line contains three integers \(n\), \(m\), and \(q\) representing the number of rows, number of columns, and number of queries, respectively.
  2. The next \(n\) lines each contain \(m\) integers where the \(j\)-th integer on the \(i\)-th line represents the element of the grid at row \(i\) and column \(j\).
  3. The following \(q\) lines each contain four integers \(r_1\), \(c_1\), \(r_2\), and \(c_2\) which define the top-left and bottom-right corners of a subgrid (1-indexed).

outputFormat

For each query, print the maximum element found in the specified subgrid on a new line. The output is sent to standard output (stdout).

## sample
4 4 3
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 1 2 2
2 2 3 3
1 1 4 4
6

11 16

</p>