#K65782. Flower Summation in a Garden

    ID: 32274 Type: Default 1000ms 256MiB

Flower Summation in a Garden

Flower Summation in a Garden

You are given a garden arranged in a grid with dimensions \(n \times m\). Each cell in the grid contains an integer representing the number of flowers planted in that cell. You will be given \(k\) queries, each specifying a rectangular sub-region of the garden by providing the top-left and bottom-right coordinates \((x_1, y_1)\) and \((x_2, y_2)\) respectively.

Your task is to calculate the total number of flowers in each specified sub-region. Formally, for each query, compute the sum:

\(\sum_{i=x_1}^{x_2} \sum_{j=y_1}^{y_2} garden[i][j]\)

Note: The grid indices are 0-indexed.

inputFormat

The input is read from stdin and has the following format:

n m
row1_element1 row1_element2 ... row1_element_m
... (n rows in total)
k
x1 y1 x2 y2
... (k queries in total)

Where:

  • \(n\) and \(m\) are the number of rows and columns of the garden respectively.
  • Next \(n\) lines each contain \(m\) space-separated integers representing the garden.
  • \(k\) is the number of queries.
  • Each of the next \(k\) lines contains four integers \(x_1, y_1, x_2, y_2\) representing a query.

outputFormat

For each query, output the sum of flowers in the specified sub-region. The answer for each query should be printed on a new line to stdout.

## sample
3 3
1 2 3
4 5 6
7 8 9
2
0 0 1 1
1 1 2 2
12

28

</p>