Graph Theory37 sections · 1633 units
Open in Course

Complexity Analysis

(Time and space bounds)

Preprocessing takes O(n)O(n) to compute sizes and depths, O(n)O(n) to decompose into chains, and O(n)O(n) to build the segment tree. Total preprocessing: O(n)O(n). Each query walks O(logn)O(\log n) chains and does O(logn)O(\log n) work per chain via segment tree queries.

Total per query: O(log2n)O(\log^2 n). With qq queries, total time is O(n+qlog2n)O(n + q \log^2 n). Space is O(n)O(n) for arrays and the segment tree. This handles n,q=200000n, q = 200000 easily within time limits.

Space complexity is O(n)O(n) for the data structures used.