AlgoScope

2D Prefix Sum

AlgorithmintermediateRange Queries

P[i][j] = a + P[i-1][j] + P[i][j-1] - P[i-1][j-1]; any rectangle sum in O(1).

Decision · step 2 of 14Matrix: 2D prefix sum
0123401230!00✓0✓0✓0300

p[1][1]: own value 3, plus above 0, plus left 0, minus the diagonal 0 that both of those already include: 3.

Open in the player →or start at step 2

What you will see

The inclusion-exclusion corners light up: plus, minus, minus, plus.

How matrix works →

Cost

BestO(r * c)
AverageO(r * c)
WorstO(r * c)
SpaceO(r * c)

O(1) per query after building.

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, try operations in any order.

Screen readers: Structure nodes announce the range they cover and their value; each step announces which nodes answer the query.

Reduced motion: Contributing nodes highlight statically; no path animation.

Before this

Leads to

Topics that need this one first.

Taught by the same lesson

Matrix covers these too, in the same run.