Competitive Programming

Data Structures & Algorithms

Long-form notes on the algorithms, data structures, and contest patterns I want to keep in a form that is still useful months later.

85 published notes
9 categories
30 planned topics
TeX + C++ source-first workflow

How This Section Is Organized

The landing note explains the scope; the taxonomy section keeps the bigger map visible.

Overview

These notes are the part of the competitive programming archive that is not tied to one contest year. The goal is simple: keep the ideas that I actually want to reuse in a form that is still readable after the adrenaline of the original contest is gone.

I care about three things here:

  • the structural reason an algorithm or data structure works,

  • the implementation shape I would still trust in contest code,

  • the failure modes that usually cause wrong answers or wasted time.

How the Notes Are Written

Each topic keeps note.tex as the written source of truth and code.cpp as the implementation source of truth. The site renders the TeX into HTML and shows the C++ file separately, so the prose can stay focused while the code remains directly copyable.

The finished notes are meant to be public-facing study material, not private scraps. That means the emphasis is on explanations, invariants, use cases, and pitfalls instead of dumping templates with no context.

If I were revising this section from scratch, I would split it into a few routes instead of pretending there is one universal order:

  • core first: binary search, prefix sums, sliding window, coordinate compression, Fenwick tree, BFS / DFS,

  • graph path: topological sort, Dijkstra, MST, SCC, Euler tour, LCA, tree decompositions,

  • DP optimization path: knapsack, tree DP, monotone queue, divide-and-conquer DP, Knuth, convex hull trick, rerooting,

  • advanced data structures: segment tree, sparse table, ordered set, Li Chao tree, persistent segment tree, rollback DSU,

  • number theory path: extended Euclid, modular arithmetic, CRT, sieve, Mobius, Miller-Rabin, discrete log.

  • That is still not the only valid order, but it is closer to how I actually revise: pick a branch, stay inside it long enough for the invariants to become familiar, then switch.

What ``Finished'' Means Here

A finished note should answer the questions I usually care about when revising:

  • What kind of contest situation makes this the right tool?

  • What invariant is doing the real work?

  • Which implementation shape is the least fragile under time pressure?

  • Which edge cases usually break the first version?

  • What does a worked contest-style example actually look like?

  • Some topics are still only planned. They are listed openly as planned instead of pretending that a shallow page is enough.

References Behind the Section

The explanations are rewritten in my own words, but the topic map and many implementation choices were informed by:

A practical first path

These notes form the shortest useful route into the section before the more structural topics take over.

What is already solid

The section is intentionally uneven: the counts below show where the dense reusable notes already exist and where the next pass still has room to grow.

20 basic notes
28 intermediate notes
35 advanced notes
2 expert notes
Fundamentals: 9 complete / 3 planned Data Structures: 16 complete / 3 planned Graph Algorithms: 12 complete / 4 planned Dynamic Programming: 11 complete / 4 planned Number Theory: 12 complete / 4 planned String Algorithms: 8 complete / 2 planned Geometry: 6 complete / 3 planned Flows and Matching: 4 complete / 2 planned Advanced Tricks: 7 complete / 5 planned

Browse by technique family

Each category page now shows finished notes, planned topics, recommended order, and level distribution, so it is easier to decide whether to revise breadth-first or go deep on one branch.

01 Fundamentals

9 published notes

These are the habits and small techniques that decide whether a problem is a quick implementation or an unnecessary detour.

Open Fundamentals
3 planned 5 basic / 4 intermediate / 0 advanced / 0 expert
02 Data Structures

16 published notes

This branch focuses on structures that trade memory, invariants, or amortization for faster updates and queries.

Open Data Structures
3 planned 5 basic / 3 intermediate / 6 advanced / 2 expert
03 Graph Algorithms

12 published notes

When a problem hides an interaction graph, these notes aim to make the graph structure explicit instead of magical.

Open Graph Algorithms
4 planned 2 basic / 6 intermediate / 4 advanced / 0 expert
04 Dynamic Programming

11 published notes

The focus here is less on memorizing formulas and more on seeing what information must survive from one decision to the next.

Open Dynamic Programming
4 planned 1 basic / 3 intermediate / 7 advanced / 0 expert
05 Number Theory

12 published notes

