Data Structures19 sections · 729 units
Open in Course

Problem - 2D Range Sum

Matrix queries

Given a 2D matrix, handle multiple queries asking for the sum of elements inside a rectangle defined by its upper left corner (row1,col1)(row1, col1) and lower right corner (row2,col2)(row2, col2).

This extends the 1D prefix sum to two dimensions. The same trade-off applies: O(nm)O(nm) preprocessing for O(1)O(1) queries. Read the problem. Notice there can be 10410^4 queries on a 200×200200 \times 200 matrix.