Purpose: Answer Lowest Common Ancestor queries in after preprocessing — no anywhere. Also known as the ±1 RMQ or four Russians method.
The Reduction Chain
- LCA → RMQ. Take an Euler tour of the tree, recording the depth at each of the visits. is the shallowest node in the tour segment between the first occurrences of and — a range minimum query.
- The array is ±1. Consecutive Euler-tour depths differ by exactly , because each step of the tour moves to a parent or a child. This restriction is what buys the constant time.
The Block Trick
Let and block size .
- Between blocks. Compute the minimum of each block ( values) and build a sparse table over those. Cost: . Any query spanning whole blocks is answered in .
- Inside blocks. A block of length in a ±1 array is fully determined by its signs, so there are only distinct block types. Precompute, for every type, the answer to every one of the internal query ranges. Total .
- A query splits into a suffix of one block, some whole blocks, and a prefix of another — three lookups.
Complexity
- Preprocessing: time and space
- Query:
- Constant factor: large. Expect 5-10× the constant of a sparse table.
Comparison of LCA methods
| Method | Preprocess | Query | Notes |
|---|---|---|---|
| Naive climbing | fine for tiny trees | ||
| Binary lifting | the contest default — short, flexible, supports path queries | ||
| Euler tour + sparse table | ~15 lines, excellent constant | ||
| Tarjan offline | amortized | needs all queries up front | |
| Farach-Colton-Bender | asymptotically optimal, big constant | ||
| Schieber-Vishkin | also optimal, bit-trick based |
What to actually write
Euler tour + sparse table. It is preprocessing, but with a tiny constant beats with a huge one for every you will meet in a contest. Reach for binary lifting if you also need -th ancestor or path aggregates.
Why “four Russians”
The technique of precomputing answers for all possible small blocks is named after Arlazarov, Dinic, Kronrod and Faradzhev, who used it to speed up boolean matrix multiplication. It appears throughout: bitset DP, the transitive closure, and precomputed popcount tables are all the same idea.
Variants / Use Cases
- Generic RMQ (not ±1) — reduce to LCA via a Cartesian tree, then apply this; still
- Lowest Common Ancestor — the topic page
- Range queries on trees — subtree sums, path queries via Euler tour + BIT
- Schieber-Vishkin — the other LCA, with a smaller constant in practice