sgtlaugh's blog

By sgtlaugh, history, 3 hours ago, In English

Greetings Codeforces community!

Since 2020 I've kept a repository of competitive programming templates, and I finally cleaned it up enough to share: 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 is an example from the Linear Algebra section.

Motivation

There are great libraries already. KACTL is built for ICPC team notebooks, so its code is short enough to print, and 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 by indy256 and algorithmic-library by zimpha.

However, what I wanted was slightly different. As a contestant, I prefer templates that are simple, well-tested, fast. 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 efficient.

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

Usage

Using a template is quite easy:

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.

The full header

Practice Problem

The last note is a recipe. 319C: Kalila and Dimna in the Logging Industry is exactly that recurrence, $$$dp_i = \min_{j \lt 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.

Solution to 319C

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: Two strings that collide under a polynomial hash. Useful for hacking solutions.
  • Monotone Minima and SMAWK: Row minima of a Monge or monotone matrix.
  • Fast Prime Counting: Number of primes up to $$$10^{12}$$$ in 0.05 s, with Meissel-Lehmer.
  • Gauss on a Band Matrix: 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: Insert patterns online with no rebuild.
  • Nelder-Mead: Local minimum of a function of a few real variables, no derivatives needed.
  • Plug DP: Count or optimize cycles and paths on a grid.
  • Network Simplex: 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:
  • Practice problem lists:
  • Reading the code to learn:
  • Probably none of it:

What stops you from using someone else's library?

  • Don't trust it:
  • Too long to paste or read:
  • Different style from mine:
  • I just use my own:

Hope this helps and thanks for reading. Cheers!

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By sgtlaugh, history, 8 years ago, In English

Hello,

Greetings good people of Codeforces. I cordially invite you all to take part in the online mirror of 2018-2019 ACM-ICPC, Asia Dhaka Regional Contest. The contest will be held on Saturday, January 19, 2019 at 16:00 (UTC+6).

The onsite contest took place on Saturday, November 10, 2018, where 300 teams competed for a spot in the 2019 ACM ICPC World Finals. I know its been a while, but hey its better late than never.

Contest duration will be 5 hours and will follow standard ICPC rules. The problem set consists of 10 problems and was prepared and tested by Jami_CSEDU, shovonshovo, sgtlaugh, dragoon, nfssdq, raihatneloy, sn23581, SnapDragon, Shahriar Manzoor, Monirul Hasan, Imran Bin Azad and Mehdi Rahman. Moreover, special thanks to Rujia Liu for reviewing and also forthright48 and ridowan007.

Note to any one who participated in the onsite contest, please refrain from sharing or discussing the problems here before the contest. The analysis will be posted once the online mirror ends and afterwards we can all discuss about the problems here.

I hope you enjoy the contest. Cheers!

UPD: BUMP! 24 hours to go, buckle up!

UPD: Contest has started, see you in the arena.

Editorial: https://docs.google.com/document/d/1VEz9q-pXK2KuRJh2WwtlhmdQ882nFDMCdi-7ZOxE47w/edit?usp=sharing

Full text and comments »

  • Vote: I like it
  • +89
  • Vote: I do not like it