Data Structures19 sections · 729 units
Open in Course

Subtree Queries with Euler Tour

Range operations

With Euler tour, subtree operations become range operations:

Subtree sum of v: query range [tin[v],tout[v]][tin[v], tout[v]]

Update node v: update position tin[v]tin[v]

Add x to all nodes in subtree of v: range update [tin[v],tout[v]][tin[v], tout[v]]

Build a segment tree on the tour array.

Each node's value is stored at its tintin position. Time: O(n)O(n) for tour construction, O(logn)O(\log n) per query/update. Space: O(n)O(n).