teacher
Sign in

Sqrt Decomposition and Mo's Algorithm

Split the array into blocks of about sqrt(n) to balance update and query costs, and reorder offline range queries so two moving pointers do little work in total.

45 minCF 1800–24006 interactive9 problems

The problem

You have an array of nn numbers and qq queries. Two shapes appear again and again.

  • Online with updates. "Change one element" and "sum of a[l..r]a[l..r]", mixed in any order.
  • Offline, no updates. "How many distinct values are in a[l..r]a[l..r]?" You may read all queries first.
1 of 7