Topics
The concept of partial sums extends naturally to two-dimensional arrays (matrices). A 2D partial sum matrix PS
for a matrix M
is defined such that PS[i][j]
stores the sum of all elements in the sub-rectangle from M[0][0]
to M[i][j]
. Constructing the prefix matrix as well as doing range sum query leverages (inclusion-exclusion principle):
We want the sum of the rectangle up to (i, j)
. We can achieve this by:
- Taking the sum of the rectangle up to
(i-1, j)
:PS[i-1][j]
- Taking the sum of the rectangle up to
(i, j-1)
:PS[i][j-1]
- Notice that the rectangle up to
(i-1, j-1)
has been added twice (once in step 1 and once in step 2). We need to subtract it once:- PS[i-1][j-1]
- Finally, add the current element
M[i][j]
Thus, formula is: PS[i][j] = PS[i-1][j] + PS[i][j-1] - PS[i-1][j-1]
Once prefix sum array is constructed, we are usually asked to answer range queries (for sums). We have the formula as:
SumRectangle(r1, c1, r2, c2) = PS[r2][c2] - PS[r1-1][c2] - PS[r2][c1-1] + PS[r1-1][c1-1]
Explanation:
PS[r2][c2]
includes the sum of the entire rectangle from(0, 0)
to(r2, c2)
.PS[r1-1][c2]
(ifr1 > 0
) removes the sum of the rectangle above our target rectangle (from(0, 0)
to(r1-1, c2)
).PS[r2][c1-1]
(ifc1 > 0
) removes the sum of the rectangle to the left of our target rectangle (from(0, 0)
to(r2, c1-1)
).PS[r1-1][c1-1]
(ifr1 > 0
andc1 > 0
) was subtracted twice (once in step 2 and once in step 3), so we need to add it back to correct for the over-subtraction. This rectangle represents the area that was removed twice.