These notes emphasize the parts of number theory that become algorithms rather than standalone proofs.

Open Number Theory
4 planned 5 basic / 1 intermediate / 6 advanced / 0 expert
06 String Algorithms

8 published notes

String problems are often bookkeeping problems in disguise. The goal is to keep the invariant small enough to update in linear time.

Open String Algorithms
2 planned 1 basic / 3 intermediate / 4 advanced / 0 expert
07 Geometry

6 published notes

The intended emphasis is contest geometry that survives integer arithmetic, not coordinate-free elegance for its own sake.

Open Geometry
3 planned 1 basic / 3 intermediate / 2 advanced / 0 expert
08 Flows and Matching

4 published notes

This branch collects problems where the cleanest solution is to phrase the whole task as conserved flow or structural matching.

Open Flows and Matching
2 planned 0 basic / 1 intermediate / 3 advanced / 0 expert
09 Advanced Tricks

7 published notes

These topics are the part of competitive programming that is hardest to compress into one sentence but easiest to recognize after enough practice.

Open Advanced Tricks
5 planned 0 basic / 4 intermediate / 3 advanced / 0 expert

One reasonable order to learn this

The notes are cross-linked by topic, but this order keeps the prerequisites and implementation burden under control.

01 Start here
Core

Learn the low-friction patterns first: monotone search, prefix preprocessing, window scans, and basic traversal.

Binary Search, Prefix Sums and Difference Arrays, Two Pointers and Sliding Window, BFS and DFS

02 Build fluency
Core

Add the first reusable data structures and string baselines once the array and graph patterns feel routine.

Fenwick Tree, Segment Tree, Disjoint Set Union, KMP, Z-Function, Topological Sort

03 Push into advanced
Advanced

This is where tree queries, heavier DP, and flow or number-theory machinery start paying off.

Lazy Segment Tree, Sparse Table, Knapsack, Tree DP, Dijkstra, Lowest Common Ancestor, Dinic, NTT

04 Save for last
Expert

Keep the amortized and pointer-heavy machinery for after the more standard contest baselines are stable.

Heavy-Light Decomposition, Convex Hull Trick, Splay Tree, Link-Cut Tree, Min-Cost Max-Flow

Choose a path, not just a topic

These routes are meant to reduce thrashing. Each one keeps related ideas close enough that implementation habits carry over from note to note.

01 Core first
7 notes

Use this if the goal is broad contest fluency before specializing. It stays close to the patterns that appear in array, graph, and prefix-processing problems.

Binary Search, Binary Search on Answer, Prefix Sums and Difference Arrays, Two Pointers and Sliding Window, Coordinate Compression, Fenwick Tree, BFS and DFS

02 Graph path
8 notes

Follow this route if tree structure, connectivity, and path queries are the current bottleneck.

BFS and DFS, Topological Sort, Dijkstra, Minimum Spanning Tree, Strongly Connected Components, Euler Tour Technique, Lowest Common Ancestor (Binary Lifting), Centroid Decomposition

03 DP optimization path
8 notes

This path is for when the recurrence is already visible and the remaining problem is state reduction or transition acceleration.

Knapsack, Tree DP, Monotone Queue Optimization, Divide and Conquer DP Optimization, Knuth Optimization, Convex Hull Trick, Rerooting DP, Aliens Trick / Lagrangian Relaxation

04 Advanced data structures path
8 notes

Take this when the implementation burden is acceptable and the main task is choosing the right structural invariant.

Fenwick Tree, Segment Tree, Sparse Table, Ordered Set (Policy-Based Data Structures), Li Chao Tree, Persistent Segment Tree, DSU Rollback, Wavelet Tree

05 Number theory path
9 notes

This route starts from the arithmetic tools that appear everywhere, then moves into structure-heavy topics such as CRT, multiplicative functions, and primality machinery.

GCD and Extended Euclid, Modular Arithmetic, Modular Inverse, Sieve of Eratosthenes, Prime Factorization, Chinese Remainder Theorem, Mobius Function, Miller-Rabin and Pollard Rho, Primitive Roots and Discrete Logarithm

Current note library

The first pass focuses on notes I expect to reuse often in contest prep, implementation review, and post-contest study.

01
Fundamentals

Binary Search

A decision-to-answer pattern for monotone predicates, answer-space search, and continuous approximation.

