Data Structures Cheatsheet¶
Use this page when you know the operations but have not yet picked the lightest structure.
Do Not Use When¶
- the main difficulty is graph or DP modeling rather than operations
- one static formula or prefix array already solves the whole problem
- you still cannot list the required operations cleanly
Choose By Operations¶
- static range sums -> prefix sums
- point add + prefix/range sum -> Fenwick hot sheet
- arbitrary merge with updates -> segment tree -> Segment Tree hot sheet
- online range add + range sum -> Lazy Segment Tree hot sheet
- online range
chmin / chmax / add / sumwhere one simple lazy tag no longer closes -> Segment Tree Beats hot sheet - piecewise-constant range partitions where assign keeps collapsing runs and interval walks are acceptable -> ODT / Chtholly hot sheet
- old versions stay queryable and point updates create new versions -> Persistent Data Structures hot sheet
- self-hosted ordered set with split/merge by key, online rank, or
k-th -> Treap / Implicit Treap hot sheet - mutable sequence with insert / erase / cut / paste by position -> Treap / Implicit Treap hot sheet
- static subarray k-th / count
<= x/ count== xby value -> Wavelet Tree hot sheet - static idempotent range query -> Sparse Table hot sheet
- connectivity merges -> DSU hot sheet
- connectivity under offline edge add/remove timeline -> DSU Rollback hot sheet
- every node needs one subtree answer from merged child containers -> DSU On Tree hot sheet
- static range queries where one active interval can be updated by endpoint moves -> Mo's hot sheet
- dynamic maximum xor against one live set of integers -> Binary Trie hot sheet
- current top / min / max only -> heap
- heaps must meld online and queries name the heap containing
x-> Pairing / Leftist Heap hot sheet - predecessor / successor / erase-one with duplicates -> multiset
- online
k-th / rank / count smaller in one dynamic ordered set -> Order Statistics Tree hot sheet - high-fanout textbook ordered dictionary / explicit B-tree breadth -> B-Tree hot sheet
- textbook probabilistic ordered dictionary / forward-pointer towers -> Skiplist hot sheet
- bounded integer universe with predecessor / successor as the real job -> X-Fast / Y-Fast hot sheet
- one live set of half-open intervals with online overlap queries -> Interval Tree hot sheet
- self-adjusting ordered set with rank /
k-th, or you want the exact rotate/splay bridge into LCT -> Splay Tree hot sheet - you deliberately want a textbook deterministic balanced BST and want to know whether AVL / Red-Black / Scapegoat / SBT is actually worth it -> Balanced BST hot sheet
- first smaller / greater witness or histogram-style blocking boundary in one scan -> Monotonic Stack / Queue hot sheet
- sliding median -> sliding-median-two-multisets.cpp
- previous value
< xin active set -> multiset-predecessor.cpp - window minimum / monotone DP -> Monotonic Stack / Queue hot sheet
First Questions¶
- static or dynamic?
- prefix-only or arbitrary range?
- online or offline?
- duplicates matter?
- do you need order, rank, or only the current best element?
Core Invariants¶
- Fenwick / prefix structures: one stable prefix meaning
- segment tree: every node represents one mergeable segment summary
- persistent segment tree: every version is one root and untouched subtrees are shared
- DSU: every element belongs to one representative-led component
- rollback DSU: the current component forest must be exactly restorable from snapshots
- ordered-set logic: duplicates and erase policy must be explicit
Retrieval Cues¶
- "largest ticket not exceeding x" -> multiset predecessor
- "median of the current window" -> two multisets before reaching for PBDS
- "minimum on every sliding window" -> Monotonic Stack / Queue hot sheet, not a segment tree
- "first smaller / greater to the left / right" -> Monotonic Stack / Queue hot sheet
- "all queries known first" -> Offline Tricks hot sheet before building a heavier online structure
- "merge child subtree maps / sets into one surviving bag" -> DSU On Tree hot sheet
- "one current range and symmetric add/remove are the whole story" -> Mo's hot sheet
- "insert / erase / maximum xor query" -> Binary Trie hot sheet
- "the keys are bounded integers and predecessor / successor is the point, not xor" -> X-Fast / Y-Fast hot sheet
- "merge the heaps containing x and y, then pop the minimum of x's heap" -> Pairing / Leftist Heap hot sheet
- "range assign keeps turning big slices into one value, and the real state is the current runs" -> ODT / Chtholly hot sheet
- "I want to hand-code a balanced BST, but I'm not sure whether AVL / Red-Black / Scapegoat / SBT is the right contest choice" -> Balanced BST hot sheet
- "cut / paste / insert / erase by current position" -> Treap / Implicit Treap hot sheet
- "rank / k-th is real, PBDS is unavailable or you want self-hosted split/merge by key" -> Treap / Implicit Treap hot sheet + Order Statistics Tree hot sheet
- "I want a self-adjusting ordered set and I specifically want to understand splaying itself" -> Splay Tree hot sheet
- "book this interval if it does not overlap anything already booked" -> Interval Tree hot sheet
- "I specifically want the multiway B-tree story with split-full-child, not a contest-first ordered set" -> B-Tree hot sheet
- "I specifically want the probabilistic skiplist story rather than any BST route" -> Skiplist hot sheet
- "static subarray k-th smallest or threshold count" -> Wavelet Tree hot sheet
Quick Anchors In This Repo¶
- Fenwick -> CVP00001
- DSU -> DSU hot sheet + C11XU
- rollback connectivity -> DSU Rollback hot sheet + Dynamic Connectivity
- subtree container merging -> DSU On Tree hot sheet + Distinct Colors
- persistent range sums -> Persistent Data Structures hot sheet + Range Queries and Copies
- branching list versions with
merge / head / tail-> Persistent Treap hot sheet + Persistent List - sequence surgery by position -> Treap / Implicit Treap hot sheet + Cut and Paste
- key-based treap order statistics -> Treap / Implicit Treap hot sheet + Salary Queries
- static range order statistics -> Wavelet Tree hot sheet + MKTHNUM - K-th Number
- ordered multiset -> Concert Tickets
- offline sweep -> Offline Tricks hot sheet + Distinct Values Queries
- current-range Mo maintenance -> Mo's hot sheet + Powerful Array
- dynamic xor multiset -> Binary Trie hot sheet + Vasiliy's Multiset
- dynamic ordered-set rank /
k-th -> Order Statistics Tree hot sheet + ORDERSET - Order Statistic Set - high-fanout textbook search tree -> B-Tree hot sheet + B-Tree Dictionary
- probabilistic textbook ordered dictionary -> Skiplist hot sheet + Skiplist Dictionary
- bounded-universe predecessor structure -> X-Fast / Y-Fast hot sheet + X-Fast Dictionary
- dynamic interval overlap queries -> Interval Tree hot sheet + Reservation System
- textbook balanced-BST compare note -> Balanced BST hot sheet + Balanced BSTs For Contests
- self-adjusting ordered multiset with parent-pointer rotations -> Splay Tree hot sheet + Ordinary Balanced Tree
- meldable singleton heaps -> Pairing / Leftist Heap hot sheet + Mergeable Heap 1
- interval partitions with
split / assign / walk-> ODT / Chtholly hot sheet + Willem, Chtholly and Seniorious - sparse table -> Sparse Table hot sheet + Static Range Minimum Queries
- segment tree -> Segment Tree hot sheet + Dynamic Range Sum Queries
- lazy segment tree -> Lazy Segment Tree hot sheet + HORRIBLE
- segment tree beats -> Segment Tree Beats hot sheet + Range Chmin Chmax Add Range Sum
- monotone boundary scan -> Monotonic Stack / Queue hot sheet + Nearest Smaller Values
- monotone window minimum -> Monotonic Stack / Queue hot sheet + Sliding Window Minimum
Common Mistakes¶
- using a segment tree when the query is static
- mutating a shared node and corrupting an old version
- forgetting that
setdrops duplicates - trying to erase arbitrary heap items without a lazy-delete plan
- missing the original-order bookkeeping in offline processing
Do Not Overbuild¶
Under contest pressure, the usual failure is not “too simple.” It is “picked a heavier structure than the operations justify.”
Ask:
- would prefix sums already work?
- would sorting + two pointers already work?
- are all queries known first, so offline processing is enough?
Next Stops¶
- Heaps and ordered sets topic
- Offline tricks topic
- Fenwick hot sheet
- DSU hot sheet
- DSU Rollback hot sheet
- DSU On Tree hot sheet
- Persistent Data Structures hot sheet
- Persistent Treap hot sheet
- Pairing / Leftist Heap hot sheet
- ODT / Chtholly hot sheet
- Segment Tree hot sheet
- Lazy Segment Tree hot sheet
- Segment Tree Beats hot sheet
- Sparse Table hot sheet
- Offline Tricks hot sheet
- Treap / Implicit Treap hot sheet
- Wavelet Tree hot sheet
- Mo's hot sheet
- Monotonic Stack / Queue hot sheet
- Template library
- Order Statistics Tree hot sheet
- B-Tree hot sheet
- Skiplist hot sheet
- X-Fast / Y-Fast hot sheet
- Interval Tree hot sheet
- Balanced BST hot sheet
- Fenwick Tree topic
- PBDS / Order Statistics Tree topic
- Skip Lists topic
- X-Fast / Y-Fast Tries topic
- Balanced BSTs For Contests topic
- Monotonic Stack / Queue topic
- Splay Tree hot sheet
- Splay Tree topic
- Segment Tree topic
- Lazy Segment Tree topic
- Segment Tree Beats topic
- Persistent Data Structures topic
- Persistent Treap topic
- Pairing Heap / Leftist Heap topic
- ODT / Chtholly topic
- Treap / Implicit Treap topic
- Wavelet Tree topic
- Mo's Algorithm topic
- Binary Trie / XOR Queries topic