Dense notes for competitive programming, from the first contest to ICPC World Finals: every topic with tested C++ code, the ideas that make it work, and a collapsed proof for each claim. Code assumes the template in ๐Ÿž Templates (ll, sz, all).

Start here

Chapters

1. Problem Solving

i. Attacking a Problem ยท ii. Invariants, Parity and Extremal Arguments ยท iii. Contribution and Complementary Counting ยท iv. Proof Techniques ยท v. Constructive Algorithms ยท vi. Classic Puzzles ยท vii. Observation Toolkit ยท viii. When the Answer Is Small ยท ix. Sorting and Ordering Observations ยท x. Parity, Coloring and Mod Observations ยท xi. Operations and Reachable States ยท xii. Ad Hoc on Arrays and Strings ยท xiii. Ad Hoc Problem Set ยท xiv. Permutation Constructions ยท xv. Graph Constructions ยท xvi. Grid and Matrix Constructions ยท xvii. Sequence and Number Constructions ยท xviii. Invariant-Based Constructions ยท xix. Greedy Constructions ยท xx. Interactive and Output-Only Constructions ยท xxi. Problem Reduction and Modeling

2. Searching and Sorting

i. Binary Search ยท ii. Ternary and Golden-Section Search ยท iii. Sorting, Selection and Inversions

3. Array Techniques

i. Prefix Sums and Difference Arrays ยท ii. Two Pointers and Sliding Window ยท iii. Monotonic Stack ยท iv. Classic Array Problems ยท v. Subarray Aggregates and Mex

4. Greedy

i. Greedy and Exchange Arguments ยท ii. Matroids ยท iii. Interval Problems ยท iv. Scheduling and Partitioning

5. Divide and Conquer and Offline

i. Divide and Conquer ยท ii. CDQ Divide and Conquer ยท iii. Offline Techniques ยท iv. Parallel Binary Search

6. Bit Manipulation

i. Bit Tricks and XOR ยท ii. Gray Code ยท iii. Submasks, Supermasks and SOS DP ยท iv. Bitwise Convolutions ยท v. XOR Basis ยท vi. Bitset ยท vii. Bitset and Word-Level Parallelism

7. Data Structures

i. Containers, Hashing and Choosing a Structure ยท ii. Ordered Set and PBDS ยท iii. Heaps ยท iv. Disjoint Set Union ยท v. Treaps and Balanced BSTs ยท vi. Chtholly Tree (Interval Map) ยท vii. Binary Trie ยท viii. Fast Set and Integer Structures ยท ix. KD-Tree and Spatial Structures ยท x. Persistent Data Structures ยท xi. Data Structure Catalog

8. Range Queries

i. Sparse Table ยท ii. Range Minimum Query ยท iii. Fenwick Tree ยท iv. Segment Tree ยท v. Lazy Propagation ยท vi. Dynamic and Persistent Segment Trees ยท vii. Segment Tree Beats and Historic Values ยท viii. Li Chao and Kinetic Segment Trees ยท ix. Segment Tree Merging and Splitting ยท x. Range Order Statistics and Wavelets ยท xi. Sqrt Decomposition ยท xii. Moโ€™s Algorithm ยท xiii. Offline and 2D Range Queries ยท xiv. Permutation Tree ยท xv. Range LIS

9. Dynamic Programming

i. DP Foundations ยท ii. Knapsack and Subset Sum ยท iii. Classic DP Problems ยท iv. LIS, LCS and Edit Distance ยท v. Interval DP ยท vi. Bitmask DP ยท vii. Broken Profile DP ยท viii. Digit DP ยท ix. Tree DP and Rerooting ยท x. Dynamic DP ยท xi. DAG and Expected-Value DP ยท xii. Counting DP Techniques ยท xiii. Matrix Exponentiation DP ยท xiv. Convex Hull Trick ยท xv. Divide and Conquer Optimization ยท xvi. Knuth-Yao Optimization ยท xvii. Monotone Queue, 1D1D and SMAWK ยท xviii. Aliens Trick ยท xix. Slope Trick

10. Graphs

i. Graph Basics and Modeling ยท ii. BFS, DFS and Grids ยท iii. Cycles and Functional Graphs ยท iv. Bipartite Graphs ยท v. DAGs and Topological Sort ยท vi. Shortest Paths ยท vii. Shortest Path Techniques ยท viii. K Shortest and Disjoint Paths ยท ix. SCC and 2-SAT ยท x. Bridges and Biconnectivity ยท xi. Dominator Tree ยท xii. Dynamic Connectivity ยท xiii. Minimum Spanning Trees ยท xiv. Directed MST ยท xv. Matrix-Tree Theorem ยท xvi. Euler Tours ยท xvii. Hamiltonian Paths and TSP ยท xviii. Special Graph Classes ยท xix. Graph Coloring