basic monotone predicate / answer search
02
Fundamentals

Ternary Search

Optimize a unimodal function on a discrete or continuous domain by keeping the side that still contains the optimum.

intermediate optimization / unimodal
03
Fundamentals

Prefix Sums and Difference Arrays

Turn repeated range work into constant-time queries or constant-time lazy marks by changing what the array stores.

basic preprocessing / range queries
04
Fundamentals

Two Pointers and Sliding Window

Exploit one-way pointer movement to turn many quadratic scans into linear passes over arrays and strings.

basic window techniques / linear scan
05
Fundamentals

Sorting and Events

Turn interval and query problems into a left-to-right sweep over sorted add, remove, and inspect events.

basic sweep line / offline
06
Fundamentals

Bitmask Techniques

Compact subset state, fast bit operations, and the enumeration patterns that power many small-state solutions.

intermediate subset state / bit operations
07
Fundamentals

Greedy Exchange Argument

A proof pattern for showing that one locally optimal choice can be forced into some global optimum without making the solution worse.

intermediate greedy / proof
08
Fundamentals

Coordinate Compression

Replace large or sparse values by their rank order so array-based structures become practical again.

basic preprocessing / indexing
09
Fundamentals

Meet-in-the-Middle

Split an exponential search into two smaller exponential searches and join the halves with sorting or hashing.

intermediate complete search / subset sums
010
Data Structures

Fenwick Tree

A compact range-query structure for point updates, prefix sums, and frequency-based order statistics.

basic range queries / prefix sums
011
Data Structures

Monotonic Stack and Queue

Maintain candidates in sorted order of value so next-greater and sliding-window extrema can be updated in linear time.

basic stack / deque
012
Data Structures

Two-Dimensional Fenwick Tree

Extend the Fenwick tree idea to point updates and rectangle sums on a grid.

intermediate fenwick tree / 2d
013
Data Structures

Segment Tree

The general-purpose interval tree for logarithmic range queries and updates when Fenwick is too small.

basic range queries / interval decomposition
014
Data Structures

Lazy Segment Tree

The standard extension of a segment tree when range updates must stay logarithmic too.

intermediate range updates / lazy propagation
015
Data Structures

Sparse Table

A static range-query structure that turns immutable RMQ-style queries into O(1) lookups.

basic static queries / rmq
016
Data Structures

Disjoint Set Union

Union-find for dynamic connectivity and component bookkeeping when edges only merge components.

basic connectivity / union find
017
Data Structures

Treap

A randomized BST built from split and merge, with order statistics and clean structural recursion.

advanced balanced bst / split merge
018
Data Structures

Splay Tree

A self-adjusting BST that pays for flexibility with amortized analysis instead of explicit balance invariants.

expert self-adjusting bst / amortized analysis
019
Data Structures

Link-Cut Tree

A dynamic-forest structure built on splay trees for link, cut, reroot, and path-query operations.

expert dynamic forest / splay tree
020
Data Structures

Ordered Set (Policy-Based Data Structures)

An indexed balanced tree for order statistics when std::set is not enough and a full custom BST is overkill.

intermediate order statistics / GNU PBDS
021
Data Structures

Li Chao Tree

A segment-tree-style structure for maintaining a dynamic lower envelope of lines under point queries.

advanced lines / DP optimization
022
Data Structures

Persistent Segment Tree

Path-copy only the changed nodes so every update creates a new version without destroying the old one.

advanced persistence / range queries
023
Data Structures

DSU Rollback

A union-find variant that can undo merges, which is the missing piece behind offline dynamic connectivity.

advanced union find / rollback
024
Data Structures

Cartesian Tree

Build a tree whose inorder traversal is the array order and whose heap property exposes range minima or maxima.

advanced tree construction / rmq
025
Data Structures

Wavelet Tree

Recursively partition values so k-th order statistics and frequency queries on subarrays can be answered quickly.

advanced order statistics / range queries
026
Graph Algorithms

BFS and DFS

The fundamental graph traversals for reachability, layering, component discovery, and tree exploration.

basic graph traversal / reachability
027
Graph Algorithms

Dijkstra

Single-source shortest paths in non-negative weighted graphs with a min-heap and lazy deletions.

