Why it matters

Online LCA needed when queries not known upfront.

Advertisement

The architecture

Euler tour: DFS in-out sequence.

Depth array.

Sparse table for RMQ.

Online LCA structureEuler tourDFS visitsDepth arrayat each positionSparse tableO(1) RMQLCA(u,v) = shallowest node between first-visit(u) and first-visit(v)
Online LCA components.
Advertisement

How it works end to end

LCA = min-depth node between first visits.

Sparse table: O(n log n) prep.

Query: O(1) using ⟨±1⟩ RMQ.