11. Flows and Matching

i. Maximum Flow ยท ii. Push-Relabel ยท iii. Flow Modeling ยท iv. Minimum Cut ยท v. Global Minimum Cut ยท vi. Gomory-Hu Tree ยท vii. Min-Cost Flow ยท viii. Assignment Problem ยท ix. Bipartite Matching ยท x. General Matching ยท xi. Weighted General Matching ยท xii. Matroid Intersection ยท xiii. Stable Matching

12. Trees

i. Tree Basics, Diameter and Center ยท ii. Tree Isomorphism and Prรผfer Codes ยท iii. Lowest Common Ancestor ยท iv. Euler Tour, Subtree and Path Queries ยท v. Heavy-Light and Long-Path Decomposition ยท vi. Centroid Decomposition ยท vii. Small to Large (DSU on Tree) ยท viii. Virtual Tree ยท ix. Kruskal Reconstruction Tree ยท x. Link-Cut Tree ยท xi. Euler Tour Tree ยท xii. Top Trees

13. Mathematics

i. Modular Arithmetic ยท ii. Barrett and Montgomery Reduction ยท iii. Probability and Expectation ยท iv. Contest Mathematics

14. Number Theory

i. GCD and Extended Euclid ยท ii. Linear Congruences and CRT ยท iii. Primes and Sieves ยท iv. Primality Testing and Factorization ยท v. Arithmetic Functions and Mรถbius ยท vi. Harmonic Lemma and Floor Sums ยท vii. Sublinear Sieves and Prime Counting ยท viii. Factorials and Binomials mod p ยท ix. Primitive Roots and Discrete Log ยท x. Quadratic Residues and Modular Roots ยท xi. Continued Fractions and Pell Equations ยท xii. Number Theory Miscellany

15. Combinatorics

i. Counting Fundamentals ยท ii. Binomial Identities ยท iii. Inclusion-Exclusion and Derangements ยท iv. Catalan Numbers and Lattice Paths ยท v. Stirling, Bell and Partition Numbers ยท vi. Burnside and Pรณlya ยท vii. Generating Functions ยท viii. Counting Graphs and Trees ยท ix. Young Tableaux and RSK

16. Linear Algebra

i. Matrices and Matrix Power ยท ii. Gaussian Elimination, Rank and Inverse ยท iii. Determinants and Permanents ยท iv. Characteristic Polynomial ยท v. Sparse and Structured Matrices

17. Polynomials and FFT

i. Fast Fourier Transform ยท ii. Number-Theoretic Transform ยท iii. Convolution Variants ยท iv. Online Convolution ยท v. Power Series Operations ยท vi. Multipoint Evaluation and Interpolation ยท vii. Composition and Taylor Shift ยท viii. Polynomial GCD and Resultant ยท ix. Counting with Power Series ยท x. Linear Recurrences ยท xi. Polynomial Factorization

18. Strings

i. String Basics and Parsing ยท ii. String Hashing ยท iii. Prefix Function and Z-Function ยท iv. Trie and Aho-Corasick ยท v. Other Pattern Matching ยท vi. Suffix Array and LCP ยท vii. Suffix Automaton ยท viii. Suffix Tree ยท ix. Burrows-Wheeler Transform ยท x. Palindromes and Manacher ยท xi. Eertree and Palindromic Factorization ยท xii. Lyndon Words and Minimal Rotation ยท xiii. Runs and Periodicity

19. Geometry

i. Points, Vectors and Orientation ยท ii. Lines and Segments ยท iii. Triangles ยท iv. Polygons ยท v. Convex Hull ยท vi. Dynamic Convex Hull ยท vii. Rotating Calipers ยท viii. Convex Polygon Queries and Tangents ยท ix. Half-Plane Intersection ยท x. Minkowski Sum ยท xi. Polygon Clipping and Union ยท xii. Circles and Inversion ยท xiii. Circle Union and Angular Sweep ยท xiv. Minimum Enclosing Circle ยท xv. Sweep Line and Segment Intersections ยท xvi. Closest Pair ยท xvii. Manhattan Geometry ยท xviii. Point Location and Point Counting ยท xix. 3D Geometry and 3D Hull ยท xx. Delaunay and Voronoi ยท xxi. Planar Graphs and Faces

