Graph Theory37 sections · 1633 units
Open in Course

Binary Lifting - Complexity

(Preprocessing and query time)

Binary lifting preprocessing takes O(nlogn)O(n \log n) time and space. You fill a table of nn rows and logn\log n columns, where each entry takes O(1)O(1). A single DFS computes all depths in O(n)O(n) time. Each LCA query takes O(logn)O(\log n) time because you iterate through at most logn\log n powers of 22 twice: once to equalize depths, once to climb together.

For qq queries, total time is O(nlogn+qlogn)O(n \log n + q \log n). Space is O(nlogn)O(n \log n) for the up table. This is fast enough for most competitive programming problems with n,q2×105n, q \leq 2 \times 10^5. The main bottleneck is the logn\log n factor per query, which Euler tour + RMQ can eliminate.