Quickstart · about 15 minutes
CP Lang for Java programmers
Six short problems. Each shows how you'd write it in Java, 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 Java
int n = in.nextInt(); long s = 0; for (int i = 0; i < n; i++) s += in.nextLong(); System.out.println(s);Read n (1 ≤ n ≤ 2·10⁵) and then n integers between -10⁹ and 10⁹. Print their sum.
3 tests · Ctrl+EnterWhat's different from Java
- No
ScannerorBufferedReader:read()takes its shape from the declared type. - No class or
mainmethod: a program is its top-level statements.
- No
2 / 6 · var, while, two pointers
Pairs with a target sum
How you'd write it in Java
int i = 0, j = n - 1, pairs = 0; while (i < j) { 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+EnterWhat's different from Java
var (i, j) = (0, n - 1)declares two variables at once from a tuple.
3 / 6 · domains, grids, ?Int and ??
Nearest exit on a grid
How you'd write it in Java
int[][] dist = new int[h][w]; for (int[] row : dist) Arrays.fill(row, -1); ArrayDeque<int[]> q = new ArrayDeque<>(); // four directions, bounds checks, ...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 Java
- Arrays are indexed by the grid's own row and column types, so a row index can't be used as a column.
4 / 6 · functions, Mod<P>, memoized recursion
Count paths in a DAG
How you'd write it in Java
long[] memo = new long[n + 1]; Arrays.fill(memo, -1); long paths(int v) { if (v == n) return 1; if (memo[v] != -1) return memo[v]; long t = 0; for (int w : adj.get(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+EnterWhat's different from Java
- No memo array to allocate and fill with -1: the decorator owns it.
5 / 6 · the library and operation values
Range minimum with updates
How you'd write it in Java
int[] t = new int[2 * n]; void set(int i, int x) { for (t[i += n] = x; i > 1; i >>= 1) t[i >> 1] = Math.min(t[i], t[i ^ 1]); }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 Java
- The operation is a value:
Monoid.(identity, op)
- The operation is a value:
6 / 6 · @!multitest and why state can't leak
Several test cases
How you'd write it in Java
int t = in.nextInt(); while (t-- > 0) solve();Problem 3 again, but the input starts with t, the number of test cases.
1 tests · Ctrl+EnterWhat's different from Java
- No
solve()method to extract.
- No
You've read six programs
Next: problems with verdicts, or look things up.