AlgoScope

Difference Matrix

AlgorithmadvancedRange Queries

Four corner writes per rectangle, then a prefix pass along rows and down columns.

Decision · step 2 of 16Difference Array: Two overlapping rectangles
012345stop0123stop05✓00-5!00000000000000000-5!005✓000000000

Rectangle rows 0 to 2, columns 1 to 3, +5. Four writes: +5 at (0,1) starts it; -5 at (0,4) stops it past the right edge; -5 at (3,1) stops it below the bottom; +5 at (3,4) cancels the corner that both stops would hit.

Open in the player →or start at step 2

What you will see

Four corners receive plus and minus marks; the prefix pass reveals the final grid.

How difference array works →

Cost

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

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.

Taught by the same lesson

Difference Array covers these too, in the same run.