Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 43s1 days 16 hours 1 minutes 43 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 43s1 days 16 hours 1 minutes 43 seconds.

Sign in to watch this lesson
Advanced - Trees · 4:52
Every vertex marks the child with the largest subtree as heavy; a light child has at most half its parent's subtree, so any root path crosses at most log n light edges and touches at most log n heavy chains. A DFS that visits the heavy child first lays every chain, and every subtree, out as a contiguous block of one array, so one segment tree serves them all. A path query repeatedly lifts the chain with the deeper head, one range per chain, for O(log^2 n) per query; edge values are stored at the lower endpoint and the LCA's slot is skipped.