Algovault: Templates With Tests for Their Tests
Разница между en8 и en9, 35 символ(ов) изменены
Greetings Codeforces community!↵
↵
Since 2020 I've kept a repository of competitive programming templates, and I finally cleaned it up enough to share: [algovault](https://github.com/sgtlaugh/algovault).↵
↵
### What it is↵
↵
Most of it is my personal collection of algorithms and templates I have learned and used in programming contests over the years. A little bit about myself: I've been doing competitive programming as a hobby since 2010, competed in the World Finals, authored several problems, and coordinated multiple ICPC regionals.↵
↵
The library consists of about 270 C++ templates, plus a few in Python. There are also 2,000+ practice problems from various online judges, each linked to its template. [Here](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/linear_algebra/README.md) is an example from the Linear Algebra section.↵
↵
### Motivation↵
↵
There are great libraries already. [KACTL](https://github.com/kth-competitive-programming/kactl) is built for ICPC team notebooks, so its code is short enough to print, and [cp-algorithms](https://github.com/cp-algorithms/cp-algorithms), which grew out of e-maxx.ru, is the best place to learn an algorithm. There are also some good personal template collections online. My personal favorites are [codelibrary](https://github.com/indy256/codelibrary) by [user:indy256,2026-12-02] and [algorithmic-library](https://github.com/zimpha/algorithmic-library) by [user:zimpha,2026-12-02].↵
↵
However, what I wanted was slightly different. As a contestant, I prefer templates that are simple, well-tested, fast
, and m. Most importantly, they should be easy to reuse. Essentially, a template I can paste into a solution easily during a contest without reading it first, and trust it to be correct and fasefficient.↵
↵
### What's different↵
↵
- **Verified:** checked against brute force, stress tests, and planted bugs in mutation testing.↵
- **Readable:** a one-minute header and plain code with no macros.↵
- **Self-tested:** every template's `main()` holds its self-tests and a worked example.↵
- **Drop-in:** every file compiles on its own, no dependencies.↵
- **Fast:** tuned and benchmarked with documented speed tricks.↵
- **Practiced:** 2,000+ solved problems linked to their templates.↵
- **Guarded:** asserts instead of silent wrong answers.↵
↵
### [Example: Convex Hull Trick](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/data_structures/convex_hull_trick.cpp)↵
↵
#### Usage↵
↵
Using a template is quite easy:↵
↵
```cpp↵
ConvexHullTrick<long long> hull;    // minimum; ConvexHullTrick<long long, true> for the maximum↵
↵
hull.add_line(k, b);                // slopes in non-increasing order↵
hull.query(x);                      // any x, O(log n)↵
hull.query_increasing(x);           // non-decreasing x, O(1) amortized↵
```↵
↵
#### Header↵
↵
Every template opens with a header you can read in a minute: what it does, the complexity, the API, and the limits where it stops being correct.↵
↵
<spoiler summary="The full header">↵
↵
```cpp↵
/***↵
 *↵
 * Convex Hull Trick↵
 * Minimum or maximum of lines y = k * x + b at a point, for lines added in slope order↵
 *↵
 * Complexity: O(1) amortized per add_line and query_increasing, O(log n) per query↵
 *↵
 * ConvexHullTrick<T> hull: no lines yet, answers the minimum↵
 * ConvexHullTrick<T, true> hull: the same hull answering the maximum↵
 * hull.add_line(k, b): add y = k * x + b, k at most every earlier slope for the minimum, at least for the maximum↵
 * hull.query(x): the minimum over the lines at x, any x in any order, at least one line↵
 * hull.query_increasing(x): the same, for x never smaller than in the previous query_increasing call↵
 *↵
 * Notes:↵
 *   - Every k * x + b must fit in T, like |k|, |x| <= 1e9 and |b| <= 1e18 for long long↵
 *   - The crossing test multiplies in __int128, so the compiler must support it (64-bit GCC or Clang)↵
 *   - |k| < 2^62 keeps that product in range↵
 *   - T may be double or long double, every answer then within rounding of the exact optimum↵
 *   - Slopes in any order or lines only on part of the range: li_chao_tree.cpp↵
 *   - dp[i] = min over j < i of dp[j] + a[j] * x[i], a non-increasing: query(x[i]), then add_line(a[i], dp[i])↵
 *↵
***/↵
```↵
↵
</spoiler>↵
↵
#### Practice Problem↵
↵
The last note is a recipe. [319C: Kalila and Dimna in the Logging Industry](https://codeforces.me/problemset/problem/319/C) is exactly that recurrence, $dp_i = \min_{j < i} (dp_j + b_j a_i)$, with $b$ as the slopes and $a$ as the query points, so the whole solution is the template plus a loop.↵
↵
<spoiler summary="Solution to 319C">↵
↵
```cpp↵
// convex_hull_trick.cpp pasted here, everything above its main()↵
↵
int main(){↵
    int n;↵
    scanf("%d", &n);↵
↵
    vector<long long> a(n), b(n), dp(n);↵
    for (auto& x : a) scanf("%lld", &x);↵
    for (auto& x : b) scanf("%lld", &x);↵
↵
    ConvexHullTrick<long long> hull;↵
    for (int i = 0; i < n; i++){↵
        if (i) dp[i] = hull.query_increasing(a[i]);↵
        hull.add_line(b[i], dp[i]);↵
    }↵
↵
    printf("%lld\n", dp[n - 1]);↵
    return 0;↵
}↵
```↵
↵
</spoiler>↵
↵
#### Assertion↵
↵
Add a slope out of order and you get a runtime error on the test that broke it, instead of a quiet wrong answer:↵
↵
```↵
Assertion `k.empty() || slope <= k.back()' failed.↵
```↵
↵
### Niche stuff↵
↵
Besides the usual segment trees and flows, here are a few templates you don't see in most libraries:↵
↵
- [Anti Polynomial Hash](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/hacking/anti_polynomial_hash.cpp): Two strings that collide under a polynomial hash. Useful for hacking solutions.↵
- [Monotone Minima and SMAWK](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/dp/monotone_minima.cpp): Row minima of a Monge or monotone matrix.↵
- [Fast Prime Counting](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/number_theory/fast_prime_counting.cpp): Number of primes up to $10^{12}$ in `0.05 s`, with Meissel-Lehmer.↵
- [Gauss on a Band Matrix](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/linear_algebra/gauss_band_matrix.cpp): Expected values of a random walk on an $n \times m$ grid in $O(nmr^2)$ instead of $O(n^3m^3)$, where $r$ is the reach of each cell's equation (about $\min(n, m)$).↵
- [Dynamic Aho-Corasick](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/strings/dynamic_aho_corasick.cpp): Insert patterns online with no rebuild.↵
- [Nelder-Mead](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/numerical/local_minimization.cpp): Local minimum of a function of a few real variables, no derivatives needed.↵
- [Plug DP](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/dp/plug_dp.cpp): Count or optimize cycles and paths on a grid.↵
- [Network Simplex](https://github.com/sgtlaugh/algovault/blob/cd2456108c51b0cd844bda9d67bc854e7ddc953d/code_library/graphs/network_simplex.cpp): Min cost flow with supplies, lower bounds and negative costs. This is several times faster than a min cost circulation in practice.↵
↵
### Feedback↵
↵
I'd love to hear what's missing or wrong. If you find an input where a template gives a wrong answer, comment below or open an issue, and it goes into the tests with credit to you.↵
↵
**Which part would you use most?**↵
↵
- Templates to copy: [likes:1,templates]↵
- Practice problem lists: [likes:1,practice]↵
- Reading the code to learn: [likes:1,learn]↵
- Probably none of it: [likes:1,none]↵
↵
**What stops you from using someone else's library?**↵
↵
- Don't trust it: [likes:2,trust]↵
- Too long to paste or read: [likes:2,long]↵
- Different style from mine: [likes:2,style]↵
- I just use my own: [likes:2,own]↵
↵
Hope this helps and thanks for reading. Cheers!↵

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en9 Английский sgtlaugh 2026-12-02 08:51:57 35 (published)
en8 Английский sgtlaugh 2026-12-02 08:49:20 236
en7 Английский sgtlaugh 2026-12-02 08:38:19 206
en6 Английский sgtlaugh 2026-12-02 08:30:33 1086 Tiny change: '^{12}$ in 0.05 s, with Mei' -> '^{12}$ in `0.05 s`, with Mei'
en5 Английский sgtlaugh 2026-12-02 08:15:49 1975 Tiny change: '_hash.cpp) - two strin' -> '_hash.cpp): two strin'
en4 Английский sgtlaugh 2026-12-02 08:00:17 6182
en3 Английский sgtlaugh 2026-12-02 07:45:14 609 Tiny change: 'ent.\n\n- Readable: a one-min' -> 'ent.\n\n- **Readable:** a one-min'
en2 Английский sgtlaugh 2026-12-02 06:55:23 1313
en1 Английский sgtlaugh 2026-12-02 06:31:37 585 Initial revision (saved to drafts)