20. Game Theory

i. Combinatorial Games and Nim ยท ii. Sprague-Grundy ยท iii. Hackenbush and Partizan Games ยท iv. Loopy Games and Retrograde Analysis ยท v. Game DP, Minimax and Game Search ยท vi. Games on Graphs and Matchings

i. Backtracking and Pruning ยท ii. Meet in the Middle ยท iii. A*, IDA* and Branch and Bound ยท iv. Exact Cover and Dancing Links ยท v. Maximum Clique and Independent Set ยท vi. Satisfiability ยท vii. Steiner Tree

22. Randomized and Heuristics

i. Randomized Algorithms ยท ii. Heuristic Optimization

23. Numerical Methods

i. Floating Point ยท ii. Big Integers and Fractions ยท iii. Root Finding and Integration ยท iv. Linear Programming

24. Theory and Beyond CP

i. Complexity Theory and NP-Hardness ยท ii. Approximation Algorithms ยท iii. Theoretical Algorithms Catalog

25. Puzzles

i. Tower of Hanoi ยท ii. Water Jug ยท iii. Wolf Goat Cabbage ยท iv. Bridge Crossing ยท v. 100 Prisoners ยท vi. Monty Hall ยท vii. Secretary Problem ยท viii. Poisoned Wine ยท ix. Celebrity Problem ยท x. Puzzle Index by Technique ยท xi. Josephus Problem ยท xii. Counterfeit Coin and 12 Coins ยท xiii. Egg Dropping ยท xiv. Lights Out ยท xv. Prisoners and Hats ยท xvi. River Crossings ยท xvii. Knightโ€™s Tour and N Queens ยท xviii. Coupon Collector ยท xix. Birthday Problem ยท xx. Gamblerโ€™s Ruin ยท xxi. Two Envelopes Paradox ยท xxii. Boy or Girl and Bertrandโ€™s Box ยท xxiii. St. Petersburg Paradox ยท xxiv. Buffonโ€™s Needle ยท xxv. Ballot Problem ยท xxvi. Stable Marriage ยท xxvii. Blue-Eyed Islanders and Muddy Children ยท xxviii. Pirates and Gold ยท xxix. 100 Doors (Lockers) ยท xxx. Rope Burning and Hourglasses ยท xxxi. Bachetโ€™s Weights ยท xxxii. Chicken McNugget (Frobenius Coin Problem) ยท xxxiii. Bachetโ€™s Game and the 21 Game ยท xxxiv. Wythoffโ€™s Game ยท xxxv. Chomp and Hex ยท xxxvi. Seven Bridges of Kรถnigsberg ยท xxxvii. Mutilated Chessboard and Tilings ยท xxxviii. Fifteen Puzzle ยท xxxix. Rubikโ€™s Cube and Permutation Puzzles ยท xl. Peg Solitaire ยท xli. Sudoku ยท xlii. Magic Squares ยท xliii. Latin Squares and Eulerโ€™s 36 Officers ยท xliv. Kirkmanโ€™s Schoolgirl Problem ยท xlv. Party Problem (Ramsey R(3,3) = 6) ยท xlvi. Cake Cutting and Fair Division ยท xlvii. Pancake Sorting ยท xlviii. Collatz (3n + 1) ยท xlix. Wheat and Chessboard ยท l. Fibonacci Rabbits ยท li. Ant on a Rubber Rope ยท lii. Sum and Product Puzzle and Cherylโ€™s Birthday ยท liii. Zebra Puzzle (Einsteinโ€™s Riddle) ยท liv. Knights and Knaves ยท lv. 100 Prisoners and a Light Bulb ยท lvi. Bulgarian Solitaire ยท lvii. Chameleons ยท lviii. Gold Chain ยท lix. Bags of Coins in One Weighing ยท lx. Tromino Tiling ยท lxi. Coins in a Row ยท lxii. Instant Insanity ยท lxiii. Jeep Problem ยท lxiv. Monkey and Coconuts ยท lxv. Icosian Game ยท lxvi. 24 Game and Countdown ยท lxvii. Chinese Rings (Baguenaudier) ยท lxviii. Gas Station Circuit ยท lxix. Socks, Handshakes and Pigeonhole Puzzles ยท lxx. Fitch Cheney Five-Card Trick

More collections ๐Ÿšง

๐ŸŽ Before Contest ยท ๐Ÿ… Proof Techniques ยท ๐Ÿ’„ Snippets