A practical collection of C++ templates for common competitive-programming data structures, graph algorithms, modular arithmetic, and combinatorics. Each file is intended to be copied into a contest solution and adapted to the problem at hand.
| Topic | File | Highlights |
|---|---|---|
| Binary lifting | Binary_Lifiting.cpp |
k-th ancestor and lowest common ancestor (LCA) queries in O(log n) after O(n log n) preprocessing. |
| Disjoint-set union | DSU.cpp |
Path compression and union by size. |
| Dijkstra — heap | Dijkstra - O(E*log(V)).cpp |
Shortest paths on non-negative weighted graphs in O(E log V). |
| Dijkstra — dense graph | Dijkstra - O(v^2).cpp |
Array-based Dijkstra for dense graphs, O(V^2). |
| Fenwick tree | Fenwick_Tree.cpp |
Point updates and prefix/range-sum queries in O(log n). |
| Segment tree | Segment_Tree.cpp |
Range sum queries, point assignment, and lazy range-add updates. |
| Flexible segment tree | Segment_Tree_Flexible.cpp |
Lazy segment tree parameterized with sum, minimum, maximum, or XOR nodes. |
| Matrix operations | Matrix.cpp |
Generic addition, multiplication, and fast matrix exponentiation. |
| Modular arithmetic | Modular.cpp |
Type-safe modular integers and exponentiation; defaults to 1,000,000,007. |
| Modular inverse | Mod_Inverse.cpp |
Fermat-based modular inverse for a prime modulus. |
| Combinations and permutations | PnC.cpp |
Factorial precomputation with nCr and nPr modulo 1,000,000,007. |
| Combinatorics helper | Combinatorics.cpp |
On-demand factorial/inverse-factorial helper; use together with Modular.cpp. |
| Starter template | Template.cpp |
Fast-I/O contest scaffold with PBDS support. |
- Copy the relevant class, struct, or function into your solution.
- Remove or replace the placeholder
main()/solve()supplied in the snippet. - Match the input indexing expected by that implementation before calling it.
Most files use #include <bits/stdc++.h>; several also use GNU PBDS. Compile with GCC in GNU C++17 mode:
g++ -std=gnu++17 -O2 -pipe "DSU.cpp" -o solutionFenwick_Tree.cppexposes a 0-indexed interface:add(idx, value),query(idx), andquery(left, right).DSU.cppallocates elements from1throughn.- The segment-tree implementations use inclusive query ranges.
- Dijkstra's algorithm requires all edge weights to be non-negative.
Combinatorics.cppdepends on theInttype declared byModular.cpp; include the modular implementation first when combining them.- The modular inverse and
nCr/nPrhelpers assume a prime modulus when using Fermat's little theorem.
Add new snippets as self-contained, readable .cpp files. Please include a short usage example or a minimal API, state index conventions clearly, and keep the time-complexity guarantees apparent from the implementation.