C++ competitive programming algorithms notebook.
The library is optimized for ICPC-style competitions, where the code is manually typed from a printed notebook. In particular, macros are used to shorten the code, sacrificing readability. The required standard is C++20.
You can grab the latest compiled PDF from workflows (you need to be logged in to download assets).
Typically, at the start of a contest, one person configures the programming environment.
.vimrc
: Vim configuration, including a bind for hash verification..bashrc
: Aliases for building and testing.template.cpp
: C++ solution template. It contains macros for code shortening, a debug printer and a stack-trace-on-abort function. Only shortening macros are required by other snippets.
The C++ template features a deb
macro for printing debug information.
The macro is enabled only if LOC
is defined, which is the case if the code is compiled using b
or d
from .bashrc
.
Otherwise, everything wrapped in deb
is not executed.
Example usage
struct Elem {
int k, x;
pair<int, int> p;
DBP(k, p); // Make struct printable, output only k and p.
};
string foo = "bar";
vector<Elem> elements = {
{1, 10, {2, 3}},
{4, 10, {5, 6}},
{7, 10, {8, 9}},
};
deb(foo, elements);
:: [LINE NUMBER], foo: bar, elements: ((k: 1, p: (2, 3)), (k: 4, p: (5, 6)), (k: 7, p: (8, 9)), )
The debug printer supports everything that standard streams can handle, and additionally:
- pairs,
- iterable collections,
- structures marked with the
DBP
macro.
To keep it relatively short, there are a few unsupported edge cases:
- Commas inside braces within
deb
are not parsed properly. std::pair
containingstd::vector
cannot be printed. To resolve this, it is enough to wrap the debug printer withnamespace std
.
The C++ template triggers a segmentation fault in the handler of the SIGABRT
signal.
This causes stack-traces to be printed on assertion fails, including those defined by _GLIBCXX_DEBUG
, when compiled with d
.
The notebook includes hashes for each snippet and code block enclosed by braces {}
.
These hashes are insensitive to white-spaces and C++ comments.
The .vimrc
config binds _
to hash computation of selected lines.
To verify if a block enclosed by {}
has been typed correctly, you can simply place the cursor inside it and use the vi{_
combination to select the contents and compute the hash.
- Install
pdflatex
andpygments
if not already installed. - Run
./make.sh
. - The PDF is located in the
build
subfolder.
Due to the 4-column PDF layout, there's a 47-character limit per line with tabs set at 2 spaces, and a 28-character limit for titles. The make script will produce errors if these requirements are violated. Tabs are used for main indentation, while spaces are used for fine alignment. This allows copy-pasting users to set the indentation level to their liking.
The generate-latex.py
script creates a single LaTeX file from the source directory src
.
It also generates code hashes.
Few directives are supported:
- Source files containing
!!EXCLUDE-FILE
are excluded. - Lines containing
//!HIDE
are skipped. - Lines starting with
//!
are skipped. These are used for hidden comments, e.g. marking the source. #pragma once
and#include "...template.h"
are also skipped.
There are currently three categories of tests in the repository:
- own: new deterministic tests, fuzz-tests, and benchmarks.
- yosupo: frontends for yosupo's library-checker.
- manual: old "tests", to be removed in the future.
The tests are largely incomplete, especially for older code. However, all such snippets should be tested in practice unless specified otherwise.
The licensing situation is unclear. The notebook is a mix of code authored by the maintainer, and code adapted from various sources (mostly KACTL). The source of much of the borrowed code is noted in the comments (hidden in generated PDF to save space). If you believe that any attribution is missing, please file an issue.