Egypt7oSkaaa/CP-Templates
Competitive Programming Templates
Featured achievements
The project's latest awards
#4 Repository of the week
#10 Repository of the week
#3 Repository of the week
⚡ CP Templates
Ahmed Hossam's competitive programming reference library
57 battle-tested algorithms & data structures, organized for contests
🌐 Browse the interactive website →
Search, preview with syntax highlighting, and download any template in one click
📋 Contents
🏆 Contest · 🕸️ Graph · 🌲 Data Structures · 📊 Range Query · #️⃣ Hashing · 📈 Dynamic Programming · 📝 String · π Number Theory · 📐 Math & Geometry · ⚙️ Data Types · 🗜️ Compression · ✂️ VS Code Snippets
🏆 Contest
| Template | Description |
|---|---|
| Contest Template | Main C++ template with macros and common includes |
🕸️ Graph
| 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) |
🌲 Data Structures
| 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 |
📊 Range Query
| 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 |
#️⃣ Hashing
| 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 |
📈 Dynamic Programming
| 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 |
📝 String
| Template | Description |
|---|---|
| KMP | Knuth-Morris-Pratt pattern matching in O(n+m) |
π Number Theory
| 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 |
📐 Math & Geometry
| 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 |
⚙️ Data Types
| Template | Description |
|---|---|
| Modular Integer | Integer wrapper type with automatic modular arithmetic |
| Big Integer | Arbitrary precision integer with arithmetic operations |
🗜️ Compression
| Template | Description |
|---|---|
| Coordinate Compression | Map large sparse coordinate values to compact range |
✂️ VS Code Snippets
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 |
🔗 Additional Resources
| Resource | Description |
|---|---|
| Stress Testing | Stress testing setup for solution validation |
Made with ❤️ by Ahmed Hossam
Similar Projects
Discover other similar projects and AI models.
A repository with over 7000 solutions to more than 1800 Leetcode problems written in C++, Python, Java, Javascript, Rust & Golang.
📚 C/C++ 技术面试基础知识总结,包括语言、程序库、数据结构、算法、系统、网络、链接装载库等知识及面试经验、招聘、内推等信息。This repository is a summary of the basic knowledge of recruiting job seekers and beginners in the direction of C/C++ technology, including language, program library, data structure, algorithm, system, network, link loading library, interview experience, recruitment, recommendation, etc.
Collection of various algorithms in mathematics, machine learning, computer science and physics implemented in C++ for educational purposes.
OpenPose: Real-time multi-person keypoint detection library for body, face, hands, and foot estimation
PlayStation 4 emulator for Windows, Linux, macOS and FreeBSD written in C++