Problem 05B. Submatrix Sum Queries ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an n × m grid of integers and q queries. Each query is a rectangle given by its top-left corner (r1, c1) and bottom-right corner (r2, c2) (0 ≤ r1 ≤ r2 ≤ n − 1, 0 ≤ c1 ≤ c2 ≤ m − 1); answer with the sum of every cell in that rectangle, inclusive.

Input format

The first line contains three integers n, m and q. Each of the next n lines contains m space-separated integers — the grid. Each of the next q lines contains four integers r1, c1, r2, c2.

Output format

Print q lines: the sum of each query rectangle, in order.

Constraints

1 ≤ n, m, \ n ⋅ m ≤ 4 ⋅ 105

1 ≤ q ≤ 2 ⋅ 105

 − 109 ≤ grid[i][j] ≤ 109

Every answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
3 4 3
1 2 3 4
5 6 7 8
9 10 11 12
0 0 1 1
1 1 2 3
0 0 2 3
14
54
78
2
1 1 1
7
0 0 0 0
7
3
2 2 1
-1 -2
-3 -4
0 0 1 1
-10

0.029s 0.007s 18