Skip to content

Chapter 2: Data Structures

Data structures are what separate "I know the algorithm" from "I can solve it fast enough." This chapter is the point where the notebook shifts from ideas to machinery: basic containers, then the competitive-programming workhorses like DSU, Fenwick trees, and segment trees, and finally the specialized tools for rollback, split/merge, and line queries. The common theme is simple: replace repeated O(n) scans with an invariant you can maintain in O(log n) or O(1).

Coming from: Chapter 1 — Essential Techniques — the techniques that tell you what to compute Going to: Chapter 3 — Graph Foundations — the traversal and shortest-path tools that tell you how to move through relationships Bridge: BRIDGE-to-graphs.md — the conceptual handoff between the two

Topics at a Glance

#TopicDifficultyKey Trigger Phrase
01Stack, Queue, and DequeBeginner"Process in LIFO/FIFO order"
02Linked List BasicsBeginner"Insert/delete in O(1) at known position"
03Linked Lists in ContestsBeginner"Simulate deletions in a sequence"
04Monotonic StackIntermediate"Next greater/smaller element"
05Monotonic QueueIntermediate"Sliding window min/max"
06Hash Map and Unordered MapBeginner"Count occurrences" or "O(1) lookup"
07Tree BasicsBeginner"Rooted tree traversal"
08Set and MultisetBeginner"Ordered container with O(log n) insert/find"
09Priority Queue and HeapsBeginner"Repeatedly extract minimum" (Dijkstra, greedy)
10Policy-Based DS (PBDS)Intermediate"Kth smallest" or "count elements < X"
11Union-Find (DSU)Intermediate"Are u and v connected?" + edges added online
12Sparse Table (RMQ)Intermediate"Static range min/max" with no updates
13Binary Indexed Tree (Fenwick)Intermediate"Prefix sums" + "point updates"
142D Fenwick TreeAdvanced"2D prefix sums with updates"
15Segment TreeIntermediate"Range query" + "point update" (any associative op)
16Segment Tree with Lazy PropagationAdvanced"Range query" + "range update"
17TrieIntermediate"Common prefix queries" or "XOR maximization"
18Cartesian Tree (Treap)Advanced"Split/merge sequences" or "implicit array with insert/delete"
20Li Chao TreeAdvanced"Add line, query max at x" — online CHT alternative
21Segment Tree MergingAdvanced"Merge subtree info during DFS" (dynamic seg tree merge)
19Persistent DSUAdvanced"Add/remove edges offline" — rollback-capable union-find

If You Only Read 3 Files

  1. Segment Tree — because it's the single most versatile data structure in competitive programming. Range queries + point updates for any associative operation. Learn this and you can solve hundreds of problems.
  2. Union-Find (DSU) — because connectivity queries appear constantly in graph problems, and DSU answers them in near-O(1). Also the backbone of Kruskal's MST algorithm.
  3. Monotonic Stack — because "next greater element" is a pattern that shows up in disguise across many problems, and the monotonic stack technique is elegant, short, and easy to get wrong if you haven't practiced it.

Cross-Chapter Connections


File Listing

FileTopicLevel
stack-queue-dequeStack, Queue, and Deque
linked-list-basicsLinked List Basics
linked-list-contestLinked Lists in Contests
hash-map-and-unordered-mapHash Map and Unordered Map
set-and-multisetSet and Multiset
priority-queue-and-heapsPriority Queue and Heaps
tree-basicsTree Basics
monotonic-stackMonotonic Stack⭐⭐
monotonic-queueMonotonic Queue⭐⭐
union-find-dsuUnion-Find (DSU)⭐⭐
binary-indexed-tree-fenwickBinary Indexed Tree (Fenwick)⭐⭐
segment-treeSegment Tree⭐⭐
sparse-table-rmqSparse Table (RMQ)⭐⭐
trieTrie⭐⭐
policy-based-dsPolicy-Based Data Structures (PBDS)⭐⭐
segment-tree-lazySegment Tree with Lazy Propagation⭐⭐⭐
binary-indexed-tree-2d2D Fenwick Tree⭐⭐⭐
cartesian-treeCartesian Tree (Treap)⭐⭐⭐
lichao-treeLi Chao Tree⭐⭐⭐
segment-tree-mergeSegment Tree Merging⭐⭐⭐
persistent-dsuPersistent DSU (Rollback)⭐⭐⭐

Suggested Reading Order

  1. Stack, Queue, and Deque — foundational restricted-access containers
  2. Hash Map and Unordered Map — O(1) lookups for counting and indexing
  3. Set and Multiset — ordered containers with O(log n) operations
  4. Priority Queue and Heaps — extract min/max for Dijkstra and greedy
  5. Tree Basics — representation and traversal fundamentals
  6. Monotonic Stack — next greater/smaller element patterns
  7. Monotonic Queue — sliding window min/max in O(1)
  8. Union-Find (DSU) — near-constant connectivity queries
  9. Binary Indexed Tree (Fenwick) — prefix sums with point updates
  10. Segment Tree — general range queries and point updates
  11. Sparse Table (RMQ) — O(1) static range minimum queries
  12. Trie — prefix-based string lookups
  13. Policy-Based DS (PBDS) — order statistics with GNU PBDS
  14. Segment Tree with Lazy Propagation — range updates with lazy propagation
  15. 2D Fenwick Tree — extend Fenwick to 2D grids
  16. Cartesian Tree (Treap) — implicit treap with split/merge
  17. Li Chao Tree — online "add line, query max at x" structure
  18. Segment Tree Merging — merge dynamic seg trees during DFS
  19. Persistent DSU — rollback-capable union-find for offline dynamic connectivity

Built for the climb from Codeforces 1100 to 2100.