Everything contest-only in one place: the pre/during/post-contest checklist, the statement-pattern lookup, common mistakes, and small tricks. Nothing here is a technique on its own — each row points at the note that actually explains it.
Checklist
Before the contest
- Template compiles; fast I/O; debug macro prints to
stderronly (C++ for CP). - Stress-test script ready: brute force, generator, checker (Debugging).
- Team: reference document printed, roles agreed (Contest Strategy).
Reading
- Read every problem’s statement and constraints; note the easiest by the scoreboard.
- For each problem: input limits target complexity (see Techniques by Pattern below).
- Re-read samples; solve one sample by hand before designing.
Before coding
- I can state the algorithm in two sentences and its complexity fits.
- I know why it is correct (or I have a brute force to stress it).
- Edge cases listed: , empty, all equal, max values, negatives, disconnected.
Before submitting
-
long longwhere needed; mod applied everywhere. - Multi-test: every global reset.
- Array sizes and indices checked; no debug output on
stdout. - Ran samples and a max test for time.
- Output format (spaces, newlines, YES/Yes, precision).
After a wrong answer
- Re-read the statement; check every item above, and the Common Mistakes table below.
- Stress test against the brute force on small random inputs.
- Explain the solution to a teammate (or out loud); check the proof.
After the contest
- Upsolve every problem you could almost solve; write the missing idea down (Problem Set).
Techniques by pattern
Statement pattern first technique to try. Constraints decide as much as wording: see Complexity and Analysis for “what fits in one second”.
By wording
| The statement says | Try first | Note |
|---|---|---|
| minimise the maximum / maximise the minimum | binary search on the answer | Binary Search |
| minimum number of operations, answer looks tiny | prove the answer is or , check the small cases | Small Answers |
| ”is it possible to turn A into B” | invariants, canonical forms | Reachability |
| output any valid object or | necessary conditions, then a construction | Constructive |
| sum over all subarrays / pairs / subsets | contribution of each element | Contribution |
| count objects avoiding a property | complement, inclusion–exclusion | Inclusion–Exclusion |
| expected value | linearity of expectation, indicators | Probability |
| nearest greater / smaller, spans, histograms | monotonic stack | Monotonic Stack |
| longest / shortest subarray with a monotone condition | two pointers | Two Pointers |
| range queries, no updates | prefix sums, sparse table | Sparse Table |
| range queries with updates | Fenwick or segment tree (lazy for range updates) | Segment Tree |
| many offline range queries, hard to merge | Mo’s algorithm | Mo |
| connectivity under edge additions | DSU | DSU |
| path queries on a tree | LCA, HLD, Euler tour | HLD |
| count paths with a property in a tree | centroid decomposition, small to large | Centroid |
| shortest path, weights 0/1 or states | 0-1 BFS, Dijkstra on a state graph | Shortest Paths |
| choose a subset, pairwise conflicts, bipartite | matching, min vertex cover (König) | Bipartite Matching |
| choose items with dependencies, profits and costs | project selection = min cut | Flow Modeling |
| boolean constraints on pairs | 2-SAT | 2-SAT |
| xor of subsets, max xor | linear basis over GF(2) | XOR Basis |
| count numbers in with a digit property | digit DP | Digit DP |
| , subsets | bitmask DP, SOS DP, meet in the middle | Bitmask DP |
| huge , linear recurrence | matrix power, Berlekamp–Massey | Matrix Power DP |
| convex hull trick, Li Chao | CHT | |
| ”exactly items” on a convex cost | Aliens trick | Aliens |
| , | harmonic lemma / floor blocks | Floor Sums |
| polynomial products, counting by size | FFT / NTT | FFT |
| impartial game | Sprague–Grundy | Sprague–Grundy |
| string equality checks, many substrings | hashing, prefix function, suffix array | Hashing |
By constraints
| Constraint | Typical complexity | Typical tools |
|---|---|---|
| permutations, backtracking | ||
| bitmask DP | ||
| meet in the middle | ||
| Floyd–Warshall, interval DP | ||
| quadratic DP, bitsets for – | ||
| , | sorting, segment trees, Mo | |
| sieves, linear scans | ||
| , | divisors, floor blocks, Du / min_25 sieves | |
| matrix power, binary lifting, closed forms |
Common mistakes
One line per mistake, with the fix. Full checklists: Debugging and Stress Testing, C++ for CP.
| Mistake | Symptom | Fix |
|---|---|---|
int overflow in products or sums | WA on large tests | long long for anything that can exceed ; 1LL * before products |
product of two long long mod overflows | WA for | __int128 or Montgomery |
negative % | negative residues | ((a % m) + m) % m |
| integer division of negatives rounds toward zero | off by one for negatives | floorDiv / ceilDiv helpers |
sz(v) - 1 with unsigned size() | huge loop when empty | cast to int (sz macro) |
| not resetting globals between test cases | WA on test 2 | clear exactly the used part, per test |
memset on every test | TLE with many tests | reset only entries |
endl in loops | TLE | '\n' |
| recursion depth – | RE (stack overflow) | iterative DFS, or ulimit -s locally only |
map / set where a vector suffices | TLE | sort + lower_bound, arrays |
unordered_map with adversarial keys | TLE (anti-hash tests) | splitmix64 custom hash |
comparator not a strict weak order (<=) | RE / WA in sort | always < |
| floating equality | WA on precision tests | integers, or compare with |
| binary search off by one | infinite loop or wrong bound | write the invariant: lo is always bad, hi always good |
| 1-indexed vs 0-indexed mixing | WA on borders | choose one convention per problem |
| forgetting , empty input, all equal | WA on edge tests | test the edges before submitting |
| graph not connected, multi-edges, self-loops | WA on hidden tests | handle each explicitly |
| wrong modulus or forgetting a final mod | WA | one const int MOD and a Mint type |
| array too small by one or by a factor of 2 (segment tree) | RE / WA | 4n for recursive segment trees; N + 5 padding |
| reading the problem too fast | solving the wrong problem | re-read the statement and the samples before coding |
| debug output left in | WA / TLE | print debug to stderr with a macro |
Tricks list
Small tricks that turn up everywhere, each with its use and the note that explains it.
| Trick | Use | Note |
|---|---|---|
| rotate coordinates | Manhattan distance becomes Chebyshev (max of two) | Attacking a Problem |
| prefix sums / difference arrays | range sums, range additions offline | Prefix Sums |
| prefix xor | subarray xor = xor of two prefixes | Prefix Sums |
| sort + two pointers | pairs with a sum condition in | Two Pointers |
| coordinate compression | values up to into indices | Sorting |
| contribution technique | sum over all subarrays as a sum over elements | Contribution |
| small to large | merge sets in total | Small to Large |
| sqrt split by frequency / degree | heavy and light elements handled differently | Sqrt Decomposition |
| binary lifting | -th ancestor, jumps in functional graphs | LCA |
| reverse time | deletions become insertions (DSU) | Offline Techniques |
| parallel binary search | many binary searches over the same updates | Parallel Binary Search |
| Kruskal reconstruction tree | queries “max edge on the path” / thresholds | KRT |
| Euler tour flattening | subtree queries become range queries | Euler Tour |
submask enumeration s = (s - 1) & m | over all masks | Submasks |
| bitset shift-or | subset sums and boolean DP in | Word-Level Parallelism |
| random hashing of sets (xor of random 64-bit keys) | compare multisets / subarrays in | Randomized Algorithms |
| doubling the array | cyclic problems on a line | Classic Array Problems |
| sentinel values | remove border checks | C++ for CP |
| takes values | sums over divisors and quotients | Floor Sums |
| ternary search on a convex function | optimum of unimodal functions | Ternary Search |
| slope trick | convex piecewise-linear DP with heaps | Slope Trick |
| Aliens trick | remove “exactly ” by a penalty | Aliens Trick |
| Chtholly tree (ODT) | range assignment with random data | Chtholly Tree |
| answer is small | check 0, 1, then the universal bound | Small Answers |
| meet in the middle | instead of | Meet in the Middle |