intermediate shortest paths / graphs
028
Graph Algorithms

Topological Sort

Order a DAG so every directed edge points forward, then reuse that order for dependency processing and DAG DP.

basic DAG / ordering
029
Graph Algorithms

Minimum Spanning Tree

Connect a weighted graph as cheaply as possible, usually with Kruskal and DSU as the contest default.

intermediate weighted graphs / Kruskal
030
Graph Algorithms

Euler Tour Technique

Flatten a rooted tree into entry and exit times so subtree queries become interval queries on an array.

intermediate trees / flattening
031
Graph Algorithms

Lowest Common Ancestor (Binary Lifting)

Lift nodes by powers of two so LCA and ancestor queries become logarithmic after one tree preprocessing pass.

intermediate trees / binary lifting
032
Graph Algorithms

Strongly Connected Components

Compress a directed graph into its mutually reachable blocks, then reason on the resulting DAG instead of the original cycles.

intermediate directed graphs / SCC
033
Graph Algorithms

Bridges and Articulation Points

Use DFS lowlink values to find edges or vertices whose removal disconnects an undirected graph.

intermediate lowlink / graph structure
034
Graph Algorithms

2-SAT

Reduce two-literal clauses to an implication graph, then solve satisfiability with SCC decomposition.

advanced logic / SCC
035
Graph Algorithms

Virtual Tree

Compress a query's marked nodes and the LCAs between them into a much smaller tree that preserves the relevant paths.

advanced tree queries / lca
036
Graph Algorithms

Centroid Decomposition

Recursively split a tree by balanced centroids so global path problems can be reduced to logarithmically many local views.

advanced trees / divide and conquer
037
Graph Algorithms

Heavy-Light Decomposition

Reduce tree path queries to a logarithmic number of segment queries by carving the tree into heavy chains.

advanced trees / path queries
038
Dynamic Programming

Knapsack

The standard capacity-based DP pattern, with the loop directions and state choices that distinguish 0/1 from unbounded use.

basic dynamic programming / capacity DP
039
Dynamic Programming

Bitmask DP

Dynamic programming over subsets when the state is a small visited set, partition, or compatibility frontier.

intermediate dynamic programming / bitmasks
040
Dynamic Programming

Digit DP

A position-by-position DP over the decimal expansion of an upper bound, with tight and leading-zero states.

intermediate digit dp / counting
041
Dynamic Programming

Tree DP

Exploit the parent-child structure of trees so each state only has to summarize one rooted subtree at a time.

intermediate dynamic programming / trees
042
Dynamic Programming

Monotone Queue Optimization

Speed up DP transitions over a sliding range by keeping only the best candidates in a deque.

advanced dynamic programming / deque
043
Dynamic Programming

Divide and Conquer DP Optimization

Speed up row-by-row transition minimization when the optimal split point moves monotonically.

advanced DP optimization / monotonicity
044
Dynamic Programming

Knuth Optimization

Speed up interval DP from O(n^3) to O(n^2) when the optimal split points move monotonically.

advanced interval dp / optimization
045
Dynamic Programming

Convex Hull Trick

A deque-based lower hull for DP recurrences with monotone slopes and monotone queries.

advanced dp optimization / lines
046
Dynamic Programming

SOS DP

Transform subset-based values so every mask can aggregate information from all of its submasks or supermasks efficiently.

advanced bitmasks / subset transform
047
Dynamic Programming

Rerooting DP

Compute a tree answer for every possible root by combining one downward DP pass with one upward transfer pass.

advanced trees / all roots
048
Dynamic Programming

Aliens Trick / Lagrangian Relaxation

Replace a hard limit on the number of chosen groups with a penalty and binary search that penalty.

advanced dp optimization / lagrangian
049
Number Theory

GCD and Extended Euclid

The basic divisibility toolkit for reducing fractions, solving linear equations, and building modular arithmetic routines.

basic gcd / extended euclid
050
Number Theory

Modular Arithmetic

Keep arithmetic safe under a modulus by tracking the algebraic rules that still survive after reduction.

basic mod / algebra
051
Number Theory

Modular Inverse

Undo multiplication modulo m when the gcd condition allows it, and choose the right inverse routine for the modulus.

basic modular arithmetic / inverse
052
Number Theory

Sieve of Eratosthenes

