Quickstart · about 15 minutes
CP Lang for Python programmers
Six short problems. Each shows how you'd write it in Python, 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 / 6 · read, ranges as input contracts, print
Sum of a list
How you'd write it in Python
n = int(input()) print(sum(map(int, input().split())))Read n (1 ≤ n ≤ 2·10⁵) and then n integers between -10⁹ and 10⁹. Print their sum.
3 tests · Ctrl+EnterWhat's different from Python
- Types are written where data enters: at
readand in function signatures. Everywhere else they are inferred. - This compiles to C++, so it runs at C++ speed.
- Types are written where data enters: at
2 / 6 · var, while, two pointers
Pairs with a target sum
How you'd write it in Python
i, j, pairs = 0, n - 1, 0 while i < j: s = a[i] + a[j] if s == x: pairs += 1; i += 1; j -= 1 elif s < x: i += 1 else: j -= 1Read n and x, then n distinct integers in increasing order. Count the pairs i < j with a[i] + a[j] = x.
3 tests · Ctrl+EnterWhat's different from Python
letis a constant,varis a variable. Most names arelet.elifandwhileread the same as in Python.
3 / 6 · domains, grids, ?Int and ??
Nearest exit on a grid
How you'd write it in Python
dist = [[-1] * w for _ in range(h)] q = deque([s]); dist[s[0]][s[1]] = 0 while q: r, c = q.popleft() for dr, dc in ((1,0),(-1,0),(0,1),(0,-1)): nr, nc = r + dr, c + dc if 0 <= nr < h and 0 <= nc < w and ...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+EnterWhat's different from Python
NoneandSome(x)are checked at compile time: you cannot add 1 to a distance that might beNone.
4 / 6 · functions, Mod<P>, memoized recursion
Count paths in a DAG
How you'd write it in Python
@lru_cache(None) def paths(v): if v == n: return 1 return sum(paths(w) for w in adj[v]) % MOD # plus sys.setrecursionlimit and threading for deep graphsRead 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+EnterWhat's different from Python
- No recursion limit: recursion depth is limited by the stack, which judge profiles set to the judge's size.
5 / 6 · the library and operation values
Range minimum with updates
How you'd write it in Python
# Usually a hand-written iterative segment tree, and often too slow in CPython.Read n values and q queries: "1 i x" sets a[i] = x, "2 l r" prints the minimum of a[l..r].
runslet (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 Python
SegTreeis in the standard library and compiles to a flat array.
6 / 6 · @!multitest and why state can't leak
Several test cases
How you'd write it in Python
for _ in range(int(input())): solve()Problem 3 again, but the input starts with t, the number of test cases.
1 tests · Ctrl+EnterWhat's different from Python
- The single-test and multitest programs differ by one line.
You've read six programs
Next: problems with verdicts, or look things up.