Graph Theory37 sections · 1633 units
Open in Course

Binary Lifting for Successors

(Jumping in powers of 2)

To find fk(x)f^k(x) for large kk, you cannot simulate kk steps. That takes O(k)O(k) time. If k=109k = 10^9, you time out.

Instead, precompute f2i(x)f^{2^i}(x) for all ii and all xx. You can reach any kk-th successor by combining powers of 22. This is binary decomposition. This is binary lifting. It reduces O(k)O(k) to O(log⁡k)O(\log k) per query after O(nlog⁡n)O(n \log n) preprocessing. The preprocessing pays off when you have multiple queries.

Space complexity is O(nlog⁡n)O(n \log n) for the data structures used.