Small-to-Large Merging

You know how to process subtrees. Now merge results from children using small-to-large merging.

40 lessons
118 min

Lessons

1. Intro

(The merging problem)

3m

2. The Merging Problem

(Combining child results)

3m

3. Naive Merging

(Why it fails)

3m

4. Worst Case: Chain Tree

(O(n²) disaster)

3m

5. The Core idea

(Merge small into large)

3m

6. Why O(n log n) Works

(Doubling argument)

3m

7. Implementation Pattern

(DSU on tree structure)

3m

8. Code Structure

(High-level template)

3m

9. Tracking Subtree Sizes

(First DFS pass)

3m

10. Reusing the Largest Set

(Memory optimization)

3m

11. Problem - Distinct Colors

(CSES 1139)

3m1 problems

12. Distinct Colors - Naive Approach

(Why it times out)

3m

13. Distinct Colors - Algorithm

(Small-to-large merging)

3m

14. Distinct Colors - Implementation

(C++ with sets)

3m1 problems

15. Distinct Colors - Walkthrough

(Example execution)

3m

16. Distinct Colors - Lessons

(What you learned)

3m

17. Problem - Lomsat gelral

(Codeforces 600E)

3m1 problems

18. Lomsat gelral - Observation

(Frequency tracking)

3m

19. Lomsat gelral - Algorithm

(Small-to-large on maps)

3m

20. Lomsat gelral - Maintaining the Max

(Tracking maximum frequency)

3m

21. Lomsat gelral - Implementation

(C++ with maps)

3m1 problems

22. Lomsat gelral - Walkthrough

(Example execution)

3m

23. Lomsat gelral - Lessons

(What you learned)

3m

24. When to Use Small-to-Large

(Pattern recognition)

3m

25. Alternatives to Small-to-Large

(Other techniques)

3m

26. Memory Considerations

(Space complexity)

3m

27. Connection to Heavy-Light Decomposition

(Heavy child concept)

3m

28. Implementation Tips

(Avoiding bugs)

3m

29. Debugging Small-to-Large

(Common mistakes)

3m

30. Quiz: Small-to-Large Complexity

Knowledge check

2m1 problems

31. Quiz: Small-to-Large Use

Knowledge check

2m1 problems

32. Variations of the Technique

(Beyond basic merging)

3m

33. Practice Tips

(Building intuition)

3m

34. Common Problem Variations

(What to expect)

3m

35. Online vs Offline

(Query handling)

3m

36. Space Improvement

(Reducing memory usage)

3m

37. Constant Factors

(Practical performance)

3m

38. Combining with Other Techniques

(Advanced applications)

3m

39. Historical Note

(DSU on tree origin)

3m

40. Section Recap

(What you learned)

3m

Practice Problems

1.
2.
Lomsat gelralCodeforceshard
3.
Tree RequestsCodeforceshard
4.
Dominant IndicesCodeforceshard

Ready to start learning?

Access all 40 lessons with interactive content and progress tracking.