Skip to main content

CP Code Library

Your Contest Arsenal

This chapter contains battle-tested implementations ready for copy-paste in contests. Each snippet is optimized for competitive programming: minimal boilerplate, correct edge case handling, and reliable under contest time pressure.
How to Use This Library:
  1. Understand the algorithm first (previous chapters)
  2. Copy the implementation during contests
  3. Modify as needed for the specific problem
  4. Test locally before submitting — even trusted templates can need adjustments for specific index conventions (0-based vs 1-based) or data types
Common pitfall with library code: Blindly copy-pasting without adapting to the problem’s indexing, data type, or modulus will cause WA. Always double-check: does this problem use 0-indexed or 1-indexed nodes? Is int sufficient or do I need long long? Is the modulus 10^9+7 or 998244353?

Number Theory

GCD, LCM, Extended GCD

Modular Arithmetic

Sieve of Eratosthenes

Prime Factorization

Combinatorics (nCr with Precomputation)


Data Structures

Disjoint Set Union (DSU)

Segment Tree (Point Update, Range Query)

Lazy Segment Tree (Range Update, Range Query)

Fenwick Tree (BIT)

A Fenwick tree (Binary Indexed Tree) supports point updates and prefix sum queries in O(log n). It uses half the memory of a segment tree and has a much smaller constant factor, making it the preferred choice when you only need prefix sums (no range min/max). When to use Fenwick over Segment Tree: Fenwick is simpler and faster for sum queries. Use a segment tree only when you need range updates (lazy propagation), range min/max, or other non-invertible operations.

Sparse Table (Static RMQ)

O(n log n) build, O(1) query for idempotent operations (min, max, GCD, OR, AND). Does NOT support updates—use a segment tree if you need updates. Why “idempotent” matters: An operation is idempotent if applying it to overlapping ranges gives the correct answer. For min: min(min(a,b,c), min(b,c,d)) = min(a,b,c,d). For sum, this fails: overlapping ranges would double-count. That is why sparse table gives O(1) for min/max/GCD but not for sum. When to use over segment tree: When there are no updates and you need the fastest possible queries. Sparse table O(1) queries beat segment tree O(log n) queries, which matters when there are 10^6+ queries.

Trie


Graph Algorithms

Dijkstra’s Algorithm

Bellman-Ford

Floyd-Warshall

Topological Sort (Kahn’s Algorithm)

LCA (Binary Lifting)

Kruskal’s MST


String Algorithms

Z-Function

KMP (Failure Function)

String Hashing


Geometry

Point and Vector

Convex Hull

Andrew’s monotone chain algorithm: Build lower hull left-to-right, then upper hull right-to-left. Uses cross product to determine turn direction: positive = left turn (keep), zero = collinear, negative = right turn (remove last point). Edge cases: If all points are collinear, the “hull” is a line segment. If n < 3, return all points. Duplicate points should be handled by the <= in the cross product check (use < to keep collinear points on the hull boundary).

Miscellaneous

Coordinate Compression

Random Number Generation

Custom Comparator for Priority Queue


Key Takeaways

Tested Code

All snippets are battle-tested in real contests.

Copy-Paste Ready

Implementations are self-contained and ready to use.

Understand First

Know the algorithm before using the code.

Customize

Modify as needed for your specific problem.

Back to Course Overview

Review the complete course curriculum.