Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.

Sign in to watch this lesson
Advanced - Data Structures and Range Queries · 4:03
When every range query is known in advance and the answer is easy to update one element at a time but impossible to merge (distinct values, the top frequency, equal pairs), Mo's algorithm answers them all by moving a single window. Sorting queries by (l / S, r) with S around √n, and snaking r through alternate strips, keeps the total movement at O((n + Q)√n) add/remove calls. The window must expand before it shrinks, the block size should be a constant, and answers go back to their original indices.