Graph Theory37 sections · 1633 units
Open in Course

Path Query Algorithm

(Walk up to LCA)

To query the path from uu to vv, use this algorithm: While uu and vv are in different chains, move the deeper one up. Query the segment from the deeper node to its chain head. Then jump to the parent of the chain head. When both nodes are finally in the same chain, query the segment between them.

This processes O(logn)O(\log n) chain segments. Each segment tree query takes O(logn)O(\log n) time, so the total time is O(log2n)O(\log^2 n) per path query. You maintain the maximum value seen across all queried segments.

Space complexity is O(n)O(n) for the data structures used.