Graph Theory37 sections · 1633 units
Open in Course

Problem - Subtree Queries

(CSES 1137)

You are given a tree of nn nodes. Each node has a value. Two types of queries: update a node's value, or ask for the sum of all values in a node's subtree. Brute force: traverse the subtree for every query. That is O(n)O(n) per query, too slow for n2105n \le 2 \cdot 10^5 and q2105q \le 2 \cdot 10^5 queries.

Euler tour converts this into a range query problem. The range representation means you can use efficient range query structures to answer subtree questions in logarithmic time.

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