#C8212. Average Plant Heights in Sub-Grids

    ID: 52170 Type: Default 1000ms 256MiB

Average Plant Heights in Sub-Grids

Average Plant Heights in Sub-Grids

You are given a 2D grid representing the heights of plants in a field. The grid has R rows and C columns. You will also be given a number of queries, each query specifying a rectangular sub-grid using its top-left and bottom-right coordinates. For each query, compute the average height within the specified sub-grid and output the result rounded to two decimal places.

You may use the following formula in your solution to quickly compute the sum of values in a sub-grid using a prefix sum matrix:

\[ S(r_2, c_2) - S(r_1-1, c_2) - S(r_2, c_1-1) + S(r_1-1, c_1-1) \]

where \(S(i,j)\) is the cumulative sum from the beginning of the grid up to cell \((i, j)\). The count of elements in a sub-grid that extends from \((r_1, c_1)\) to \((r_2, c_2)\) is \((r_2 - r_1 + 1) \times (c_2 - c_1 + 1)\). Use this method to compute the average efficiently.

inputFormat

The first line contains two integers R and C representing the number of rows and columns, respectively.

The next R lines each contain C space-separated integers, representing the grid of plant heights.

The following line contains an integer Q, the number of queries.

Each of the next Q lines contains four integers r1, c1, r2, c2 describing a query. Here, (r1, c1) is the top-left coordinate and (r2, c2) is the bottom-right coordinate of the sub-grid (1-indexed).

outputFormat

For each query, output the average height of the plants in the corresponding sub-grid, rounded to two decimal places. Each answer should be printed on a new line.

## sample
3 4
5 7 9 4
2 3 6 8
1 4 5 7
2
1 1 2 2
2 2 3 4
4.25

5.50

</p>