#C11407. Average Pollution Calculation

    ID: 40720 Type: Default 1000ms 256MiB

Average Pollution Calculation

Average Pollution Calculation

In this problem, you are given a grid representing the pollution levels of a city divided into sectors. Each cell in the grid contains an integer value indicating the pollution level at that sector. You are also given several queries, each query specifying a sub-grid defined by its top‐left and bottom‐right coordinates (using 1-indexing). Your task is to calculate the average pollution level for each sub-grid and output the result as a floating-point number rounded to two decimal places.

Formally, let (P) and (Q) denote the number of rows and columns in the grid respectively. For each query given by coordinates (a_1, b_1, a_2, b_2) (with (a_1 \leq a_2) and (b_1 \leq b_2)), compute [ \text{average} = \frac{\sum_{i=a_1}^{a_2} \sum_{j=b_1}^{b_2} grid[i][j]}{(a_2 - a_1 + 1) \times (b_2 - b_1 + 1)} ] and print the result with exactly two decimal places.

inputFormat

The input is given from standard input (stdin) in the following format:

  • The first line contains two integers (P) and (Q) representing the number of rows and columns of the grid.
  • The next (P) lines each contain (Q) space-separated integers, indicating the pollution levels in each sector of the grid.
  • The next line contains an integer (T), the number of queries.
  • Each of the following (T) lines contains four integers (a_1), (b_1), (a_2), (b_2), representing the coordinates of the top-left and bottom-right cells of the sub-grid, respectively (with 1-indexing and (a_1 \leq a_2,; b_1 \leq b_2)).

outputFormat

For each query, output one line with the average pollution level for the specified sub-grid. The average must be rounded and formatted to exactly two decimal places.## sample

3 3
12 15 9
8 10 17
5 14 7
1
1 1 2 2
11.25

</p>