A C++ template for contests
ICPC Bolivia Committee
In a contest, the time you spend writing boilerplate is time you are not spending thinking. A template agreed on beforehand saves several minutes per problem.
The minimal template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t; // uncomment if the problem has multiple test cases
while (t--) {
// solution
}
return 0;
}
That is all most problems need. Resist the temptation to drag along two hundred lines of macros: a template you do not fully understand is a source of bugs, not an advantage.
Why those two lines in main
ios::sync_with_stdio(false) turns off synchronization between the C++ streams and the
C ones. cin.tie(nullptr) stops cin from forcing a flush of cout before every
read.
Together they make cin/cout comparable to scanf/printf in speed. On a problem
with hundreds of thousands of input lines, the difference decides between accepted and
time limit exceeded.
Overflow: the most expensive mistake
The most frequent mistake in a contest is not algorithmic, it is about types. In practice
int reaches only about 2 × 10⁹; a sum of 10⁵ elements of size 10⁵ already overflows.
The alias using ll = long long; is in the template precisely so that using it costs two
characters. When in doubt, use ll: the memory cost is irrelevant compared with a
penalty.
The team notebook
Anything that takes more than ten minutes to write from memory should be printed in the team notebook: data structures, graph algorithms, modular arithmetic, basic geometry.
The 2026 rules allow printed material in the quantity determined by the site. Confirm the local limit before competing and prepare the notebook as part of your training, not as a last-minute formality.
More material in resources and in the CP-Algorithms guides.