How it works
How it works
Build an array where each entry holds the sum of all earlier elements, then subtract endpoints for a range.
Two dimensions
A 2D prefix sum answers submatrix-sum queries in constant time using inclusion and exclusion.
Related patterns
Prefix XOR, prefix counts, and difference arrays extend the same idea.