Two-Dimensional Prefix Sum Template
To efficiently compute the sum of elements in any submatrix of a given 2D grid, we use a two-dimensional prefix sum technique. This approach allows each query to be anwsered in constant time after an initial preprocessing step.
Core Idea
We constrcut a prefix sum matrix pref where pref[i][j] represents the sum of all elements in the rectangle f ...
Posted on Tue, 13 Oct 2026 16:35:23 +0000 by joozt