RepositoryC++EGEgypt
7oSkaaa

7oSkaaa/CP-Templates

Competitive Programming Templates

167stars
35forks
1issues
2views

Featured achievements

The project's latest awards

4
TINIX TRENDING·Topic: problem-solving

#4 Repository of the week

10
TINIX TRENDING·Topic: competitive-programming

#10 Repository of the week

3
TINIX TRENDING·Topic: problem-solving

#3 Repository of the week

⚡ CP Templates

Ahmed Hossam's competitive programming reference library
57 battle-tested algorithms & data structures, organized for contests

Stars Forks Language Website


       



🌐 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

TemplateDescription
Contest TemplateMain C++ template with macros and common includes

🕸️ Graph

TemplateDescription
Graph TraversalDFS & BFS on adjacency list graphs
Graph RepresentationAdjacency list and adjacency matrix representations
Dijkstra's AlgorithmSingle-source shortest path for non-negative weighted graphs
Floyd-WarshallAll-pairs shortest path in O(V³)
Prim's MSTMinimum spanning tree using priority queue
Bellman-FordSSSP supporting negative edge weights and cycle detection
Centroid DecompositionDivide-and-conquer on trees via centroid
LCA — Binary LiftingLowest Common Ancestor with O(log n) queries
LCA WeightedLCA with path distance queries on weighted trees
Tarjan's SCCStrongly connected components, bridges, and articulation points
Kuhn's MatchingMaximum bipartite matching via augmenting paths
Link Cut TreeDynamic tree connectivity with path queries in O(log n)

🌲 Data Structures

TemplateDescription
Binary Search TreeBST with insert, delete, search operations
Segment TreePoint update, range query in O(log n)
Segment Tree — LazyRange update with lazy propagation in O(log n)
Persistent Segment TreeImmutable versioned segment tree for historical queries
Segment Tree 2D2D segment tree for 2D range queries and updates
Fenwick Tree (BIT)Binary indexed tree for prefix sum queries in O(log n)
Fenwick Tree 2D2D BIT for 2D range sum queries
Fenwick Tree RangeBIT supporting both range updates and range queries
Sparse TableStatic RMQ in O(1) with O(n log n) preprocessing
DSU / Union-FindDisjoint set union with path compression and union by rank
Heavy Light DecompositionPath and subtree queries on trees via HLD
Ordered SetPolicy-based order statistics tree: find_by_order, order_of_key
TriePrefix tree for string insert and search
Binary TrieBit-based trie for XOR maximum and queries
Monotonic StackNext greater/smaller element queries in O(n)
Monotonic QueueSliding window min/max in O(n)
Implicit Splay TreeImplicit key splay tree for sequence split/merge operations
Splay TreeSelf-adjusting BST with amortized O(log n) operations
HeapBinary heap / priority queue implementation

📊 Range Query

TemplateDescription
SQRT DecompositionBucket decomposition for range queries in O(√n)
Mo's AlgorithmOffline range queries sorted by Mo's ordering in O((n+q)√n)
Mo's on TreesMo's algorithm adapted for tree path queries

#️⃣ Hashing

TemplateDescription
Rolling HashPolynomial rolling hash for O(1) substring comparisons
Hashed DequeDeque with rolling hash support for sliding window hashing
Hash Segment TreeSegment tree storing hash values for range hash queries

📈 Dynamic Programming

TemplateDescription
Digit DPCount integers in [L,R] satisfying digit-based predicates
Convex Hull TrickLi Chao tree / CHT for linear DP transition optimization
Kadane's AlgorithmMaximum subarray sum in O(n)
Manacher's AlgorithmAll palindromic substrings in O(n)
Prefix Sum 2D2D prefix sums for O(1) rectangle sum queries
Partial Sum 2D2D difference array for O(1) rectangle updates

📝 String

TemplateDescription
KMPKnuth-Morris-Pratt pattern matching in O(n+m)

π Number Theory

TemplateDescription
Sieve of EratosthenesFind all primes up to N in O(N log log N)
Sieve + Prime FactorsLinear sieve precomputing smallest prime factor per number
FactorizationTrial division and factorization algorithms
Miller-RabinDeterministic primality test for large numbers

📐 Math & Geometry

TemplateDescription
Convex HullAndrew's monotone chain convex hull in O(n log n)
Math UtilitiesGCD, LCM, extended Euclidean and number utilities
Matrix ExponentiationFast matrix power for linear recurrences in O(k³ log n)
Modular InverseModular inverse via Fermat's little theorem
Geometry Points2D point struct with cross/dot product and angle operations
Ternary SearchFind minimum/maximum of a unimodal function

⚙️ Data Types

TemplateDescription
Modular IntegerInteger wrapper type with automatic modular arithmetic
Big IntegerArbitrary precision integer with arithmetic operations

🗜️ Compression

TemplateDescription
Coordinate CompressionMap 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 FileCovers
template.code-snippetsCP contest template, Google template
graphs.code-snippetsDijkstra, Floyd, LCA, HLD, Tarjan, LCT, Centroid, MoTree
trees.code-snippetsSegment tree, Fenwick, Trie, Sparse table, PST, Splay
data_structures.code-snippetsDSU, Heap, Monotonic stack/queue, Ordered set, Sqrt decomp
math.code-snippetsMiller-Rabin, ModInt, BigInt, Sieve, Matrix power
dp.code-snippetsDigit DP, Kadane
strings.code-snippetsKMP, Manacher, Rolling hash
geometry.code-snippetsPoint struct, Convex hull, Convex hull trick
algorithms.code-snippetsMo's algorithm, Coord compression, Ternary search

🔗 Additional Resources

ResourceDescription
Stress TestingStress testing setup for solution validation

Made with ❤️ by Ahmed Hossam

Similar Projects

Discover other similar projects and AI models.

RepositoryJava
AnasImloul/Leetcode-Solutions

A repository with over 7000 solutions to more than 1800 Leetcode problems written in C++, Python, Java, Javascript, Rust & Golang.

405
Dec 2, 2026
RepositoryPython
practical-tutorials/project-based-learning

Curated list of project-based tutorials

290.9k
Nov 30, 2026
RepositoryJavaScript
CyC2018/CS-Notes

:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计

186.6k
Nov 27, 2026
RepositoryC++
ClickHouse/ClickHouse

ClickHouse® is a real-time analytics database management system

51.3k
Dec 1, 2026
RepositoryC++
Light-City/CPlusPlusThings

C++那些事

43.6k
Oct 13, 2026
RepositoryC++
huihut/interview

📚 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.

38.3k
Sep 17, 2026
RepositoryC++
TheAlgorithms/C-Plus-Plus

Collection of various algorithms in mathematics, machine learning, computer science and physics implemented in C++ for educational purposes.

34.9k
Nov 30, 2026
RepositoryC++
CMU-Perceptual-Computing-Lab/openpose

OpenPose: Real-time multi-person keypoint detection library for body, face, hands, and foot estimation

34.6k
Aug 3, 2024
RepositoryC++
shadps4-emu/shadPS4

PlayStation 4 emulator for Windows, Linux, macOS and FreeBSD written in C++

34.4k
Nov 28, 2026