Data Structures19 sections · 729 units
Open in Course

Path Queries with HLD

Using segment trees

To query a path from uu to vv:

1.1. Find LCA of uu and vv

2.2. Query the path from uu to LCA

3.3. Query the path from vv to LCA

For each path segment, climb chain by chain:

  • While uu and vv are in different chains, query the chain containing the deeper node, then jump to the chain head's parent
  • When in the same chain, query the range between them

Each chain query is a segment tree range query. At most O(logn)O(\log n) chains are visited, so total time is O(log2n)O(\log^2 n).