Technique
Binary Lifting & LCA
Precompute up[k][v] = 2k-th ancestor (or successor) so k-th ancestor, LCA, and path aggregates take O(log n).
0/19 solved
Recognise it
The signal
many LCA / distance / k-th ancestor queries
1Brute force
Walk parent pointers one by one: O(depth) per query.
2The observation
Linear walks: any jump length is a sum of powers of two.
3Why it works
up[k][v] = up[k-1][up[k-1][v]].
Reach for it when
- +successor after k steps in a functional graph
- +path min/max on a static tree
Not when
- ×path updates (hld + seg-tree)
- ×tree changes (link-cut trees)
Practice
- New Roads QueriesCSESMedium
- Cyclic ArrayCSESMedium
- Distance QueriesCSESMedium
- Company Queries IICSESMedium
- Company Queries ICSESMedium
- Planets Queries ICSESMedium
- Counting PathsCSESMedium
- Qpwoeirut and VerticesCodeforces2300
- Kingdom and its CitiesCodeforces2800
- Count PathsCodeforces2000
- Forbidden CitiesCSESHard
- Movie Festival QueriesCSESHard
Pitfalls
Misconceptions
- !forgetting to lift the deeper node to equal depth first
Go deepervariants and techniques · 7 combinations · resources
Combinations
Binary Lifting & LCA+Tree DFS (Paths & Recursion)
distance between two nodes, k-th ancestor, many queries
- Binary Lifting & LCA:
- jumps to ancestors in O(log n)
- Tree DFS (Paths & Recursion):
- computes depth and parent
Functional / Successor Graphs+Binary Lifting & LCA
teleporters/successor after k steps; first time reaching b from a
- Functional / Successor Graphs:
- every node has one successor
- Binary Lifting & LCA:
- k-step successor in O(log k)
Virtual (Auxiliary) Tree+DP on Trees+Binary Lifting & LCA
many queries with a small vertex set each and sum of k bounded
- Virtual (Auxiliary) Tree:
- shrink the tree to the O(k) marked vertices and their LCAs
- DP on Trees:
- run the separation/cover DP on the small tree
- Binary Lifting & LCA:
- LCA queries while building the virtual tree
Kruskal Reconstruction Tree+Binary Lifting & LCA
the minimum possible maximum edge between u and v (or the first time they connect) for many pairs
- Kruskal Reconstruction Tree:
- turns each union into a node valued by the edge weight
- Binary Lifting & LCA:
- LCA queries, or jumping to the highest ancestor with value <= x
Binary Lifting & LCA+Interval Scheduling (sort by end)
many queries: how many non-overlapping intervals fit inside [a, b]
- Binary Lifting & LCA:
- jump 2^k greedy steps at once
- Interval Scheduling (sort by end):
- from time t, the greedy next movie is the one ending earliest among those starting >= t
Binary Lifting & LCA+Monotonic Stack
count elements of the 'next greater' chain starting at a that lie within b
- Binary Lifting & LCA:
- count how many next-greater jumps stay within [a, b]
- Monotonic Stack:
- next greater element for every index
Block-Cut Tree / Bridge Tree+Binary Lifting & LCA
queries: can a reach b without visiting c (undirected)
- Block-Cut Tree / Bridge Tree:
- turns 'must pass through c' into a tree-path membership test
- Binary Lifting & LCA:
- LCA / ancestor checks for the path test