Precompute primality and smallest prime factors once, then answer many prime-related queries cheaply.

basic primes / preprocessing
053
Number Theory

Chinese Remainder Theorem

Combine modular constraints into one congruence when the residue classes are compatible.

advanced CRT / congruences
054
Number Theory

Prime Factorization

Break numbers into prime powers and choose the right factorization strategy for the size and query pattern.

basic primes / factorization
055
Number Theory

Euler Phi

Count how many integers up to n are coprime to n, and use the factorization formula to turn that count into code.

intermediate coprimality / multiplicative function
056
Number Theory

Mobius Function

Use the Mobius function and inversion to separate exact divisibility structure from overcounted multiples.

advanced mobius / inversion
057
Number Theory

NTT

Polynomial convolution modulo 998244353 using roots of unity and iterative butterfly layers.

advanced convolution / polynomials
058
Number Theory

Primitive Roots and Discrete Logarithm

Work inside cyclic multiplicative groups by finding generators and solving exponent equations with baby-step giant-step.

advanced cyclic groups / bsgs
059
Number Theory

Miller-Rabin and Pollard Rho

The standard 64-bit primality and factorization toolkit once trial division stops being realistic.

advanced primality / factorization
060
Number Theory

Polynomial Interpolation

Recover or evaluate a low-degree polynomial from sample points, usually with modular Lagrange interpolation.

advanced polynomials / lagrange
061
String Algorithms

KMP

Prefix-function based linear-time string matching with no backtracking in the text.

basic strings / pattern matching
062
String Algorithms

Z-Function

Measure how much each suffix matches the full string, then reuse that linear-time structure for pattern matching and periodicity.

intermediate strings / linear matching
063
String Algorithms

Rolling Hash

A fast probabilistic fingerprint for substring comparison, string sets, and prefix-based matching tricks.

intermediate hashing / substrings
064
String Algorithms

Trie

Store strings by prefix so inserts, prefix checks, and dictionary transitions depend on length rather than set size.

intermediate prefix tree / strings
065
String Algorithms

Suffix Array

Sort all suffixes once, then reuse the order and LCP information for many substring and lexicographic queries.

advanced suffix structures / sorting
066
String Algorithms

Suffix Automaton

A linear-time substring automaton that is especially good at online extension, distinct-substring counts, and occurrence queries.

advanced suffix structures / automaton
067
String Algorithms

Manacher

Compute longest odd and even palindromic radii around every center in linear time.

advanced palindrome / linear time
068
String Algorithms

Aho-Corasick

A trie with failure links that searches many patterns in one text in a single pass.

advanced multiple patterns / automaton
069
Geometry

Orientation and Cross Product

The core integer-geometry predicate behind turns, area tests, segment intersection, and hull construction.

basic geometry primitives / integer geometry
070
Geometry

Convex Hull

Build the outer envelope of a point set with monotone chain and use it as the basis for many geometry reductions.

intermediate geometry / convex hull
071
Geometry

Line Intersection

Reliable line and segment intersection tests built on orientation, with exact predicates and careful boundary handling.

intermediate segments / predicates
072
Geometry

Point in Polygon

Classify a point as outside, on the boundary, or inside a polygon with a contest-ready ray-crossing routine.

intermediate polygons / classification
073
Geometry

Rotating Calipers

Walk two pointers around a convex polygon to enumerate antipodal structure without restarting from scratch.

advanced convex polygon / two pointers
074
Geometry

Closest Pair

Find the nearest pair of points in O(n log n) with divide and conquer and a y-sorted strip check.

advanced divide and conquer / geometry
075
Flows and Matching

Dinic

A layered-network max-flow algorithm that is fast enough for most contest flow models and reusable in many reductions.

advanced max flow / layered graph
076
Flows and Matching

Bipartite Matching

Match the two sides of a bipartite graph efficiently, and recognize when the problem is assignment rather than general flow.

intermediate matching / bipartite graph
077
Flows and Matching

Hopcroft-Karp

Find a maximum bipartite matching faster than repeated DFS by growing many shortest augmenting paths in one phase.

advanced matching / bipartite graph
078
Flows and Matching

Min-Cost Max-Flow

Send flow while optimizing total cost, which turns assignment and constrained transport models into graph problems.

