Quickstart · about 15 minutes

CP Lang for C++ programmers

Six short problems. Each shows how you'd write it in C++, then the CP Lang solution. Edit it, break it, submit it: your browser judges it. A solution the compiler does not run yet is shown without a judge.

  1. 1 / 6 · read, ranges as input contracts, print

    Sum of a list

    How you'd write it in C++
    int n; cin >> n;
    long long s = 0;
    for (int i = 0; i < n; i++) { long long x; cin >> x; s += x; }
    cout << s << "\n";

    Read n (1 ≤ n ≤ 2·10⁵) and then n integers between -10⁹ and 10⁹. Print their sum.

    3 tests · Ctrl+Enter

    What's different from C++

    • The range -1e9..=1e9 is the statement's constraint. The compiler proves the sum needs 64 bits, so there is no long long to forget.
    • In a debug run, input outside the range traps with the line and the rule.
  2. 2 / 6 · var, while, two pointers

    Pairs with a target sum

    How you'd write it in C++
    int i = 0, j = n - 1, pairs = 0;
    while (i < j) {
      long long s = a[i] + a[j];
      if (s == x) pairs++, i++, j--;
      else if (s < x) i++;
      else j--;
    }

    Read n and x, then n distinct integers in increasing order. Count the pairs i < j with a[i] + a[j] = x.

    3 tests · Ctrl+Enter

    What's different from C++

    • a[i] + a[j] can reach 2·10⁹. The compiler knows both bounds, so the sum gets a width that holds it; no overflow by accident.
  3. 3 / 6 · domains, grids, ?Int and ??

    Nearest exit on a grid

    How you'd write it in C++
    vector<vector<int>> dist(h, vector<int>(w, -1));
    queue<pair<int,int>> q; q.push(s); dist[s.first][s.second] = 0;
    while (!q.empty()) { auto [r, c] = q.front(); q.pop();
      for (auto [dr, dc] : dirs) { int nr = r + dr, nc = c + dc;
        if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue; ... } }

    Read an h × w grid of '.', '#', one 'S' and some 'E'. Print the fewest steps from S to any E, moving up, down, left or right, or -1.

    3 tests · Ctrl+Enter

    What's different from C++

    • p.adj4 yields only the neighbours inside the grid. There are no bounds checks to get wrong.
    • ?Int replaces the -1 sentinel: an unreached cell is None, and ?? supplies the answer when there is none.
  4. 4 / 6 · functions, Mod<P>, memoized recursion

    Count paths in a DAG

    How you'd write it in C++
    const int MOD = 1e9 + 7;
    vector<long long> memo(n + 1, -1);
    function<long long(int)> paths = [&](int v) -> long long {
      if (v == n) return 1;
      if (memo[v] != -1) return memo[v];
      long long t = 0;
      for (int w : adj[v]) t = (t + paths(w)) % MOD;
      return memo[v] = t;
    };

    Read a DAG with n vertices and m edges u → v. Print the number of paths from 1 to n modulo 10⁹ + 7.

    3 tests · Ctrl+Enter

    What's different from C++

    • Mod<1_000_000_007> does the reduction on every operation; a missing % MOD can't be written.
    • The memo table is sized from the parameter's type V. In 0.3 the decorator is spelled @cache.
  5. 5 / 6 · the library and operation values

    Range minimum with updates

    How you'd write it in C++
    struct SegTree { int n; vector<int> t;
      void set(int i, int x) { for (t[i += n] = x; i > 1; i >>= 1) t[i >> 1] = min(t[i], t[i ^ 1]); }
      int query(int l, int r) { /* half-open, 0-based */ }
    };

    Read n values and q queries: "1 i x" sets a[i] = x, "2 l r" prints the minimum of a[l..r].

    runs
    let (n, q): (1..=2e5, 1..=2e5) = read()
    domain V = 1..=n
    let a: [V]0..=1e9 = read()
    var st = SegTree(min, a)
    for _ in 0..q:
        let t: 1..=2 = read()
        if t == 1:
            let (i, x): (V, 0..=1e9) = read()
            st.set(i, x)
        else:
            let (l, r): (V, V) = read()
            print(st.fold(l..=r))

    What's different from C++

    • The tree is indexed by V, the same 1-based positions as the input: no l - 1 conversions.
    • l..=r is inclusive and says so.
  6. 6 / 6 · @!multitest and why state can't leak

    Several test cases

    How you'd write it in C++
    int t; cin >> t;
    while (t--) {
      // every global array must be cleared here, or the last test leaks into this one
    }

    Problem 3 again, but the input starts with t, the number of test cases.

    1 tests · Ctrl+Enter

    What's different from C++

    • @!multitest reads t and runs the rest of the file once per test. Everything below it is fresh each time, so state can't leak.

You've read six programs

Next: problems with verdicts, or look things up.