#C7735. Maximum Apples in a Subgrid

    ID: 51639 Type: Default 1000ms 256MiB

Maximum Apples in a Subgrid

Maximum Apples in a Subgrid

You are given an orchard represented as a grid of size \(n \times m\). Each cell of the grid contains the number of apples on a tree. Your task is to process a series of queries. Each query specifies a sub-grid, and you must determine the maximum number of apples on any tree within that sub-grid.

Input Constraints:

  • The first line contains two integers \(n\) and \(m\) — the number of rows and columns in the grid.
  • The next \(n\) lines each contain \(m\) integers representing the number of apples on each tree.
  • The following line contains an integer \(q\), the number of queries.
  • Each of the next \(q\) lines contains four integers \(r_1, c_1, r_2, c_2\) (0-indexed) indicating the top-left and bottom-right coordinates of the sub-grid.

For each query, output one integer — the maximum number of apples in the specified sub-grid.

inputFormat

The input begins with two integers n and m, representing the number of rows and columns. The following n lines each contain m integers, which represent the number of apples on each tree in the orchard. Next, an integer q is given, which is the number of queries. Then q lines follow, each containing four integers r1, c1, r2, c2 — the coordinates that define the sub-grid for which you need to determine the maximum apple count.

outputFormat

For every query, output an integer on a separate line, representing the maximum number of apples found in the specified sub-grid.## sample

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

9 9

</p>