Graph Theory37 sections · 1633 units
Open in Course

Core Idea - Preprocessing Once

(Amortizing the cost)

Building the jump table takes O(nlogn)O(n \log n) time. With qq queries, total time is O(nlogn+qlogk)O(n \log n + q \log k). The preprocessing is done once, then you answer all queries. If you simulate each query, total time is O(qk)O(qk). When kk is large and qq is large, simulation times out.

Binary lifting is the only way. Preprocessing pays off when you have multiple queries. The first query might feel expensive, but amortized cost per query is O(logk)O(\log k). This amortization is common in competitive programming.