Loading repovive.com/problems/classics/37
Given an n×m grid of integers and q queries, answer each query asking for the sum of elements in a submatrix.
Each query specifies the top-left corner (r1,c1) and bottom-right corner (r2,c2) of the submatrix (1-indexed).
3 3 3 1 2 3 4 5 6 7 8 9 1 1 2 2 1 1 3 3 2 2 3 3
The first query covers 1, 2, 4, and 5, whose sum is 12. The second query covers the whole grid, whose sum is 45. The third query covers 5, 6, 8, and 9, whose sum is 28.
12 45 28
2 2 3 5 3 2 8 1 1 1 1 2 2 2 2 1 1 2 2
The first two queries ask for single cells with values 5 and 8. The last query asks for the whole grid, so 5 + 3 + 2 + 8 = 18.
5 8 18
3 4 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 1 1 3 1 1 1 3 4
Every grid value is 1. The first query covers one row of 4 cells, the second covers one column of 3 cells, and the last covers all 12 cells.
4 3 12