Skip to content

Order Statistics Tree Hot Sheet

Use this sheet when one dynamic ordered set needs rank or k-th queries online, not just predecessor or successor.

Do Not Use When

Choose By Signal

  • dynamic set + k-th smallest / count smaller than x -> PBDS / Order Statistics Tree
  • predecessor / floor in active multiset -> multiset
  • compressed frequencies with rank counts -> Fenwick hot sheet
  • static subarray order statistics -> Wavelet Tree hot sheet

Core Invariants

  • the tree stores all live keys in sorted order
  • order statistics come from subtree sizes, surfaced as order_of_key() and find_by_order()
  • raw ordered_set<T> is a unique-key set
  • duplicates need a pair-key wrapper such as (value, unique_id)

Main Traps

  • forgetting PBDS is a GNU extension, not portable ISO C++
  • using one-based k directly with zero-based find_by_order()
  • reaching for PBDS when multiset or Fenwick is simpler
  • trying to store duplicates in raw ordered_set<T> and silently losing them

Exact Starter Route

Reopen Paths