Graph Theory37 sections · 1633 units
Open in Course

Comparison to Naive Approach

(Why binary lifting wins)

Naive approach: O(1)O(1) preprocessing, O(k)O(k) per query. For qq queries with knk \approx n, that is O(qn)O(qn) total. Binary lifting: O(nlogn)O(n \log n) preprocessing, O(logk)O(\log k) per query. Total: O(nlogn+qlogn)O(n \log n + q \log n). If qq is large, binary lifting is orders of magnitude faster. For n=100,000n = 100,000 and q=1,000,000q = 1,000,000, naive takes billions of operations.

Binary lifting takes millions. That is the difference between timing out and passing comfortably.