Why it matters

Mo's algorithm enables efficient batch range query processing. Competitive standard.

Advertisement

The architecture

Sort queries by (bucket of L, R). Bucket size √n.

Move L, R incrementally; update count.

Mo's algorithmSort queriesby (bucket_L, R)Move windowL, R adjustmentsAnswer eachwith current stateOnly works offline: queries known ahead of time
Mo's mechanics.
Advertisement

How it works end to end

Total moves: O((n+q)√n) amortized.

Requires: online 'add/remove element' operations.

Extensions: Mo's on tree, with updates.