Graph Theory37 sections · 1633 units
Open in Course

QTREE - Complexity

(Same as vertex version)

Preprocessing takes O(n)O(n) to build the tree structure and perform HLD decomposition. Building the segment tree takes O(n)O(n). Each QUERY walks O(log⁡n)O(\log n) chains and does O(log⁡n)O(\log n) work per chain via segment tree queries, giving O(log⁡2n)O(\log^2 n) per query.

Each CHANGE is a single segment tree point update, taking O(log⁡n)O(\log n) time. Total time: O(n+qlog⁡2n)O(n + q \log^2 n), which handles the constraints comfortably.

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