Sqrt Decomposition

You've learned tree structures. Sqrt decomposition divides arrays into blocks for O(√n) queries.

42 lessons
129 min

Lessons

1. Intro

Why √n matters

3m

2. The Core Idea

Dividing into blocks

3m

3. Vocabulary - Block Size

Choosing B

3m

4. Quiz: Block Index

Which block contains i?

2m1 problems

5. Building Block Sums

Preprocessing

3m

6. Range Sum Query

Combining blocks

4m

7. Range Sum - Implementation

The code

4m

8. Point Updates

Updating one element

3m

9. Point Update - Implementation

The code

2m

10. Problem - Range Sum Queries I

Basic sqrt decomposition

3m1 problems

11. Range Sum I - Analysis

Complexity comparison

3m

12. Range Sum I - Implementation

Complete solution

4m1 problems

13. Lessons from Range Sum

summary

2m

14. Range Minimum Query

Min instead of sum

3m

15. Range Min - Update Challenge

Rebuilding blocks

3m

16. Quiz: Update Complexity

Sum vs min updates

2m1 problems

17. When to Use Sqrt Decomposition

Tradeoffs

3m

18. MO's Algorithm - Introduction

Offline query magic

4m

19. MO's Sorting Order

The magic ordering

4m

20. MO's Algorithm - Framework

The template

4m

21. Problem - Distinct Values

Count unique elements

3m1 problems

22. Distinct Values - Add/Remove

Tracking counts

3m

23. Distinct Values - Implementation

Complete solution

4m1 problems

24. Lessons from Distinct Values

summary

2m

25. MO's with Expensive Operations

When add/remove costs more

3m

26. Problem - Mode Query

Most frequent element

3m1 problems

27. Mode Query - Data Structures

Tracking frequencies

4m

28. Mode Query - Implementation

The code

4m1 problems

29. Lessons from Mode Query

summary

2m

30. MO's Optimization - Zigzag Order

Reducing constant factor

3m

31. Quiz: MO's Complexity

Understanding the analysis

2m1 problems

32. Block Decomposition for Updates

Range updates too

3m

33. Lazy Block Updates

Implementation details

4m

34. Problem - Polynomial Queries

Harder range updates

3m1 problems

35. Polynomial Queries - Analysis

Arithmetic progression sums

4m

36. Polynomial Queries - Pushdown

When tails need updates

3m1 problems

37. Lessons from Polynomial Queries

summary

2m

38. MO's with Updates

Time as third dimension

4m

39. MO's with Updates - Analysis

Why n^(5/3)?

3m

40. Comparing Sqrt to Other Structures

When to choose what

3m

41. Challenge: Design Your Solution

Practice choosing techniques

3m

42. Section Recap

What you've learned

2m

Practice Problems

1.
Powerful arrayCodeforcesmedium

The quintessential Mo's algorithm problem. Teaches fundamental add/remove operations and proper block size selection to avoid TLE.

2.
XOR and Favorite NumberCodeforcesmedium

Brilliant combination of prefix XOR with Mo's algorithm. Demonstrates how to transform range queries into Mo's algorithm format.

3.

Classic Mo's algorithm counting elements x that occur exactly x times. Great for beginners learning Mo's algorithm basics.

4.
Tree and QueriesCodeforceshard

Mo's algorithm on trees using Euler tour transformation. Teaches how to flatten trees into arrays for Mo's algorithm.

5.
HolesCodeforcesmedium

Pure sqrt decomposition without Mo's algorithm. Excellent for understanding block-based updates and jump operations.

6.
Serega and FunCodeforceshard

Advanced sqrt decomposition with cyclic shifts. Demonstrates lazy propagation concepts and block maintenance with deques.

7.
Xenia and TreeCodeforceshard

Sqrt optimization on trees with BFS preprocessing every √m queries. Teaches query batching techniques for maintaining O(q√n) complexity.

8.
Subset SumsCodeforceshard

Introduces heavy-light decomposition for sets. Splits sets into 'heavy' and 'light' demonstrating threshold-based sqrt decomposition.

9.

Sqrt decomposition with lazy propagation for Fibonacci updates. Combines mathematical properties with block-based updates.

10.
DZY Loves ColorsCodeforceshard

Range update and range query with sqrt decomposition. Teaches lazy propagation on blocks tracking colorfulness changes.

11.
Instant MessengerCodeforcesmedium

Graph-based sqrt decomposition splitting nodes by degree. Demonstrates applying sqrt decomposition beyond arrays.

12.

Sqrt decomposition for connectivity queries across colored edges. Teaches handling multiple dimensions with sqrt techniques.

13.

Foundational sqrt decomposition problem. Perfect introduction teaching how to split arrays into √n blocks with precomputed sums.

14.

Solvable with sqrt decomposition maintaining block-level majority candidates. Demonstrates advanced aggregation beyond simple sums.

15.

Sqrt decomposition for 2D range queries. Shows how to extend 1D block decomposition to 2D problems efficiently.

16.

Range flip operations with sqrt decomposition and lazy updates. Teaches handling bit flips in blocks with pending XOR operations.

17.
Range ModuleLeetCodehard

Interval system solvable with sqrt decomposition. Shows maintaining interval coverage using blocks instead of trees.

18.

Dynamic interval merging with sqrt decomposition. Demonstrates maintaining distinct counts across overlapping intervals.

19.
Falling SquaresLeetCodehard

Coordinate compression with sqrt decomposition for interval maximum queries. Handles large coordinate spaces with manageable blocks.

20.

Interval painting with overlap detection. Combines range updates with jump pointers demonstrating path compression in blocks.

Ready to start learning?

Access all 42 lessons with interactive content and progress tracking.