An orchard is laid out as an R by C grid of plots; plot value g[i][j] is that plot's net yield (which may be negative for a plot running at a loss). Answer q queries. Each query gives r1 c1 r2 c2 (all 1-indexed, with r1 <= r2 and c1 <= c2) and asks for the total yield of the axis-aligned rectangle whose top-left plot is (r1, c1) and bottom-right plot is (r2, c2), inclusive.
Line 1: two integers R and C.
Next R lines: each has C space-separated integers (one grid row).
Next line: an integer q.
Next q lines: four integers r1 c1 r2 c2.
Print q lines. Line i is a single integer: the rectangle sum for query i.
Example 1
Input
3 3 1 2 3 4 5 6 7 8 9 2 1 1 2 2 2 2 3 3
Expected
12 28
Explanation
First rectangle covers 1,2,4,5 = 12. Second covers 5,6,8,9 = 28.
Example 2
Input
1 1 42 1 1 1 1 1
Expected
42
Explanation
The only rectangle is the single plot, so the sum is 42.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →