Why it matters

Sqrt decomposition is simple + effective. Understanding enables competitive programming.

Advertisement

The architecture

Divide n elements into √n buckets.

Precompute per-bucket aggregate.

Query: partial + full buckets.

Sqrt decomposition√n bucketseach √n elementsBucket aggregateprecomputedQueryO(√n) timeAlternative to segment tree; simpler code, similar log factor slower
Sqrt decomposition.
Advertisement

How it works end to end

Range sum: partial buckets at boundaries + full bucket sums.

Point update: update element + bucket aggregate.

Applications: many query types not just sum.