Skip to content
  • Competitive Programming & Fast I/O · C++

Competitive Programming & Fast I/O

LearnViaNotes @learnvianotes· 4 min read

C++ dominates contests because of speed and the STL. A few habits — fast I/O, long long, the right container — prevent most wrong answers and time-limit errors.

The template

C++ — contest template (GCC)
#include <bits/stdc++.h>          // GCC-only “everything” header; clang/MSVC: include what you use
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);   // don't sync C++ streams with C stdio
    cin.tie(nullptr);              // don't flush cout before every cin

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<ll> a(n);
        for (auto& x : a) cin >> x;
        ll best = *max_element(a.begin(), a.end());
        ll sum = accumulate(a.begin(), a.end(), 0LL);
        cout << best << ' ' << sum << '\n';     // '\n', never endl in hot loops
    }
}
C++ — the same, portable
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<ll> a(n);
        for (auto& x : a) cin >> x;
        cout << *max_element(a.begin(), a.end()) << ' ' << accumulate(a.begin(), a.end(), 0LL) << '\n';
    }
}
Output
5 8
1000000000 2000000000

Habits that save you

TrapFix
int overflow (sums, products > 2·10⁹)long long (ll) everywhere sums live; 1LL * a * b
Slow I/O with 10⁶ numberssync_with_stdio(false) + cin.tie(nullptr), '\n' instead of endl
unordered_map hacked with collisionsCustom hash with a random seed, or map
Recursion depth 10⁵–10⁶Iterative DFS with an explicit stack
Large local arrays → stack overflowGlobal arrays or vector
Comparing doublesUse an epsilon, or stay in integers
v.size() - 1 when emptyCast: (int)v.size() - 1
Modular arithmetic(a % M + M) % M for negatives; multiply in long long

Handy snippets

C++
#include <algorithm>
#include <bit>
#include <iostream>
#include <numeric>
#include <vector>
using ll = long long;
constexpr ll MOD = 1'000'000'007;

ll power(ll base, ll exp, ll mod) {                  // fast modular exponentiation
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

std::vector<int> sieve(int n) {                      // primes up to n
    std::vector<bool> composite(n + 1);
    std::vector<int> primes;
    for (int i = 2; i <= n; ++i) {
        if (composite[i]) continue;
        primes.push_back(i);
        for (ll j = 1LL * i * i; j <= n; j += i) composite[j] = true;
    }
    return primes;
}

struct DSU {                                         // union–find
    std::vector<int> parent, size;
    explicit DSU(int n) : parent(n), size(n, 1) { std::iota(parent.begin(), parent.end(), 0); }
    int find(int x) { return parent[x] == x ? x : parent[x] = find(parent[x]); }
    bool unite(int a, int b) {
        a = find(a); b = find(b);
        if (a == b) return false;
        if (size[a] < size[b]) std::swap(a, b);
        parent[b] = a; size[a] += size[b];
        return true;
    }
};

int main() {
    std::cout << power(2, 1'000'000, MOD) << '\n';
    auto p = sieve(30);
    std::cout << p.size() << ' ' << p.back() << '\n';
    DSU d(5);
    d.unite(0, 1); d.unite(3, 4); d.unite(1, 4);
    std::cout << (d.find(0) == d.find(3)) << (d.find(2) == d.find(0)) << '\n';
    std::cout << __builtin_popcount(13) << ' ' << __builtin_ctz(8) << ' '
              << std::popcount(13u) << ' ' << std::bit_width(1000u) - 1 << '\n';   // portable <bit> versions
}
Output
235042059
10 29
10
3 3 3 9

Operations per second

nFeasible complexity (≈10⁸ simple ops/s)
≤ 10–12O(n!)
≤ 20–25O(2ⁿ)
≤ 500O(n³)
≤ 5 000O(n²)
≤ 10⁶O(n log n)
≤ 10⁸O(n)
> 10⁹O(log n) or O(1) — math

What do ios::sync_with_stdio(false) and cin.tie(nullptr) do?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report