Subtree Queries Hot Sheet¶
Use this page when the tree is static, the query touches one whole subtree, and you want to flatten that subtree into one interval before choosing a range structure.
Do Not Use When¶
- the query is on an arbitrary path
u -> v, not one subtree - the real work is combining child states rather than querying a flattened interval
- you only need ancestor checks, so
tin/toutalone is enough - the tree itself changes online
Choose By Signal¶
- point update on one node + subtree sum/count -> Euler flattening ->
euler-tour-subtree.cpp - subtree-only query but richer range aggregate -> Euler flattening + Segment Tree hot sheet
- arbitrary path aggregate -> HLD hot sheet
- child-merge optimization -> DP cheatsheet or Tree DP
- ancestor yes/no ->
tin/toutor LCA, usually no Fenwick/segment tree needed
Core Invariants¶
- this lane uses a single-entry DFS order, not the full Euler tour
tin[u]is the flattened position of nodeu- the subtree of
uis exactly the half-open interval[tin[u], tout[u]) tout[u] - tin[u]equals the subtree size- node-value update on
ubecomes one point update attin[u]
Main Traps¶
- confusing subtree flattening with the full Euler tour used for some LCA reductions
- mixing inclusive subtree ranges from the statement with the half-open internal interval
[tin[u], tout[u]) - reaching for HLD when the query is subtree-only
- forgetting that point assignment on the original tree becomes a point delta update on Fenwick
Exact Starters In This Repo¶
- exact starter ->
euler-tour-subtree.cpp - flagship in-lane rep -> Subtree Queries
- tree worldview refresher -> Trees
- lighter range engine refresher -> Fenwick hot sheet
- richer range engine compare point -> Segment Tree hot sheet
Reopen Paths¶
- full lesson and path-vs-subtree chooser -> Euler Tour / Subtree Queries
- rooted-tree worldview first -> Trees
- richer interval engine -> Segment Tree
- arbitrary path queries -> Heavy-Light Decomposition
- family chooser -> Graph cheatsheet
- snippet chooser -> Template Library