Data Structures19 sections · 729 units
Open in Course

Problem - LCA Queries

O(1) LCA with preprocessing

Given a tree and qq queries, each asking for the LCA of two nodes uu and vv.

Using Euler tour + Sparse Table:

  • Preprocess: O(nlog⁡n)O(n \log n)
  • Each query: O(1)O(1)

Alternative: Binary lifting gives O(nlog⁡n)O(n \log n) preprocess, O(log⁡n)O(\log n) query.

Better for smaller qq, worse for large qq. Constraints: nn up to 10510^5, qq up to 10610^6.