Ahmed Hossam's competitive programming reference library
57 battle-tested algorithms & data structures, organized for contests
Search, preview with syntax highlighting, and download any template in one click
🏆 Contest · 🕸️ Graph · 🌲 Data Structures · 📊 Range Query · #️⃣ Hashing · 📈 Dynamic Programming · 📝 String · π Number Theory · 📐 Math & Geometry · ⚙️ Data Types · 🗜️ Compression · ✂️ VS Code Snippets
| Template | Description |
|---|---|
| Contest Template | Main C++ template with macros and common includes |
| Template | Description |
|---|---|
| Graph Traversal | DFS & BFS on adjacency list graphs |
| Graph Representation | Adjacency list and adjacency matrix representations |
| Dijkstra's Algorithm | Single-source shortest path for non-negative weighted graphs |
| Floyd-Warshall | All-pairs shortest path in O(V³) |
| Prim's MST | Minimum spanning tree using priority queue |
| Bellman-Ford | SSSP supporting negative edge weights and cycle detection |
| Centroid Decomposition | Divide-and-conquer on trees via centroid |
| LCA — Binary Lifting | Lowest Common Ancestor with O(log n) queries |
| LCA Weighted | LCA with path distance queries on weighted trees |
| Tarjan's SCC | Strongly connected components, bridges, and articulation points |
| Kuhn's Matching | Maximum bipartite matching via augmenting paths |
| Link Cut Tree | Dynamic tree connectivity with path queries in O(log n) |
| Template | Description |
|---|---|
| Binary Search Tree | BST with insert, delete, search operations |
| Segment Tree | Point update, range query in O(log n) |
| Segment Tree — Lazy | Range update with lazy propagation in O(log n) |
| Persistent Segment Tree | Immutable versioned segment tree for historical queries |
| Segment Tree 2D | 2D segment tree for 2D range queries and updates |
| Fenwick Tree (BIT) | Binary indexed tree for prefix sum queries in O(log n) |
| Fenwick Tree 2D | 2D BIT for 2D range sum queries |
| Fenwick Tree Range | BIT supporting both range updates and range queries |
| Sparse Table | Static RMQ in O(1) with O(n log n) preprocessing |
| DSU / Union-Find | Disjoint set union with path compression and union by rank |
| Heavy Light Decomposition | Path and subtree queries on trees via HLD |
| Ordered Set | Policy-based order statistics tree: find_by_order, order_of_key |
| Trie | Prefix tree for string insert and search |
| Binary Trie | Bit-based trie for XOR maximum and queries |
| Monotonic Stack | Next greater/smaller element queries in O(n) |
| Monotonic Queue | Sliding window min/max in O(n) |
| Implicit Splay Tree | Implicit key splay tree for sequence split/merge operations |
| Splay Tree | Self-adjusting BST with amortized O(log n) operations |
| Heap | Binary heap / priority queue implementation |
| Template | Description |
|---|---|
| SQRT Decomposition | Bucket decomposition for range queries in O(√n) |
| Mo's Algorithm | Offline range queries sorted by Mo's ordering in O((n+q)√n) |
| Mo's on Trees | Mo's algorithm adapted for tree path queries |
| Template | Description |
|---|---|
| Rolling Hash | Polynomial rolling hash for O(1) substring comparisons |
| Hashed Deque | Deque with rolling hash support for sliding window hashing |
| Hash Segment Tree | Segment tree storing hash values for range hash queries |
| Template | Description |
|---|---|
| Digit DP | Count integers in [L,R] satisfying digit-based predicates |
| Convex Hull Trick | Li Chao tree / CHT for linear DP transition optimization |
| Kadane's Algorithm | Maximum subarray sum in O(n) |
| Manacher's Algorithm | All palindromic substrings in O(n) |
| Prefix Sum 2D | 2D prefix sums for O(1) rectangle sum queries |
| Partial Sum 2D | 2D difference array for O(1) rectangle updates |
| Template | Description |
|---|---|
| KMP | Knuth-Morris-Pratt pattern matching in O(n+m) |
| Template | Description |
|---|---|
| Sieve of Eratosthenes | Find all primes up to N in O(N log log N) |
| Sieve + Prime Factors | Linear sieve precomputing smallest prime factor per number |
| Factorization | Trial division and factorization algorithms |
| Miller-Rabin | Deterministic primality test for large numbers |
| Template | Description |
|---|---|
| Convex Hull | Andrew's monotone chain convex hull in O(n log n) |
| Math Utilities | GCD, LCM, extended Euclidean and number utilities |
| Matrix Exponentiation | Fast matrix power for linear recurrences in O(k³ log n) |
| Modular Inverse | Modular inverse via Fermat's little theorem |
| Geometry Points | 2D point struct with cross/dot product and angle operations |
| Ternary Search | Find minimum/maximum of a unimodal function |
| Template | Description |
|---|---|
| Modular Integer | Integer wrapper type with automatic modular arithmetic |
| Big Integer | Arbitrary precision integer with arithmetic operations |
| Template | Description |
|---|---|
| Coordinate Compression | Map large sparse coordinate values to compact range |
Drop any file from snippets/ into your project's .vscode/ folder to activate it.
| Snippet File | Covers |
|---|---|
| template.code-snippets | CP contest template, Google template |
| graphs.code-snippets | Dijkstra, Floyd, LCA, HLD, Tarjan, LCT, Centroid, MoTree |
| trees.code-snippets | Segment tree, Fenwick, Trie, Sparse table, PST, Splay |
| data_structures.code-snippets | DSU, Heap, Monotonic stack/queue, Ordered set, Sqrt decomp |
| math.code-snippets | Miller-Rabin, ModInt, BigInt, Sieve, Matrix power |
| dp.code-snippets | Digit DP, Kadane |
| strings.code-snippets | KMP, Manacher, Rolling hash |
| geometry.code-snippets | Point struct, Convex hull, Convex hull trick |
| algorithms.code-snippets | Mo's algorithm, Coord compression, Ternary search |
| Resource | Description |
|---|---|
| Stress Testing | Stress testing setup for solution validation |
Made with ❤️ by Ahmed Hossam