Wavelet Tree Hot Sheet¶
Use this page when one static array gets many subarray queries whose answer depends on value order inside the range:
- k-th smallest
- count
<= x - count
== x
Do Not Use When¶
- updates happen between queries
- the task is just static RMQ / min / gcd, so Sparse Table hot sheet already fits
- the real invariant is one active interval maintained by endpoint add/remove, so Mo's hot sheet is the better lens
- the task is about versions / snapshots, so Persistent Data Structures hot sheet is the right compare point
Choose By Signal¶
- static RMQ / idempotent aggregate -> Sparse Table hot sheet
- one active window with cheap add/remove -> Mo's hot sheet
- old versions or prefix roots are the natural story -> Persistent Data Structures hot sheet
- static subarray k-th / value counting by rank -> Wavelet Tree
Core Invariants¶
- each node owns one compressed value interval
[lo, hi] - each node stores the subsequence of array elements whose values lie in that interval, with original order preserved
pref[i]means: among the firstisubsequence elements at this node, how many go to the left child- for a node-local query interval
[l, r), the child ranges become: - left ->
[pref[l], pref[r)) - right ->
[l - pref[l], r - pref[r))
Main Query Rules¶
kth_smallest: compute how many queried elements go left; go left ifk < left_count, otherwise go right withk -= left_countcount_leq(x): prune whole value intervals that are fully above or fully belowxcount_equal(x): follow only the unique leaf path forx
Main Traps¶
- mixing original-array positions with node-local subsequence positions after the first descent
- forgetting to convert statement
kfrom one-based to zero-based - skipping coordinate compression when values are large
- stretching the starter to dynamic updates or wavelet-matrix-only variants
Exact Starter In This Repo¶
- exact starter -> wavelet-tree.cpp
- flagship static k-th rep -> MKTHNUM - K-th Number
- best compare point -> Persistent Data Structures
- lighter static route when value order does not matter -> Sparse Table hot sheet
Reopen Paths¶
- full tutorial -> Wavelet Tree
- compare against versions / prefix roots -> Persistent Data Structures hot sheet
- compare against current-window offline maintenance -> Mo's hot sheet
- parent chooser -> Data structures cheatsheet
- snippet chooser -> Template Library