Heavy-Light Decomposition

You know Euler tours for subtrees. HLD handles path queries by decomposing into heavy chains.

49 lessons
144 min

Lessons

1. Intro

(Solving path queries fast)

3m

2. The Path Query Problem

(Maximum on a path)

3m

3. Why Subtree Tricks Fail

Paths cross subtree boundaries

3m

4. The Chain Idea

(Break paths into chains)

3m

5. Subtree Size

(Counting descendants)

3m

6. Heavy and Light Edges

(Splitting by subtree size)

3m

7. Heavy Paths as Chains

(Maximal heavy sequences)

3m

8. The $O(\log n)$ Chain Property

(Why paths cross few chains)

3m

9. Path to LCA Structure

(Two paths meet at ancestor)

3m

10. Chain Decomposition DFS

(Assigning nodes to chains)

3m

11. Chain Positions

(Indexing nodes within chains)

3m

12. Chain Heads

(Top node of each chain)

3m

13. Segment Tree per Chain

(Range queries on chains)

3m

14. Single Segment Tree Improvement

(Flatten all chains)

3m

15. Path Query Algorithm

(Walk up to LCA)

3m

16. Depth Array

(Comparing node depths)

3m

17. Parent Array

(Jumping to parent)

3m

18. Update Operation

(Changing a node value)

3m

19. Edge Queries vs Vertex Queries

(Where to store values)

3m

20. LCA in HLD

(Finding the meeting point)

3m

21. Complexity Analysis

(Time and space bounds)

3m

22. Implementation - Preprocessing

(Computing sizes and parents)

3m

23. Implementation - Decomposition

(Building chains)

3m

24. Implementation - Segment Tree

(Supporting range max)

3m

25. Implementation - Path Query

(Combining chain segments)

3m

26. Path Updates

(Modifying all nodes on path)

3m

27. Path Sum Queries

(Sum instead of maximum)

3m

28. Problems - Path Queries II - Read Statement

(CSES 1138 path maximum)

3m1 problems

29. Path Queries II - Heavy Edges

(Decompose the tree)

3m

30. Path Queries II - Segment Tree

(Store values by position)

3m

31. Path Queries II - Query Algorithm

(Walk up both sides)

3m

32. Path Queries II - Update Algorithm

(Change one node)

3m

33. Path Queries II - Complexity

(Per-query analysis)

3m

34. Path Queries II - Implementation

(Pseudocode)

3m1 problems

35. Path Queries II - Lessons

(What you learned)

3m

36. Problems - QTREE - Read Statement

(SPOJ classic HLD problem)

3m1 problems

37. QTREE - Edge to Vertex Mapping

(Store edge weights at nodes)

3m

38. QTREE - Query Path Edges

(Exclude the LCA)

3m

39. QTREE - Edge Update

Change edge $i$ to weight $ti$

3m

40. QTREE - Complexity

(Same as vertex version)

3m

41. QTREE - Implementation Notes

(Edge indexing)

3m1 problems

42. QTREE - Lessons

(Edge queries via HLD)

3m

43. When to Use HLD

(Path queries on trees)

3m

44. HLD vs LCA Binary Lifting

(Updates vs queries)

3m

45. Common Mistakes

(Pitfalls to avoid)

3m

46. Quiz: Heavy Edge Definition

Knowledge check

2m1 problems

47. Quiz: Chain Crossing Bound

Knowledge check

2m1 problems

48. Quiz: Query Complexity

Knowledge check

2m1 problems

49. Section Recap

(What you learned)

3m

Practice Problems

1.
2.
QTREESPOJhard
3.
Xenia and TreeCodeforceshard
4.
Tree and QueriesCodeforceshard

Ready to start learning?

Access all 49 lessons with interactive content and progress tracking.