Graph Theory37 sections · 1633 units
Open in Course

Segment Tree per Chain

(Range queries on chains)

Build one segment tree for each chain. The segment tree for chain cc has size equal to the number of nodes in that chain. Position ii in the segment tree stores the value of the node with pos[v] = i in chain cc. You can query the maximum in a range [l,r][l, r] in O(logn)O(\log n) time using standard segment tree queries.

Since the tree has nn nodes total across all chains, the total space is O(n)O(n) for all segment trees combined. Each node belongs to exactly one chain, so the segment tree sizes add up to nn.

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