What if you have a matrix instead of an array? Can you answer submatrix sum queries in ? Yes. The same idea applies, but you need inclusion-exclusion to avoid double-counting. For a 2D prefix sum, stores the sum of all elements in the rectangle from to .
Data Structures19 sections · 729 units
Open in CourseExtending to 2D
Matrix range sums