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

Sign in to watch this lesson
Advanced - Trees · 3:56
Write an edge into a tour every time a depth-first walk goes down it and again when it comes back up, so each edge appears exactly twice. Between pos[a] and pos[j] for an ancestor a of j, every side trip appears as a matched down-up pair and cancels, leaving exactly the path's edges going down; two Fenwick trees (painted down tabs and painted up tabs) count painted edges on the path as a difference. A general path splits at the LCA, and painting or unpainting an edge updates both trees, all in O(log n).