Graph Theory37 sections · 1633 units
Open in Course

Path Queries II - Complexity

(Per-query analysis)

Preprocessing takes O(n)O(n) for DFS to compute sizes, depths, and parents. HLD decomposition takes O(n)O(n). Building the segment tree takes O(n)O(n). Total preprocessing: O(n)O(n). Each path query walks O(logn)O(\log n) chains and queries O(logn)O(\log n) segment tree nodes per chain, giving O(log2n)O(\log^2 n) per query.

Each update is O(logn)O(\log n). With qq queries, total time is O(n+qlog2n)O(n + q \log^2 n), which handles n,q=200000n, q = 200000 easily within the 11 second time limit.

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