advanced flow / cost optimization
079
Advanced Tricks

Binary Search on Answer

Turn optimization into repeated feasibility checks by designing a monotone predicate over the answer space.

intermediate optimization / monotone predicate
080
Advanced Tricks

Parallel Binary Search

Answer many monotone offline queries at once by testing them in batches at shared midpoints.

advanced offline / batching
081
Advanced Tricks

Mo's Algorithm

Offline range-query ordering that trades sorting plus add/remove operations for fast answers on static arrays.

intermediate offline queries / sqrt decomposition
082
Advanced Tricks

Offline Queries

Reorder queries so that one sweep or one monotone data-structure state can answer them more cheaply than online processing.

intermediate offline processing / sorting
083
Advanced Tricks

Small-to-Large Merging

Amortize repeated subtree merges by always moving the smaller container into the larger one.

advanced amortized analysis / trees
084
Advanced Tricks

Sqrt Decomposition

Split the array into blocks so point updates and range queries become simple block-level work instead of full rescans.

intermediate block decomposition / range queries
085
Advanced Tricks

Bitset Optimization

Use machine-word parallelism to update or compare dozens of boolean states with one operation.

advanced bitset / word parallelism

Save these for when the basics feel routine

These notes are the ones most likely to repay a slower read: they unlock bigger structures, stronger reductions, or heavier implementation patterns.

Controlled Topic Map

This is the stable topic map for the section: some entries already have full notes, others are intentionally marked as planned.

Fundamentals

  • Complexity analysis

  • Sorting

  • Binary search

  • Ternary search

  • Two pointers

  • Prefix sums

  • Difference arrays

  • Coordinate compression

  • Sorting + events

  • Greedy exchange argument

  • Bitmasks

  • Meet-in-the-middle

Data Structures

  • Fenwick tree

  • 2D Fenwick tree

  • Segment tree

  • Lazy segment tree

  • Sparse table

  • Disjoint set union

  • Monotonic stack / queue

  • Ordered set

  • Treap

  • Splay tree

  • Cartesian tree

  • Persistent segment tree

  • Li Chao tree

  • Link-cut tree

  • Trie

  • Wavelet tree

Graph Algorithms

  • DFS / BFS

  • Topological sort

  • Dijkstra

  • Bellman-Ford

  • Floyd-Warshall

  • LCA

  • Heavy-light decomposition

  • Bridges / articulation points

  • SCC

  • Euler tour technique

  • DSU on tree

  • Centroid decomposition

  • Minimum spanning tree

  • 2-SAT

  • Virtual tree

Dynamic Programming

  • Classical DP

  • Knapsack

  • Interval DP

  • Tree DP

  • Digit DP

  • Bitmask DP

  • Convex hull trick

  • Divide-and-conquer optimization

  • Knuth optimization

  • Aliens trick / Lagrangian relaxation

  • Monotone queue optimization

  • SOS DP

  • Rerooting DP

  • Slope trick

Number Theory

  • gcd / lcm

  • Modular arithmetic

  • Modular inverse

  • CRT

  • Sieve

  • Prime factorization

  • Euler phi

  • Mobius function

  • Inclusion-exclusion

  • Primitive roots

  • Discrete logarithm

  • Miller-Rabin

  • Pollard Rho

  • Polynomial interpolation

  • NTT

String Algorithms

  • Prefix function / KMP

  • Z-function

  • Rolling hash

  • Suffix array

  • Suffix automaton

  • Aho-Corasick

  • Palindromic tree

  • Manacher

Geometry

  • Orientation / cross product

  • Line intersection

  • Convex hull

  • Rotating calipers

  • Closest pair

  • Half-plane intersection

  • Point in polygon

  • Sweep line

Flows and Matching

  • Dinic

  • Min-cost max-flow

  • Bipartite matching

  • Hopcroft-Karp

  • Hungarian algorithm

  • General matching

Advanced Tricks

  • Mo's algorithm

  • Sqrt decomposition

  • Small-to-large merging

  • Offline queries

  • Divide and conquer on answer

  • Parallel binary search

  • Randomization

  • Amortized analysis

  • Potential method

  • Bitset optimization

  • Parametric search

Source Files and Assets

Raw files are still available here when you want the original TeX, C++, or statement assets.

Show raw files