Graph Theory37 sections · 1633 units
Open in Course

The Path Query Problem

(Maximum on a path)

You are given a tree with nn nodes. Each node has a value val[i]. You need to handle two operations: query the maximum value on the path from node uu to node vv, and update the value of node ii to xx. If you walk the path for each query, visiting each node along the way, you get O(n)O(n) per query.

With q=100000q = 100000 queries and n=100000n = 100000 nodes, that is 101010^{10} operations, which times out. You need a faster approach that does not visit every node on the path. This is where Heavy-Light Decomposition comes in.

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