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(nlogn)O(n \log n)
  • Each query: O(1)O(1)

Alternative: Binary lifting gives O(nlogn)O(n \log n) preprocess, O(logn)O(\log n) query.

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