Quickstart · about 15 minutes
CP Lang for Rust programmers
Six short problems. Each shows how you'd write it in Rust, 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 Rust
let n: usize = it.next().unwrap().parse().unwrap(); let s: i64 = (0..n).map(|_| it.next().unwrap().parse::<i64>().unwrap()).sum(); 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 Rust
- No
unwrapandparsechains:read()parses into the declared type. - Integer widths come from ranges, not from
i32ori64.
- No
2 / 6 · var, while, two pointers
Pairs with a target sum
How you'd write it in Rust
let (mut i, mut j, mut pairs) = (0, n - 1, 0); while i < j { let s = a[i] + a[j]; if s == x { pairs += 1; i += 1; j -= 1; } else if s < x { i += 1 } else { j -= 1 } }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 Rust
varislet mut;letis immutable, as in Rust.
3 / 6 · domains, grids, ?Int and ??
Nearest exit on a grid
How you'd write it in Rust
let mut dist = vec![vec![None; w]; h]; let mut q = VecDeque::from([s]); while let Some((r, c)) = q.pop_front() { for (dr, dc) in [(0, 1), (1, 0), (0, -1), (-1, 0)] { /* checked_add, bounds */ } }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 Rust
?IntisOption, but!traps with a source-level report instead of a panic backtrace.
4 / 6 · functions, Mod<P>, memoized recursion
Count paths in a DAG
How you'd write it in Rust
fn paths(v: usize, adj: &[Vec<usize>], memo: &mut [Option<u64>]) -> u64 { if let Some(x) = memo[v] { return x; } let t = adj[v].iter().map(|&w| paths(w, adj, memo)).sum::<u64>() % MOD; memo[v] = Some(t); 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 Rust
- No
&mut memothreaded through every call.
- No
5 / 6 · the library and operation values
Range minimum with updates
How you'd write it in Rust
// A hand-rolled segment tree struct, or a crate the judge may not have.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 Rust
- Generic over the operation without traits or impl blocks.
6 / 6 · @!multitest and why state can't leak
Several test cases
How you'd write it in Rust
for _ in 0..t { solve(&mut it); }Problem 3 again, but the input starts with t, the number of test cases.
1 tests · Ctrl+EnterWhat's different from Rust
- No state struct to reset.
You've read six programs
Next: problems with verdicts, or look things up.