Compare commits
2
Commits
e821df15f4
...
210c7c38f7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
210c7c38f7 | ||
|
|
1aee0050a0 |
@@ -8,4 +8,11 @@ skipped/*/main
|
|||||||
.current
|
.current
|
||||||
.usaco_history
|
.usaco_history
|
||||||
.current_stack
|
.current_stack
|
||||||
|
.lang
|
||||||
a.out
|
a.out
|
||||||
|
|
||||||
|
# Generated cargo scaffolding: exists only so rust-analyzer/clippy can see
|
||||||
|
# the solutions. "usaco lint" (or any rust build) recreates it.
|
||||||
|
Cargo.toml
|
||||||
|
Cargo.lock
|
||||||
|
target/
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ site in `site/` (see [site/README.md](site/README.md)).
|
|||||||
## Layout
|
## Layout
|
||||||
|
|
||||||
```
|
```
|
||||||
template.cpp starting template for every problem
|
template.cpp starting template for C++ problems
|
||||||
|
template.rs starting template for Rust problems
|
||||||
site/ the website (Go server + Vue SPA), deployed as its own container
|
site/ the website (Go server + Vue SPA), deployed as its own container
|
||||||
solving/<name>/ problems in progress
|
solving/<name>/ problems in progress
|
||||||
solved/<name>/ finished problems
|
solved/<name>/ finished problems
|
||||||
@@ -16,17 +17,54 @@ skipped/<name>/ problems set aside for later
|
|||||||
|
|
||||||
The website's CLI page (`site/web/src/views/CliPage.vue`) is a condensed copy of the Commands section below; keep it in step.
|
The website's CLI page (`site/web/src/views/CliPage.vue`) is a condensed copy of the Commands section below; keep it in step.
|
||||||
|
|
||||||
Each problem dir contains `main.cpp` plus `<io>.in` / `<io>.out` files (USACO file IO is wired up automatically via `STDIO("<io>")`). Optional extras: `.url` (the problem link), `tests/N.in` + `N.out`, `notes.md` (shown on the website, usually written from there), `problem.md` (the statement, saved or fetched from the website's editor) and `explain.md` (an explanation the website had Claude write).
|
Each problem dir contains `main.cpp` *or* `main.rs` plus `<io>.in` / `<io>.out` files. File IO is wired up automatically — `STDIO("<io>")` in C++, `stdio("<io>")` in Rust. A problem's language is simply whichever source file it has, so every command (`run`, `test`, `cat`, `edit`, ...) figures it out on its own. Optional extras: `.url` (the problem link), `tests/N.in` + `N.out`, `notes.md` (shown on the website, usually written from there), `problem.md` (the statement, saved or fetched from the website's editor, or by a LeetCode import) and `explain.md` (an explanation the website had Claude write).
|
||||||
|
|
||||||
|
## Languages
|
||||||
|
|
||||||
|
```
|
||||||
|
./usaco lang # show the default language for new problems
|
||||||
|
./usaco lang rust # set it (stored in .lang)
|
||||||
|
./usaco new foo --rust # or pick per problem (--cpp / --rust)
|
||||||
|
USACO_LANG=cpp ./usaco new foo # or per shell
|
||||||
|
```
|
||||||
|
|
||||||
|
Switch an existing problem's language with `./usaco reset <name> rust` (or `template`) — the old source is kept as a `.bak`. LeetCode imports are always C++.
|
||||||
|
|
||||||
|
**C++** compiles with `g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL`, **Rust** with `rustc -O --edition 2021` (plain `rustc`, no Cargo, so the solution stays a single submittable file). Override the flags via `CXXFLAGS` / `RUSTCFLAGS` in `lib/common.sh`.
|
||||||
|
|
||||||
|
### The Rust template
|
||||||
|
|
||||||
|
`template.rs` is self-contained and paste-ready for any judge. It gives you:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let (mut sc, mut out) = stdio("problem"); // <- usaco new wires the name in
|
||||||
|
let n: usize = sc.next(); // one token, any FromStr type
|
||||||
|
let v: Vec<i64> = sc.vec(n); // n tokens
|
||||||
|
let s: Vec<u8> = sc.bytes(); // a word as bytes
|
||||||
|
writeln!(out, "{}", join(&v, " ")).unwrap();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`stdio(name)` picks its mode from the directory it runs in:
|
||||||
|
|
||||||
|
- `name.in` exists → reads `name.in`, writes `name.out` (USACO, and what `usaco run` / `usaco test` use)
|
||||||
|
- `name.in` missing → reads stdin, writes stdout (paste straight into Codeforces)
|
||||||
|
|
||||||
|
So the same file works locally and on a judge with no edits. Also included: a `Scanner` that slurps the whole input up front and hands out parsed tokens (`next`, `vec`, `pair`, `pairs`, `bytes`, `chars`), `yesno`, `join`, `remax`/`remin`, and the usual `std::collections` imports. `dbg!(x)` goes to stderr, so it never pollutes the `.out` file.
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
### Start a problem
|
### Start a problem
|
||||||
```
|
```
|
||||||
./usaco new diamond-collector
|
./usaco new diamond-collector
|
||||||
./usaco new diamond-collector diamond # if the freopen name differs
|
./usaco new diamond-collector diamond # if the file-io name differs
|
||||||
./usaco new diamond-collector --url https://usaco.org/index.php?page=viewproblem2&cpid=639
|
./usaco new diamond-collector --url https://usaco.org/index.php?page=viewproblem2&cpid=639
|
||||||
```
|
```
|
||||||
Creates `solving/<name>/main.cpp` from the template and an empty `.in` file, then opens it in `$EDITOR`. `--url` saves the problem link for `open`.
|
```
|
||||||
|
./usaco new diamond-collector --rust # Rust instead of the default
|
||||||
|
```
|
||||||
|
Creates `solving/<name>/main.cpp` (or `main.rs`) from the template and an empty `.in` file, then opens it in `$EDITOR`. `--url` saves the problem link for `open`.
|
||||||
|
|
||||||
### Start a LeetCode problem
|
### Start a LeetCode problem
|
||||||
```
|
```
|
||||||
@@ -49,9 +87,9 @@ Limits: when the statement says "any order", a correct answer in a different ord
|
|||||||
./usaco run diamond-collector # compile + run, then print the .out file
|
./usaco run diamond-collector # compile + run, then print the .out file
|
||||||
./usaco build diamond-collector # compile only
|
./usaco build diamond-collector # compile only
|
||||||
```
|
```
|
||||||
Compiles with `g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL` and runs inside the problem dir so file IO just works. Put your test input in the `.in` file first.
|
Compiles the problem's source (C++ or Rust) and runs it inside the problem dir, feeding it `<io>.in` on stdin, so both file-IO and stdin/stdout solutions work. Put your test input in the `.in` file first.
|
||||||
|
|
||||||
Add `--debug` to `run`, `build` or `test` (or set `USACO_DEBUG=1`) for a sanitizer build (`-fsanitize=address,undefined`, `_GLIBCXX_DEBUG`): out-of-bounds indexing, overflow and other UB crash with a stack trace instead of silently giving wrong answers.
|
Add `--debug` to `run`, `build` or `test` (or set `USACO_DEBUG=1`) for a checked build. C++ gets sanitizers (`-fsanitize=address,undefined`, `_GLIBCXX_DEBUG`): out-of-bounds indexing, overflow and other UB crash with a stack trace instead of silently giving wrong answers. Rust gets `-C overflow-checks -C debug-assertions`, so integer overflow panics instead of wrapping.
|
||||||
|
|
||||||
**Debug output:** `LOCAL` is only defined by `./usaco` builds, so the template's `#ifdef LOCAL #include "debugging.h"` (pretty-printers for vectors, maps, pairs…) and its `DBG(x)` macros vanish on the judge — submit `main.cpp` as-is.
|
**Debug output:** `LOCAL` is only defined by `./usaco` builds, so the template's `#ifdef LOCAL #include "debugging.h"` (pretty-printers for vectors, maps, pairs…) and its `DBG(x)` macros vanish on the judge — submit `main.cpp` as-is.
|
||||||
|
|
||||||
@@ -76,7 +114,7 @@ The website commits too: marking a problem solved/skipped, or editing its link o
|
|||||||
|
|
||||||
### Edit
|
### Edit
|
||||||
```
|
```
|
||||||
./usaco vim diamond-collector # open main.cpp in $EDITOR (defaults to nvim)
|
./usaco vim diamond-collector # open the solution in $EDITOR (defaults to nvim)
|
||||||
./usaco e diamond-collector # same thing, shorter (also: edit)
|
./usaco e diamond-collector # same thing, shorter (also: edit)
|
||||||
```
|
```
|
||||||
Opens from inside the problem dir, so the `.in`/`.out` files are right there.
|
Opens from inside the problem dir, so the `.in`/`.out` files are right there.
|
||||||
@@ -90,9 +128,9 @@ Opens from inside the problem dir, so the `.in`/`.out` files are right there.
|
|||||||
|
|
||||||
### Snippets
|
### Snippets
|
||||||
```
|
```
|
||||||
./usaco cat diamond-collector # print main.cpp to stdout (pipeable)
|
./usaco cat diamond-collector # print the solution to stdout (pipeable)
|
||||||
./usaco cat diam | wl-copy # e.g. pipe it wherever you want
|
./usaco cat diam | wl-copy # e.g. pipe it wherever you want
|
||||||
./usaco copy diamond-collector # copy main.cpp to the clipboard (alias: cp)
|
./usaco copy diamond-collector # copy the solution to the clipboard (alias: cp)
|
||||||
```
|
```
|
||||||
`copy` auto-detects the clipboard tool: `wl-copy` on Wayland, then `xclip`/`xsel` on X11, and falls back to an OSC52 terminal escape that works even over SSH.
|
`copy` auto-detects the clipboard tool: `wl-copy` on Wayland, then `xclip`/`xsel` on X11, and falls back to an OSC52 terminal escape that works even over SSH.
|
||||||
|
|
||||||
@@ -116,13 +154,37 @@ The selection persists across sessions in `.current` (cleared automatically if t
|
|||||||
|
|
||||||
### Misc
|
### Misc
|
||||||
```
|
```
|
||||||
./usaco reset diamond-collector # fresh main.cpp from template (old one -> main.cpp.bak), delete binary + .out
|
./usaco reset diamond-collector # fresh source from template (old one -> .bak), delete binary + .out
|
||||||
# asks first (-y skips); keeps tests/ and .in (aliases: template, rm)
|
# asks first (-y skips); keeps tests/ and .in (aliases: template, rm)
|
||||||
./usaco io diamond-collector # show the io name; 'io <name> <new>' renames .in/.out and rewires STDIO
|
./usaco reset diamond-collector rust # ...and switch the problem to Rust
|
||||||
./usaco clean # delete compiled binaries (main, a.out) and .bak files everywhere
|
./usaco io diamond-collector # show the io name; 'io <name> <new>' renames .in/.out and rewires STDIO/stdio
|
||||||
./usaco path diamond-collector # print the problem's directory
|
./usaco clean # delete compiled binaries (main, a.out) and .bak files everywhere
|
||||||
|
./usaco lint # clippy over every Rust solution (see "Rust linting" below)
|
||||||
|
./usaco lint excel-sheet # ...or just one
|
||||||
|
./usaco lint template # ...or template.rs itself
|
||||||
|
./usaco path diamond-collector # print the problem's directory
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust linting
|
||||||
|
|
||||||
|
Problems are bare `main.rs` files, and rust-analyzer only does real analysis
|
||||||
|
(type-aware diagnostics, clippy on save) inside a cargo project. `usaco lint`
|
||||||
|
generates a root `Cargo.toml` that declares every Rust solution, plus
|
||||||
|
`template.rs`, as its own `[[bin]]`, which is enough to give the editor a
|
||||||
|
project to attach to. Any Rust build regenerates it too, so it stays in step as
|
||||||
|
problems are created or moved.
|
||||||
|
|
||||||
|
Nothing compiles through cargo — `usaco build`/`run` still call `rustc`
|
||||||
|
directly. The manifest is purely for tooling, so it's gitignored along with
|
||||||
|
`Cargo.lock` and `target/`; run `usaco lint` once after a fresh clone to
|
||||||
|
recreate it. The lint selection (clippy pedantic, minus the cast and
|
||||||
|
missing-docs noise) lives in `sync_cargo_manifest()` in `lib/common.sh`.
|
||||||
|
|
||||||
|
Editor side: rust-analyzer must actually be installed (`rustup component add
|
||||||
|
rust-analyzer` — the rustup shim is on `$PATH` even when the component isn't,
|
||||||
|
so it fails silently), and `check.command` has to be set to `clippy`, since the
|
||||||
|
default is plain `cargo check`.
|
||||||
|
|
||||||
## Shell integration (recommended)
|
## Shell integration (recommended)
|
||||||
|
|
||||||
Add to `~/.zshrc`:
|
Add to `~/.zshrc`:
|
||||||
@@ -140,8 +202,9 @@ This gives you:
|
|||||||
./usaco new the-lost-cow --url <link>
|
./usaco new the-lost-cow --url <link>
|
||||||
./usaco open the-lost-cow # read the problem
|
./usaco open the-lost-cow # read the problem
|
||||||
# paste sample input into solving/the-lost-cow/the-lost-cow.in
|
# paste sample input into solving/the-lost-cow/the-lost-cow.in
|
||||||
# edit solving/the-lost-cow/main.cpp
|
# edit solving/the-lost-cow/main.rs (or main.cpp)
|
||||||
./usaco run the-lost-cow # iterate until the output is right
|
./usaco run the-lost-cow # iterate until the output is right
|
||||||
|
./usaco test create the-lost-cow # lock the sample in as a test case
|
||||||
./usaco done the-lost-cow # 🎉 (auto-commits)
|
./usaco done the-lost-cow # 🎉 (auto-commits)
|
||||||
./usaco sync # publish it on the website
|
./usaco sync # publish it on the website
|
||||||
```
|
```
|
||||||
|
|||||||
+1
-1
@@ -2,4 +2,4 @@
|
|||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"
|
||||||
build_problem "$dir"
|
build_problem "$dir"
|
||||||
echo "Built $dir/main"
|
echo "Built $dir/main ${DIM}($(lang_of "$dir"))${RST}"
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
# usaco cat <name>
|
# usaco cat <name>
|
||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"
|
||||||
cat "$dir/main.cpp"
|
cat "$(main_file "$dir")"
|
||||||
|
|||||||
+2
-1
@@ -2,5 +2,6 @@
|
|||||||
for s in solving solved skipped; do
|
for s in solving solved skipped; do
|
||||||
[[ -d "$ROOT/$s" ]] || continue
|
[[ -d "$ROOT/$s" ]] || continue
|
||||||
find "$ROOT/$s" -mindepth 2 -maxdepth 2 -type f \
|
find "$ROOT/$s" -mindepth 2 -maxdepth 2 -type f \
|
||||||
\( -name main -o -name a.out -o -name '*.bak' \) -print -delete | sed "s|^$ROOT/|removed |"
|
\( -name main -o -name a.out -o -name '*.bak' -o -name '*.pdb' \) -print -delete \
|
||||||
|
| sed "s|^$ROOT/|removed |"
|
||||||
done
|
done
|
||||||
|
|||||||
+6
-5
@@ -1,16 +1,17 @@
|
|||||||
# usaco copy <name> (alias: cp)
|
# usaco copy <name> (alias: cp)
|
||||||
# Copies the problem's main.cpp to the clipboard.
|
# Copies the problem's solution source to the clipboard.
|
||||||
# Wayland: wl-copy; X11: xclip/xsel; last resort OSC52 escape (works over SSH).
|
# Wayland: wl-copy; X11: xclip/xsel; last resort OSC52 escape (works over SSH).
|
||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"
|
||||||
|
src="$(main_file "$dir")"
|
||||||
|
|
||||||
if [[ -n ${WAYLAND_DISPLAY:-} ]] && command -v wl-copy >/dev/null 2>&1; then
|
if [[ -n ${WAYLAND_DISPLAY:-} ]] && command -v wl-copy >/dev/null 2>&1; then
|
||||||
wl-copy < "$dir/main.cpp"
|
wl-copy < "$src"
|
||||||
elif command -v xclip >/dev/null 2>&1; then
|
elif command -v xclip >/dev/null 2>&1; then
|
||||||
xclip -selection clipboard < "$dir/main.cpp"
|
xclip -selection clipboard < "$src"
|
||||||
elif command -v xsel >/dev/null 2>&1; then
|
elif command -v xsel >/dev/null 2>&1; then
|
||||||
xsel -b --input < "$dir/main.cpp"
|
xsel -b --input < "$src"
|
||||||
else
|
else
|
||||||
# OSC52: terminal-native clipboard, also works through SSH
|
# OSC52: terminal-native clipboard, also works through SSH
|
||||||
base64 -w0 < "$dir/main.cpp" | printf '\e]52;c;%s\a' "$(cat)"
|
base64 -w0 < "$src" | printf '\e]52;c;%s\a' "$(cat)"
|
||||||
fi
|
fi
|
||||||
|
|||||||
+5
-1
@@ -4,8 +4,12 @@ dir="$(find_problem "$1")"; name="$(basename "$dir")"
|
|||||||
[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; }
|
[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; }
|
||||||
[[ -d "$ROOT/solved/$name" ]] && { echo "Error: solved/$name already exists" >&2; exit 1; }
|
[[ -d "$ROOT/solved/$name" ]] && { echo "Error: solved/$name already exists" >&2; exit 1; }
|
||||||
mv "$ROOT/solving/$name" "$ROOT/solved/$name"
|
mv "$ROOT/solving/$name" "$ROOT/solved/$name"
|
||||||
|
sync_cargo_manifest >/dev/null # the bin paths just moved
|
||||||
echo "Moved to solved/$name 🎉"
|
echo "Moved to solved/$name 🎉"
|
||||||
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||||
git -C "$ROOT" add -A "solved/$name" "solving/$name" 2>/dev/null || true
|
# separately: one pathspec that matches nothing (a problem never committed
|
||||||
|
# from solving/) would make git refuse to stage the other
|
||||||
|
git -C "$ROOT" add -A "solved/$name" 2>/dev/null || true
|
||||||
|
git -C "$ROOT" add -A "solving/$name" 2>/dev/null || true
|
||||||
git -C "$ROOT" commit -m "Solve $name" >/dev/null && echo "Committed: Solve $name ${DIM}(usaco sync to publish it)${RST}"
|
git -C "$ROOT" commit -m "Solve $name" >/dev/null && echo "Committed: Solve $name ${DIM}(usaco sync to publish it)${RST}"
|
||||||
fi
|
fi
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
# usaco vim/edit/e <name>
|
# usaco vim/edit/e <name>
|
||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"
|
||||||
|
src="$(basename "$(main_file "$dir")")"
|
||||||
# run from inside the dir so :!./main etc. work and .in files are at hand
|
# run from inside the dir so :!./main etc. work and .in files are at hand
|
||||||
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
|
(cd "$dir" && exec ${EDITOR:-nvim} "$src")
|
||||||
|
|||||||
+13
-4
@@ -10,7 +10,7 @@ HISTFILE="$ROOT/.usaco_history"
|
|||||||
# commands that take a problem name (the selection is injected when you omit it)
|
# commands that take a problem name (the selection is injected when you omit it)
|
||||||
WITH_PROBLEM="run build test done undo skip io cat copy edit open path reset"
|
WITH_PROBLEM="run build test done undo skip io cat copy edit open path reset"
|
||||||
# commands that take no problem
|
# commands that take no problem
|
||||||
NO_PROBLEM="list stats clean sync"
|
NO_PROBLEM="list stats clean sync lang"
|
||||||
# REPL-only words (used for completion and for 'help')
|
# REPL-only words (used for completion and for 'help')
|
||||||
REPL_WORDS="s use none push pop stack help quit exit"
|
REPL_WORDS="s use none push pop stack help quit exit"
|
||||||
|
|
||||||
@@ -70,7 +70,8 @@ ${DIM}selection${RST}
|
|||||||
${DIM}problem commands${RST} ${DIM}(name optional — omit it to use the selection; giving one switches to it)${RST}
|
${DIM}problem commands${RST} ${DIM}(name optional — omit it to use the selection; giving one switches to it)${RST}
|
||||||
$WITH_PROBLEM
|
$WITH_PROBLEM
|
||||||
${DIM}other${RST}
|
${DIM}other${RST}
|
||||||
$NO_PROBLEM, new <name>, new <leetcode-url|daily>
|
$NO_PROBLEM, new <name> [io] [--rust|--cpp], new <leetcode-url|daily>
|
||||||
|
lint [<name>] clippy the selection, or every rust solution
|
||||||
help [<cmd>] this, or the dispatcher's usage line for <cmd>
|
help [<cmd>] this, or the dispatcher's usage line for <cmd>
|
||||||
!cmd run a shell command in the problem dir
|
!cmd run a shell command in the problem dir
|
||||||
q quit
|
q quit
|
||||||
@@ -83,7 +84,7 @@ _usaco_comp() { # completes REPL commands and problem names
|
|||||||
for d in "$ROOT"/solving/*/ "$ROOT"/solved/*/ "$ROOT"/skipped/*/; do names+=("$(basename "$d")"); done
|
for d in "$ROOT"/solving/*/ "$ROOT"/solved/*/ "$ROOT"/skipped/*/; do names+=("$(basename "$d")"); done
|
||||||
shopt -u nullglob
|
shopt -u nullglob
|
||||||
if [[ $COMP_CWORD -eq 0 ]]; then
|
if [[ $COMP_CWORD -eq 0 ]]; then
|
||||||
COMPREPLY=($(compgen -W "$WITH_PROBLEM $NO_PROBLEM $REPL_WORDS new ${names[*]}" -- "$w"))
|
COMPREPLY=($(compgen -W "$WITH_PROBLEM $NO_PROBLEM $REPL_WORDS new lint ${names[*]}" -- "$w"))
|
||||||
else
|
else
|
||||||
COMPREPLY=($(compgen -W "${names[*]}" -- "$w"))
|
COMPREPLY=($(compgen -W "${names[*]}" -- "$w"))
|
||||||
fi
|
fi
|
||||||
@@ -141,6 +142,12 @@ while :; do
|
|||||||
if USACO_CREATED="$created" "$ROOT/usaco" new "${args[@]}"; then set_current "$(<"$created")"; fi
|
if USACO_CREATED="$created" "$ROOT/usaco" new "${args[@]}"; then set_current "$(<"$created")"; fi
|
||||||
rm -f "$created"
|
rm -f "$created"
|
||||||
continue ;;
|
continue ;;
|
||||||
|
lint)
|
||||||
|
# the one command whose problem name is optional: it lints the
|
||||||
|
# selection if there is one, otherwise every rust solution
|
||||||
|
[[ ${#args[@]} -eq 0 && -n "$cur" ]] && args=("$cur")
|
||||||
|
"$ROOT/usaco" lint "${args[@]+"${args[@]}"}" || true
|
||||||
|
continue ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# expand short aliases to full names so the tables below match
|
# expand short aliases to full names so the tables below match
|
||||||
@@ -158,7 +165,9 @@ while :; do
|
|||||||
if [[ " $WITH_PROBLEM " == *" $c "* ]]; then
|
if [[ " $WITH_PROBLEM " == *" $c "* ]]; then
|
||||||
# 'test create' keeps its subword first; the name goes after it
|
# 'test create' keeps its subword first; the name goes after it
|
||||||
pre=(); if [[ "$c" == test && "${args[0]:-}" =~ ^(create|add|new)$ ]]; then pre=("${args[0]}"); args=("${args[@]:1}"); fi
|
pre=(); if [[ "$c" == test && "${args[0]:-}" =~ ^(create|add|new)$ ]]; then pre=("${args[0]}"); args=("${args[@]:1}"); fi
|
||||||
if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* ]]; then # explicit name: use it and switch to it
|
# 'reset rust' switches the selection's language: a language word isn't a name
|
||||||
|
lang_arg=""; [[ "$c" == reset ]] && normalize_lang "${args[0]:-}" >/dev/null 2>&1 && lang_arg=1
|
||||||
|
if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* && -z "$lang_arg" ]]; then # explicit name: use it and switch to it
|
||||||
n="$(real_name "${args[0]}")" || { find_problem "${args[0]}" >/dev/null || true; continue; }
|
n="$(real_name "${args[0]}")" || { find_problem "${args[0]}" >/dev/null || true; continue; }
|
||||||
set_current "$n"; args=("${args[@]:1}")
|
set_current "$n"; args=("${args[@]:1}")
|
||||||
elif [[ -z "$cur" ]]; then
|
elif [[ -z "$cur" ]]; then
|
||||||
|
|||||||
+4
-12
@@ -3,9 +3,10 @@
|
|||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"
|
||||||
old="$(detect_io "$dir" || true)"
|
old="$(detect_io "$dir" || true)"
|
||||||
if [[ $# -lt 2 ]]; then
|
if [[ $# -lt 2 ]]; then
|
||||||
stdio="$(grep -o 'STDIO("[^"]*")' "$dir/main.cpp" 2>/dev/null | sed 's/STDIO("\(.*\)")/\1/' | head -1 || true)"
|
wired="$(show_io "$dir" || true)"
|
||||||
|
echo "lang: $(lang_of "$dir")"
|
||||||
echo "files: ${old:-none}"
|
echo "files: ${old:-none}"
|
||||||
echo "STDIO: ${stdio:-none}"
|
echo "source: ${wired:-none}"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
io="$2"
|
io="$2"
|
||||||
@@ -16,14 +17,5 @@ if [[ -n "$old" && "$old" != "$io" ]]; then
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
[[ -e "$dir/$io.in" ]] || { touch "$dir/$io.in"; echo "Created $io.in"; }
|
[[ -e "$dir/$io.in" ]] || { touch "$dir/$io.in"; echo "Created $io.in"; }
|
||||||
# rewire STDIO in main.cpp
|
wire_io "$dir" "$io"
|
||||||
if grep -q 'STDIO("' "$dir/main.cpp" 2>/dev/null; then
|
|
||||||
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$dir/main.cpp"
|
|
||||||
echo "Updated STDIO(\"$io\") in main.cpp"
|
|
||||||
elif grep -q '^int main() {$' "$dir/main.cpp" 2>/dev/null; then
|
|
||||||
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$dir/main.cpp"
|
|
||||||
echo "Inserted STDIO(\"$io\") in main.cpp"
|
|
||||||
else
|
|
||||||
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
|
|
||||||
fi
|
|
||||||
echo "io name for $(basename "$dir") is now '$io'"
|
echo "io name for $(basename "$dir") is now '$io'"
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# usaco lang [cpp|rust]
|
||||||
|
if [[ $# -eq 0 ]]; then
|
||||||
|
echo "default for 'usaco new': $(default_lang)"
|
||||||
|
[[ -n "${USACO_LANG:-}" ]] && echo "${DIM}(from \$USACO_LANG)${RST}"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
lang="$(normalize_lang "$1")" || exit 1
|
||||||
|
printf '%s\n' "$lang" > "$ROOT/.lang"
|
||||||
|
echo "Default language for new problems: $lang"
|
||||||
|
[[ -n "${USACO_LANG:-}" ]] && echo "${YEL}note:${RST} \$USACO_LANG=$USACO_LANG overrides this in your shell" >&2
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
# usaco lint [name...]
|
||||||
|
# Regenerates the cargo manifest rust-analyzer reads, then runs clippy over the
|
||||||
|
# rust solutions. With no arguments it lints every one, plus template.rs; names
|
||||||
|
# select individual problems (unique prefixes work, as everywhere else), and
|
||||||
|
# the literal name "template" selects template.rs.
|
||||||
|
command -v cargo >/dev/null || { echo "Error: cargo not found" >&2; exit 1; }
|
||||||
|
|
||||||
|
count="$(sync_cargo_manifest)"
|
||||||
|
if [[ -z "$count" ]]; then
|
||||||
|
echo "No rust solutions or template.rs yet — nothing to lint."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
targets=()
|
||||||
|
for name in "$@"; do
|
||||||
|
if [[ "$name" == template ]]; then
|
||||||
|
[[ -f "$ROOT/template.rs" ]] || { echo "Error: template.rs not found" >&2; exit 1; }
|
||||||
|
targets+=(--bin template)
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
dir="$(find_problem "$name")" || exit 1
|
||||||
|
[[ -f "$dir/main.rs" ]] || { echo "Error: $(basename "$dir") is not a rust problem" >&2; exit 1; }
|
||||||
|
rel="${dir#"$ROOT"/}"
|
||||||
|
targets+=(--bin "${rel%/*}-${rel##*/}")
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ ${#targets[@]} -eq 0 ]]; then
|
||||||
|
what="$count rust solution(s)"
|
||||||
|
[[ -f "$ROOT/template.rs" ]] && what+=" and template.rs"
|
||||||
|
echo "${DIM}Linting $what...${RST}"
|
||||||
|
targets=(--bins)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Builds live under target/, away from the ./main binaries usaco produces.
|
||||||
|
(cd "$ROOT" && cargo clippy --quiet "${targets[@]}")
|
||||||
+13
-5
@@ -1,9 +1,12 @@
|
|||||||
# usaco new <name> [io-name] [--url <link>] | usaco new <leetcode-url|daily>
|
# usaco new <name> [io-name] [--url <link>] [--rust|--cpp] | usaco new <leetcode-url|daily>
|
||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
name=""; io=""; url=""
|
name=""; io=""; url=""; lang=""
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--url) url="${2:-}"; shift 2 ;;
|
--url) url="${2:-}"; shift 2 ;;
|
||||||
|
--lang|-l) lang="$(normalize_lang "${2:-}")" || exit 1; shift 2 ;;
|
||||||
|
--rust|--rs) lang=rust; shift ;;
|
||||||
|
--cpp|--c++) lang=cpp; shift ;;
|
||||||
*) if [[ -z "$name" ]]; then name="$1"; elif [[ -z "$io" ]]; then io="$1"; fi; shift ;;
|
*) if [[ -z "$name" ]]; then name="$1"; elif [[ -z "$io" ]]; then io="$1"; fi; shift ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
@@ -11,7 +14,9 @@ done
|
|||||||
|
|
||||||
# A LeetCode link (or "daily": today's daily question) imports the problem:
|
# A LeetCode link (or "daily": today's daily question) imports the problem:
|
||||||
# statement, starter code, a generated driver, and the examples as tests.
|
# statement, starter code, a generated driver, and the examples as tests.
|
||||||
|
# Always C++, whatever 'usaco lang' says: the driver is C++.
|
||||||
if [[ "$name" == daily || "$name" =~ ^(https?://)?(www\.)?leetcode\.com/problems/ ]]; then
|
if [[ "$name" == daily || "$name" =~ ^(https?://)?(www\.)?leetcode\.com/problems/ ]]; then
|
||||||
|
[[ "${lang:-cpp}" == cpp ]] || { echo "Error: LeetCode imports are C++ only" >&2; exit 1; }
|
||||||
name="$(python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$name")" || exit 1
|
name="$(python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$name")" || exit 1
|
||||||
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
|
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
|
||||||
echo "Created solving/$name (statement in problem.md; 'usaco t $name' runs the examples)"
|
echo "Created solving/$name (statement in problem.md; 'usaco t $name' runs the examples)"
|
||||||
@@ -20,14 +25,17 @@ if [[ "$name" == daily || "$name" =~ ^(https?://)?(www\.)?leetcode\.com/problems
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
io="${io:-$name}"
|
io="${io:-$name}"
|
||||||
|
lang="${lang:-$(default_lang)}"
|
||||||
dir="$ROOT/solving/$name"
|
dir="$ROOT/solving/$name"
|
||||||
if [[ -d "$dir" || -d "$ROOT/solved/$name" || -d "$ROOT/skipped/$name" ]]; then
|
if [[ -d "$dir" || -d "$ROOT/solved/$name" || -d "$ROOT/skipped/$name" ]]; then
|
||||||
echo "Error: '$name' already exists" >&2; exit 1
|
echo "Error: '$name' already exists" >&2; exit 1
|
||||||
fi
|
fi
|
||||||
mkdir -p "$dir"
|
mkdir -p "$dir"
|
||||||
fresh_main "$dir" "$io"
|
fresh_main "$dir" "$io" "$lang"
|
||||||
touch "$dir/$io.in"
|
touch "$dir/$io.in"
|
||||||
[[ -n "$url" ]] && echo "$url" > "$dir/.url"
|
[[ -n "$url" ]] && echo "$url" > "$dir/.url"
|
||||||
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
|
[[ -n "${USACO_CREATED:-}" ]] && printf '%s\n' "$name" > "$USACO_CREATED"
|
||||||
echo "Created solving/$name (io: $io.in / $io.out)"
|
src="$(src_name "$lang")"
|
||||||
(cd "$dir" && exec ${EDITOR:-nvim} main.cpp)
|
echo "Created solving/$name/$src (io: $io.in / $io.out)"
|
||||||
|
[[ "$lang" == rust ]] && sync_cargo_manifest >/dev/null
|
||||||
|
(cd "$dir" && exec ${EDITOR:-nvim} "$src")
|
||||||
|
|||||||
+25
-11
@@ -1,25 +1,39 @@
|
|||||||
# usaco reset/rm/delete/template/tpl <name> [-y]
|
# usaco reset/rm/delete/template/tpl <name> [cpp|rust] [-y]
|
||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"; shift
|
||||||
name="$(basename "$dir")"
|
name="$(basename "$dir")"
|
||||||
|
yes=""; lang=""
|
||||||
|
for a in "$@"; do
|
||||||
|
if [[ "$a" == -y ]]; then yes=1; else lang="$(normalize_lang "$a")" || exit 1; fi
|
||||||
|
done
|
||||||
|
lang="${lang:-$(lang_of "$dir" 2>/dev/null || default_lang)}"
|
||||||
|
leet=""; [[ -f "$dir/driver.h" ]] && leet=1
|
||||||
|
if [[ -n "$leet" && "$lang" != cpp ]]; then
|
||||||
|
echo "Error: $name is a LeetCode import, and those are C++ only" >&2; exit 1
|
||||||
|
fi
|
||||||
io="$(detect_io "$dir" || true)"
|
io="$(detect_io "$dir" || true)"
|
||||||
io="${io:-$name}"
|
io="${io:-$name}"
|
||||||
if [[ "${2:-}" != -y ]]; then
|
old="$(main_file "$dir" 2>/dev/null || true)"
|
||||||
echo "This resets ${dir#$ROOT/} to a fresh template:"
|
src="$(src_name "$lang")"
|
||||||
if [[ -f "$dir/driver.h" ]]; then
|
if [[ -z "$yes" ]]; then
|
||||||
echo " main.cpp -> LeetCode's starter code again (old one saved as main.cpp.bak)"
|
echo "This resets ${dir#$ROOT/} to a fresh $lang template:"
|
||||||
|
if [[ -n "$leet" ]]; then
|
||||||
|
echo " $src -> LeetCode's starter code again (old one saved as $src.bak)"
|
||||||
else
|
else
|
||||||
echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)"
|
echo " $src -> re-created from template (old one saved as $(basename "${old:-$src}").bak)"
|
||||||
fi
|
fi
|
||||||
echo " removes compiled binary and $io.out (tests/ and $io.in are kept)"
|
echo " removes compiled binary and $io.out (tests/ and $io.in are kept)"
|
||||||
read -r -p "Reset '$name'? [y/N] " ans
|
read -r -p "Reset '$name'? [y/N] " ans
|
||||||
[[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; }
|
[[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; }
|
||||||
fi
|
fi
|
||||||
[[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak"
|
[[ -n "$old" && -f "$old" ]] && cp "$old" "$old.bak"
|
||||||
if [[ -f "$dir/driver.h" ]]; then # a LeetCode import: re-fetch its starter code
|
# switching language: drop the old source so lang_of stays unambiguous
|
||||||
|
[[ -n "$old" && "$(basename "$old")" != "$src" ]] && rm -f "$old"
|
||||||
|
if [[ -n "$leet" ]]; then # a LeetCode import: re-fetch its starter code
|
||||||
python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$dir" --main-only >/dev/null || exit 1
|
python3 -I "$ROOT/lib/leetcode.py" "$ROOT" "$dir" --main-only >/dev/null || exit 1
|
||||||
else
|
else
|
||||||
fresh_main "$dir" "$io"
|
fresh_main "$dir" "$io" "$lang"
|
||||||
fi
|
fi
|
||||||
|
[[ "$lang" == rust || "$old" == *.rs ]] && sync_cargo_manifest >/dev/null
|
||||||
rm -f "$dir/main" "$dir/a.out" "$dir/$io.out"
|
rm -f "$dir/main" "$dir/a.out" "$dir/$io.out"
|
||||||
echo "Reset $name (old solution in main.cpp.bak)"
|
echo "Reset $name (old solution in $(basename "${old:-$src}").bak)"
|
||||||
|
|||||||
+5
-4
@@ -2,11 +2,12 @@
|
|||||||
[[ $# -ge 1 ]] || usage
|
[[ $# -ge 1 ]] || usage
|
||||||
dir="$(find_problem "$1")"
|
dir="$(find_problem "$1")"
|
||||||
build_problem "$dir"
|
build_problem "$dir"
|
||||||
# run from inside the dir so freopen finds the .in file
|
|
||||||
(cd "$dir" && ./main)
|
|
||||||
# show the .out file if the program wrote one
|
|
||||||
io="$(detect_io "$dir" || true)"
|
io="$(detect_io "$dir" || true)"
|
||||||
if [[ -n "$io" && -f "$dir/$io.out" ]]; then
|
io="${io:-$(basename "$dir")}"
|
||||||
|
rc=0; run_solution "$dir" "$io" || rc=$?
|
||||||
|
if [[ -e "$dir/$io.out" ]]; then
|
||||||
echo "── $io.out ──"
|
echo "── $io.out ──"
|
||||||
cat "$dir/$io.out"
|
cat "$dir/$io.out"
|
||||||
fi
|
fi
|
||||||
|
[[ $rc -eq 0 ]] || echo "${RED}exited $rc${RST}" >&2
|
||||||
|
exit $rc
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ dir="$(find_problem "$1")"; name="$(basename "$dir")"
|
|||||||
[[ -d "$ROOT/skipped/$name" ]] && { echo "Error: skipped/$name already exists" >&2; exit 1; }
|
[[ -d "$ROOT/skipped/$name" ]] && { echo "Error: skipped/$name already exists" >&2; exit 1; }
|
||||||
mkdir -p "$ROOT/skipped"
|
mkdir -p "$ROOT/skipped"
|
||||||
mv "$ROOT/solving/$name" "$ROOT/skipped/$name"
|
mv "$ROOT/solving/$name" "$ROOT/skipped/$name"
|
||||||
|
sync_cargo_manifest >/dev/null # the bin paths just moved
|
||||||
echo "Moved to skipped/$name (usaco undo $name to bring it back)"
|
echo "Moved to skipped/$name (usaco undo $name to bring it back)"
|
||||||
|
|||||||
+1
-2
@@ -28,9 +28,8 @@ run_case() { # $1 = dir, $2 = io, $3 = tests/N.in path; returns 0 pass, 1 fail,
|
|||||||
local dir="$1" io="$2" tin="$3" n ms start exp
|
local dir="$1" io="$2" tin="$3" n ms start exp
|
||||||
n="$(basename "$tin" .in)"
|
n="$(basename "$tin" .in)"
|
||||||
cp "$tin" "$dir/$io.in"
|
cp "$tin" "$dir/$io.in"
|
||||||
rm -f "$dir/$io.out"
|
|
||||||
start=$(date +%s%N)
|
start=$(date +%s%N)
|
||||||
if ! (cd "$dir" && ./main); then
|
if ! run_solution "$dir" "$io"; then
|
||||||
echo "${RED}✗ case $n CRASHED (nonzero exit)${RST}"
|
echo "${RED}✗ case $n CRASHED (nonzero exit)${RST}"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ from="$(basename "$(dirname "$dir")")"
|
|||||||
[[ "$from" != solving ]] || { echo "Error: $name is already in solving/" >&2; exit 1; }
|
[[ "$from" != solving ]] || { echo "Error: $name is already in solving/" >&2; exit 1; }
|
||||||
[[ -d "$ROOT/solving/$name" ]] && { echo "Error: solving/$name already exists" >&2; exit 1; }
|
[[ -d "$ROOT/solving/$name" ]] && { echo "Error: solving/$name already exists" >&2; exit 1; }
|
||||||
mv "$ROOT/$from/$name" "$ROOT/solving/$name"
|
mv "$ROOT/$from/$name" "$ROOT/solving/$name"
|
||||||
|
sync_cargo_manifest >/dev/null # the bin paths just moved
|
||||||
echo "Moved back to solving/$name (was $from)"
|
echo "Moved back to solving/$name (was $from)"
|
||||||
|
|||||||
+183
-8
@@ -5,6 +5,40 @@
|
|||||||
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra -DLOCAL -I"$ROOT")
|
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra -DLOCAL -I"$ROOT")
|
||||||
# --debug (or USACO_DEBUG=1): catch out-of-bounds, overflow and UB at runtime
|
# --debug (or USACO_DEBUG=1): catch out-of-bounds, overflow and UB at runtime
|
||||||
DEBUG_CXXFLAGS=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -D_GLIBCXX_DEBUG)
|
DEBUG_CXXFLAGS=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -D_GLIBCXX_DEBUG)
|
||||||
|
RUSTCFLAGS=(-O --edition 2021)
|
||||||
|
# --debug for rust: debug info, plus the overflow and debug_assert! checks -O drops
|
||||||
|
DEBUG_RUSTCFLAGS=(-g --edition 2021 -C debug-assertions -C overflow-checks)
|
||||||
|
|
||||||
|
# ----- languages -----
|
||||||
|
# A problem's language is just whichever source file it has: main.cpp or main.rs.
|
||||||
|
LANGS="cpp rust"
|
||||||
|
|
||||||
|
default_lang() { # language 'usaco new' uses when no flag is given
|
||||||
|
local l="${USACO_LANG:-}"
|
||||||
|
[[ -z "$l" && -r "$ROOT/.lang" ]] && l="$(<"$ROOT/.lang")"
|
||||||
|
normalize_lang "${l:-cpp}" 2>/dev/null || echo cpp
|
||||||
|
}
|
||||||
|
|
||||||
|
normalize_lang() { # accepts cpp/c++/cc/rust/rs, echoes cpp|rust
|
||||||
|
case "${1,,}" in
|
||||||
|
cpp|c++|cc|cxx) echo cpp ;;
|
||||||
|
rust|rs) echo rust ;;
|
||||||
|
*) echo "Error: unknown language '$1' (use cpp or rust)" >&2; return 1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
src_name() { [[ "$1" == rust ]] && echo main.rs || echo main.cpp; }
|
||||||
|
|
||||||
|
lang_of() { # $1 = problem dir; echoes cpp|rust
|
||||||
|
if [[ -f "$1/main.rs" ]]; then echo rust
|
||||||
|
elif [[ -f "$1/main.cpp" ]]; then echo cpp
|
||||||
|
else echo "Error: no main.cpp or main.rs in $1" >&2; return 1; fi
|
||||||
|
}
|
||||||
|
|
||||||
|
main_file() { # $1 = problem dir; echoes the path of its source file
|
||||||
|
local l; l="$(lang_of "$1")" || return 1
|
||||||
|
echo "$1/$(src_name "$l")"
|
||||||
|
}
|
||||||
|
|
||||||
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
|
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
|
||||||
local name="$1" s d matches=()
|
local name="$1" s d matches=()
|
||||||
@@ -26,13 +60,102 @@ find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; uni
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
build_problem() { # $1 = problem dir
|
# ----- rust project model (for rust-analyzer / clippy) -----
|
||||||
if [[ -n "${USACO_DEBUG:-}" ]]; then
|
# Problems are bare main.rs files with no manifest, and rust-analyzer only does
|
||||||
echo "${DIM}(debug build: sanitizers on)${RST}" >&2
|
# real analysis -- type-aware diagnostics, clippy on save -- inside a cargo
|
||||||
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
|
# project. So we generate one package at the root that declares every rust
|
||||||
else
|
# solution (and template.rs) as its own [[bin]] target. Nothing builds through cargo: 'usaco
|
||||||
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
|
# build/run' still shells out to rustc. The manifest exists purely so the editor
|
||||||
|
# has something to chew on, which is why it's gitignored and regenerated from a
|
||||||
|
# directory scan (that also prunes problems that moved or went away).
|
||||||
|
#
|
||||||
|
# To change which lints fire, edit the [lints.clippy] table below.
|
||||||
|
sync_cargo_manifest() {
|
||||||
|
local s d name entry count=0 bins=""
|
||||||
|
for s in solving solved skipped; do
|
||||||
|
[[ -d "$ROOT/$s" ]] || continue
|
||||||
|
for d in "$ROOT/$s"/*/; do
|
||||||
|
[[ -f "${d}main.rs" ]] || continue
|
||||||
|
# Target names can't start with a digit, and problems like '101'
|
||||||
|
# do; the directory prefix keeps them legal and unique.
|
||||||
|
name="$s-$(basename "$d")"
|
||||||
|
printf -v entry '[[bin]]\nname = "%s"\npath = "%smain.rs"\n\n' "$name" "${d#"$ROOT"/}"
|
||||||
|
bins+="$entry"
|
||||||
|
count=$((count + 1))
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
# template.rs is a target too, so mistakes in it surface in the editor and
|
||||||
|
# in 'usaco lint template' instead of in every problem stamped out from it.
|
||||||
|
# Its name has no directory prefix, so it can't collide with a problem.
|
||||||
|
if [[ -f "$ROOT/template.rs" ]]; then
|
||||||
|
bins+=$'[[bin]]\nname = "template"\npath = "template.rs"\n\n'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ -z "$bins" ]]; then
|
||||||
|
rm -f "$ROOT/Cargo.toml"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat > "$ROOT/Cargo.toml" <<EOF
|
||||||
|
# GENERATED by 'usaco lint' -- edits here are lost. See sync_cargo_manifest()
|
||||||
|
# in lib/common.sh. This file only exists so rust-analyzer can analyse the
|
||||||
|
# solutions and run clippy on them; builds go through rustc, not cargo.
|
||||||
|
|
||||||
|
[package]
|
||||||
|
name = "usaco"
|
||||||
|
version = "0.0.0"
|
||||||
|
edition = "2021"
|
||||||
|
autobins = false
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
pedantic = { level = "warn", priority = -1 }
|
||||||
|
|
||||||
|
# Casts between usize/i32/i64 are the bread and butter of competitive
|
||||||
|
# programming. Warning on each one buries the lints that find actual bugs.
|
||||||
|
cast_possible_truncation = "allow"
|
||||||
|
cast_possible_wrap = "allow"
|
||||||
|
cast_sign_loss = "allow"
|
||||||
|
cast_precision_loss = "allow"
|
||||||
|
|
||||||
|
# Library-API hygiene, meaningless for a single-file solution.
|
||||||
|
missing_panics_doc = "allow"
|
||||||
|
missing_errors_doc = "allow"
|
||||||
|
must_use_candidate = "allow"
|
||||||
|
doc_markdown = "allow"
|
||||||
|
|
||||||
|
# '|x| x.to_string()' reads better than the spelled-out method path.
|
||||||
|
redundant_closure_for_method_calls = "allow"
|
||||||
|
|
||||||
|
${bins%$'\n'}
|
||||||
|
EOF
|
||||||
|
printf '%s\n' "$count"
|
||||||
|
}
|
||||||
|
|
||||||
|
build_problem() { # $1 = problem dir; compiles to $1/main whatever the language
|
||||||
|
local dir="$1" lang
|
||||||
|
lang="$(lang_of "$dir")" || return 1
|
||||||
|
if [[ -n "${USACO_DEBUG:-}" ]]; then
|
||||||
|
echo "${DIM}(debug build: $([[ $lang == cpp ]] && echo sanitizers || echo overflow checks) on)${RST}" >&2
|
||||||
|
fi
|
||||||
|
case "$lang" in
|
||||||
|
cpp)
|
||||||
|
if [[ -n "${USACO_DEBUG:-}" ]]; then
|
||||||
|
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$dir/main.cpp" -o "$dir/main"
|
||||||
|
else
|
||||||
|
g++ "${CXXFLAGS[@]}" "$dir/main.cpp" -o "$dir/main"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
rust)
|
||||||
|
# Keep the editor's project model in step with what's on disk.
|
||||||
|
sync_cargo_manifest >/dev/null
|
||||||
|
if [[ -n "${USACO_DEBUG:-}" ]]; then
|
||||||
|
rustc "${DEBUG_RUSTCFLAGS[@]}" "$dir/main.rs" -o "$dir/main"
|
||||||
|
else
|
||||||
|
rustc "${RUSTCFLAGS[@]}" "$dir/main.rs" -o "$dir/main"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
detect_io() { # echoes the io basename (first *.in in $1), or nothing
|
detect_io() { # echoes the io basename (first *.in in $1), or nothing
|
||||||
@@ -60,8 +183,60 @@ solved_at() { # $1 = solved/<name> dir: epoch it (last) entered solved/
|
|||||||
[[ -n "$t" ]] && echo "$t" || last_touched "$1"
|
[[ -n "$t" ]] && echo "$t" || last_touched "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
fresh_main() { # $1 = problem dir, $2 = io name: main.cpp from template with STDIO wired up
|
fresh_main() { # $1 = problem dir, $2 = io name, [$3 = language]: source from template, io wired up
|
||||||
sed "s/^int main() {$/int main() {\n STDIO(\"$2\");/" "$ROOT/template.cpp" > "$1/main.cpp"
|
local dir="$1" io="$2" lang="${3:-}"
|
||||||
|
[[ -n "$lang" ]] || lang="$(lang_of "$dir")" || return 1
|
||||||
|
case "$lang" in
|
||||||
|
cpp) sed "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$ROOT/template.cpp" > "$dir/main.cpp" ;;
|
||||||
|
rust) sed "s/stdio(\"[^\"]*\")/stdio(\"$io\")/" "$ROOT/template.rs" > "$dir/main.rs" ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
wire_io() { # $1 = problem dir, $2 = io name: point the source's file-io call at <io>
|
||||||
|
local dir="$1" io="$2" lang src
|
||||||
|
lang="$(lang_of "$dir")" || return 1
|
||||||
|
src="$dir/$(src_name "$lang")"
|
||||||
|
if [[ "$lang" == rust ]]; then
|
||||||
|
if grep -q 'stdio("' "$src"; then
|
||||||
|
sed -i "s/stdio(\"[^\"]*\")/stdio(\"$io\")/" "$src"
|
||||||
|
echo "Updated stdio(\"$io\") in main.rs"
|
||||||
|
else
|
||||||
|
echo "Warning: couldn't find stdio(\"...\") in main.rs — wire it up manually" >&2
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if grep -q 'STDIO("' "$src"; then
|
||||||
|
sed -i "s/STDIO(\"[^\"]*\")/STDIO(\"$io\")/" "$src"
|
||||||
|
echo "Updated STDIO(\"$io\") in main.cpp"
|
||||||
|
elif grep -q '^int main() {$' "$src"; then
|
||||||
|
sed -i "s/^int main() {$/int main() {\n STDIO(\"$io\");/" "$src"
|
||||||
|
echo "Inserted STDIO(\"$io\") in main.cpp"
|
||||||
|
else
|
||||||
|
echo "Warning: couldn't find STDIO(...) or 'int main() {' in main.cpp — wire it up manually" >&2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
show_io() { # $1 = problem dir: echo the io name the source is wired to, if any
|
||||||
|
local lang src; lang="$(lang_of "$1")" || return 1
|
||||||
|
src="$1/$(src_name "$lang")"
|
||||||
|
if [[ "$lang" == rust ]]; then
|
||||||
|
grep -o 'stdio("[^"]*")' "$src" | sed 's/stdio("\(.*\)")/\1/' | head -1
|
||||||
|
else
|
||||||
|
grep -o 'STDIO("[^"]*")' "$src" | sed 's/STDIO("\(.*\)")/\1/' | head -1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
run_solution() { # $1 = dir, $2 = io name: run ./main, leave its output in $1/$2.out
|
||||||
|
# Works for both styles: a solution that opens <io>.in/.out itself (STDIO / stdio)
|
||||||
|
# and one that just reads stdin and writes stdout (Codeforces-style).
|
||||||
|
local dir="$1" io="$2" tmp rc=0
|
||||||
|
rm -f "$dir/$io.out"
|
||||||
|
tmp="$(mktemp)"
|
||||||
|
[[ -e "$dir/$io.in" ]] || touch "$dir/$io.in"
|
||||||
|
(cd "$dir" && ./main) < "$dir/$io.in" > "$tmp" || rc=$?
|
||||||
|
[[ -e "$dir/$io.out" ]] || cp "$tmp" "$dir/$io.out"
|
||||||
|
rm -f "$tmp"
|
||||||
|
return $rc
|
||||||
}
|
}
|
||||||
|
|
||||||
# colors (disabled when not a tty or NO_COLOR is set)
|
# colors (disabled when not a tty or NO_COLOR is set)
|
||||||
|
|||||||
@@ -192,10 +192,11 @@ func readProblem(pdir, status string) (*Problem, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// main.cpp first: it is the CLI's solution, anything else is a side
|
// main.cpp / main.rs first: it is the CLI's solution, anything else is
|
||||||
// experiment (sol.py) or a helper.
|
// a side experiment (sol.py) or a helper.
|
||||||
|
isMain := func(name string) bool { return strings.HasPrefix(name, "main.") }
|
||||||
sort.SliceStable(p.Files, func(i, j int) bool {
|
sort.SliceStable(p.Files, func(i, j int) bool {
|
||||||
return p.Files[i].Name == "main.cpp" && p.Files[j].Name != "main.cpp"
|
return isMain(p.Files[i].Name) && !isMain(p.Files[j].Name)
|
||||||
})
|
})
|
||||||
if p.Sample != nil && strings.TrimSpace(p.Sample.Content) == "" {
|
if p.Sample != nil && strings.TrimSpace(p.Sample.Content) == "" {
|
||||||
p.Sample = nil
|
p.Sample = nil
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ const sections = [
|
|||||||
{
|
{
|
||||||
title: "Start a problem",
|
title: "Start a problem",
|
||||||
rows: [
|
rows: [
|
||||||
["usaco new <name>", "Create solving/<name>/ with main.cpp from the template and an empty .in file, then open it in $EDITOR."],
|
["usaco new <name>", "Create solving/<name>/ with main.cpp (or main.rs) from the template and an empty .in file, then open it in $EDITOR."],
|
||||||
|
["usaco new <name> --rust", "Pick the language for this problem (--cpp / --rust); otherwise the default from usaco lang."],
|
||||||
|
["usaco lang [cpp|rust]", "Show or set the default language for new problems (stored in .lang; $USACO_LANG overrides it per shell)."],
|
||||||
["usaco new <name> <io>", "Same, when the USACO file name differs from the problem name."],
|
["usaco new <name> <io>", "Same, when the USACO file name differs from the problem name."],
|
||||||
["usaco new <name> --url <link>", "Also save the problem link, for usaco open."],
|
["usaco new <name> --url <link>", "Also save the problem link, for usaco open."],
|
||||||
["usaco new <leetcode-link>", "Import a LeetCode problem (also usaco from): the statement as problem.md, LeetCode's C++ starter code in main.cpp, a generated driver.h that runs it on LeetCode-format input, and the examples as tests with their expected outputs."],
|
["usaco new <leetcode-link>", "Import a LeetCode problem (also usaco from): the statement as problem.md, LeetCode's C++ starter code in main.cpp, a generated driver.h that runs it on LeetCode-format input, and the examples as tests with their expected outputs."],
|
||||||
@@ -26,11 +28,11 @@ const sections = [
|
|||||||
{
|
{
|
||||||
title: "Build, run and test",
|
title: "Build, run and test",
|
||||||
rows: [
|
rows: [
|
||||||
["usaco run <name>", "Compile and run inside the problem dir (so file IO just works), then print the .out file."],
|
["usaco run <name>", "Compile and run inside the problem dir, feeding it the .in on stdin (so file-IO and stdin/stdout solutions both work), then print the .out file."],
|
||||||
["usaco build <name>", "Compile only: g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL."],
|
["usaco build <name>", "Compile only: g++ -O2 -std=c++17 -Wall -Wextra -DLOCAL, or rustc -O --edition 2021 for main.rs."],
|
||||||
["usaco test create <name>", "Save a test case: tests/N.in (the current .in, or pasted) and the expected N.out."],
|
["usaco test create <name>", "Save a test case: tests/N.in (the current .in, or pasted) and the expected N.out."],
|
||||||
["usaco test <name> [N…]", "Build, run every saved case (or just the ones given), diff, and show pass/fail with timings."],
|
["usaco test <name> [N…]", "Build, run every saved case (or just the ones given), diff, and show pass/fail with timings."],
|
||||||
["--debug", "On run, build or test (or USACO_DEBUG=1): an ASan/UBSan + _GLIBCXX_DEBUG build, so out-of-bounds and UB crash with a stack trace."],
|
["--debug", "On run, build or test (or USACO_DEBUG=1): an ASan/UBSan + _GLIBCXX_DEBUG build, so out-of-bounds and UB crash with a stack trace. Rust gets overflow checks and debug assertions."],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -45,20 +47,21 @@ const sections = [
|
|||||||
{
|
{
|
||||||
title: "Edit and look around",
|
title: "Edit and look around",
|
||||||
rows: [
|
rows: [
|
||||||
["usaco e <name>", "Open main.cpp in $EDITOR from inside the problem dir (also vim, edit)."],
|
["usaco e <name>", "Open the solution in $EDITOR from inside the problem dir (also vim, edit)."],
|
||||||
["usaco list", "All problems by status, oldest-touched first."],
|
["usaco list", "All problems by status, oldest-touched first."],
|
||||||
["usaco stats", "Counts, plus problems solved in the last 7 and 30 days."],
|
["usaco stats", "Counts, plus problems solved in the last 7 and 30 days."],
|
||||||
["usaco open <name>", "Open the saved problem link in a browser."],
|
["usaco open <name>", "Open the saved problem link in a browser."],
|
||||||
["usaco cat <name>", "Print main.cpp to stdout."],
|
["usaco cat <name>", "Print the solution to stdout."],
|
||||||
["usaco copy <name>", "Copy main.cpp to the clipboard (wl-copy, xclip/xsel, or OSC52 over SSH)."],
|
["usaco copy <name>", "Copy the solution to the clipboard (wl-copy, xclip/xsel, or OSC52 over SSH)."],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Misc",
|
title: "Misc",
|
||||||
rows: [
|
rows: [
|
||||||
["usaco reset <name>", "Fresh main.cpp from the template (the old one goes to main.cpp.bak). Asks first; -y skips. Keeps tests/ and the .in."],
|
["usaco reset <name> [cpp|rust]", "Fresh source from the template (the old one goes to a .bak); giving a language switches the problem to it. Asks first; -y skips. Keeps tests/ and the .in."],
|
||||||
["usaco io <name> [new]", "Show the IO name, or rename the .in/.out and rewire STDIO."],
|
["usaco io <name> [new]", "Show the IO name, or rename the .in/.out and rewire STDIO / stdio."],
|
||||||
["usaco clean", "Delete compiled binaries and .bak files everywhere."],
|
["usaco clean", "Delete compiled binaries and .bak files everywhere."],
|
||||||
|
["usaco lint [name…]", "Run clippy over the Rust solutions (or the named ones; template lints template.rs), refreshing the Cargo.toml rust-analyzer reads."],
|
||||||
["usaco path <name>", "Print the problem's directory."],
|
["usaco path <name>", "Print the problem's directory."],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -89,7 +92,7 @@ usaco [watermelon:solved]> q`;
|
|||||||
Every problem here is written in a workspace managed by <a :href="src('usaco')"><code>./usaco</code></a>, a small bash script at
|
Every problem here is written in a workspace managed by <a :href="src('usaco')"><code>./usaco</code></a>, a small bash script at
|
||||||
the root of <a :href="repo">the repo on Gitea</a> (the commands themselves are in
|
the root of <a :href="repo">the repo on Gitea</a> (the commands themselves are in
|
||||||
<a :href="src('lib/cmd')"><code>lib/cmd/</code></a>). Each problem lives in <code>solving/</code>, <code>solved/</code> or
|
<a :href="src('lib/cmd')"><code>lib/cmd/</code></a>). Each problem lives in <code>solving/</code>, <code>solved/</code> or
|
||||||
<code>skipped/</code> as a directory with <code>main.cpp</code> and its <code>.in</code>/<code>.out</code>
|
<code>skipped/</code> as a directory with <code>main.cpp</code> or <code>main.rs</code> and its <code>.in</code>/<code>.out</code>
|
||||||
files. Wherever a command takes a problem name, a unique prefix works too:
|
files. Wherever a command takes a problem name, a unique prefix works too:
|
||||||
<code>usaco e diam</code> finds <code>diamond-collector</code>.
|
<code>usaco e diam</code> finds <code>diamond-collector</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
0
|
||||||
|
3
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
1
|
||||||
|
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
#![allow(dead_code, unused_imports, unused_macros, unused_mut, unused_variables)]
|
||||||
|
use std::cmp::{Ordering, Reverse, max, min};
|
||||||
|
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufWriter, Read, Write};
|
||||||
|
|
||||||
|
// ----- TYPES -----
|
||||||
|
type Ll = i64;
|
||||||
|
type Pii = (i32, i32);
|
||||||
|
|
||||||
|
// ----- FILE IO -----
|
||||||
|
// stdio("excel-sheet") picks the mode from the directory it runs in:
|
||||||
|
// name.in exists -> read name.in, write name.out (USACO + `usaco run/test`)
|
||||||
|
// name.in missing -> read stdin, write stdout (Codeforces submission)
|
||||||
|
// So the same file works locally and when pasted into a judge.
|
||||||
|
fn stdio(name: &str) -> (Scanner, Box<dyn Write>) {
|
||||||
|
let infile = format!("{}.in", name);
|
||||||
|
if std::path::Path::new(&infile).exists() {
|
||||||
|
let src = std::fs::read_to_string(&infile).expect("could not read .in file");
|
||||||
|
let out = File::create(format!("{}.out", name)).expect("could not create .out file");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(out)))
|
||||||
|
} else {
|
||||||
|
let mut src = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_to_string(&mut src)
|
||||||
|
.expect("could not read stdin");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(io::stdout())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- INPUT -----
|
||||||
|
// Slurps the whole input, then hands out whitespace-separated tokens.
|
||||||
|
// let n: usize = sc.next(); one token
|
||||||
|
// let (a, b) = (sc.next::<i64>(), sc.next::<i64>());
|
||||||
|
// let v: Vec<i64> = sc.vec(n); n tokens
|
||||||
|
// let s: Vec<u8> = sc.bytes(); a word as bytes (s[i] - b'a')
|
||||||
|
struct Scanner {
|
||||||
|
toks: std::str::SplitAsciiWhitespace<'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scanner {
|
||||||
|
fn new(src: &str) -> Scanner {
|
||||||
|
let leaked: &'static str = Box::leak(src.to_owned().into_boxed_str());
|
||||||
|
Scanner {
|
||||||
|
toks: leaked.split_ascii_whitespace(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn next<T: std::str::FromStr>(&mut self) -> T {
|
||||||
|
let tok = self.toks.next().expect("ran out of input");
|
||||||
|
tok.parse().ok().expect("could not parse token")
|
||||||
|
}
|
||||||
|
fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
|
||||||
|
(0..n).map(|_| self.next()).collect()
|
||||||
|
}
|
||||||
|
fn pair<T: std::str::FromStr, U: std::str::FromStr>(&mut self) -> (T, U) {
|
||||||
|
(self.next(), self.next())
|
||||||
|
}
|
||||||
|
fn pairs<T: std::str::FromStr, U: std::str::FromStr>(&mut self, n: usize) -> Vec<(T, U)> {
|
||||||
|
(0..n).map(|_| self.pair()).collect()
|
||||||
|
}
|
||||||
|
fn bytes(&mut self) -> Vec<u8> {
|
||||||
|
self.next::<String>().into_bytes()
|
||||||
|
}
|
||||||
|
fn chars(&mut self) -> Vec<char> {
|
||||||
|
self.next::<String>().chars().collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- OUTPUT -----
|
||||||
|
// Write with writeln!(out, "{}", x). These help with the common shapes:
|
||||||
|
fn yesno(b: bool) -> &'static str {
|
||||||
|
if b { "YES" } else { "NO" }
|
||||||
|
}
|
||||||
|
// join(&v, " ") -> "1 2 3"
|
||||||
|
fn join<T: std::fmt::Display>(v: &[T], sep: &str) -> String {
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(sep)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- DEBUG -----
|
||||||
|
// dbg!(x) is built in and goes to stderr, so it never pollutes the .out file.
|
||||||
|
|
||||||
|
// ----- MAX MIN -----
|
||||||
|
fn remax<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b > *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn remin<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b < *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let (mut sc, mut out) = stdio("excel-sheet");
|
||||||
|
let t: usize = sc.next();
|
||||||
|
for _ in 0..t {
|
||||||
|
let x: i32 = sc.next();
|
||||||
|
while (x > 0) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
#![allow(dead_code, unused_imports, unused_macros, unused_mut, unused_variables)]
|
||||||
|
use std::cmp::{max, min, Ordering, Reverse};
|
||||||
|
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufWriter, Read, Write};
|
||||||
|
|
||||||
|
// ----- TYPES -----
|
||||||
|
type Ll = i64;
|
||||||
|
type Pii = (i32, i32);
|
||||||
|
|
||||||
|
// ----- FILE IO -----
|
||||||
|
// stdio("name") picks the mode from the directory it runs in:
|
||||||
|
// name.in exists -> read name.in, write name.out (USACO + `usaco run/test`)
|
||||||
|
// name.in missing -> read stdin, write stdout (Codeforces submission)
|
||||||
|
// So the same file works locally and when pasted into a judge.
|
||||||
|
fn stdio(name: &str) -> (Scanner, Box<dyn Write>) {
|
||||||
|
let infile = format!("{name}.in");
|
||||||
|
if std::path::Path::new(&infile).exists() {
|
||||||
|
let src = std::fs::read_to_string(&infile).expect("could not read .in file");
|
||||||
|
let out = File::create(format!("{name}.out")).expect("could not create .out file");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(out)))
|
||||||
|
} else {
|
||||||
|
let mut src = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_to_string(&mut src)
|
||||||
|
.expect("could not read stdin");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(io::stdout())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- INPUT -----
|
||||||
|
// Slurps the whole input, then hands out whitespace-separated tokens.
|
||||||
|
// let n: usize = sc.next(); one token
|
||||||
|
// let (a, b) = (sc.next::<i64>(), sc.next::<i64>());
|
||||||
|
// let v: Vec<i64> = sc.vec(n); n tokens
|
||||||
|
// let s: Vec<u8> = sc.bytes(); a word as bytes (s[i] - b'a')
|
||||||
|
struct Scanner {
|
||||||
|
toks: std::str::SplitAsciiWhitespace<'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scanner {
|
||||||
|
fn new(src: &str) -> Scanner {
|
||||||
|
let leaked: &'static str = Box::leak(src.to_owned().into_boxed_str());
|
||||||
|
Scanner {
|
||||||
|
toks: leaked.split_ascii_whitespace(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn next<T: std::str::FromStr>(&mut self) -> T {
|
||||||
|
let tok = self.toks.next().expect("ran out of input");
|
||||||
|
tok.parse().ok().expect("could not parse token")
|
||||||
|
}
|
||||||
|
fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
|
||||||
|
(0..n).map(|_| self.next()).collect()
|
||||||
|
}
|
||||||
|
fn pair<T: std::str::FromStr, U: std::str::FromStr>(&mut self) -> (T, U) {
|
||||||
|
(self.next(), self.next())
|
||||||
|
}
|
||||||
|
fn pairs<T: std::str::FromStr, U: std::str::FromStr>(&mut self, n: usize) -> Vec<(T, U)> {
|
||||||
|
(0..n).map(|_| self.pair()).collect()
|
||||||
|
}
|
||||||
|
fn bytes(&mut self) -> Vec<u8> {
|
||||||
|
self.next::<String>().into_bytes()
|
||||||
|
}
|
||||||
|
fn chars(&mut self) -> Vec<char> {
|
||||||
|
self.next::<String>().chars().collect()
|
||||||
|
}
|
||||||
|
fn leet_vec<T: std::str::FromStr>(&mut self) -> Vec<T> {
|
||||||
|
let s: String = self.next();
|
||||||
|
s.trim_matches(|c| c == '[' || c == ']')
|
||||||
|
.split(',')
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.parse().ok().expect("leet vec parse err"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
fn leet_grid<T: std::str::FromStr>(&mut self) -> Vec<Vec<T>> {
|
||||||
|
let s: String = self.next();
|
||||||
|
let s = s.trim_start_matches('[').trim_end_matches(']');
|
||||||
|
let mut res = Vec::new();
|
||||||
|
for row in s.split("],[") {
|
||||||
|
let cur = row
|
||||||
|
.split(',')
|
||||||
|
.filter(|c| !c.is_empty())
|
||||||
|
.map(|c| c.parse().ok().expect("leet grid error"))
|
||||||
|
.collect();
|
||||||
|
res.push(cur);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- OUTPUT -----
|
||||||
|
// Write with writeln!(out, "{}", x). These help with the common shapes:
|
||||||
|
fn yesno(b: bool) -> &'static str {
|
||||||
|
if b {
|
||||||
|
"YES"
|
||||||
|
} else {
|
||||||
|
"NO"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// join(&v, " ") -> "1 2 3"
|
||||||
|
fn join<T: std::fmt::Display>(v: &[T], sep: &str) -> String {
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(sep)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- DEBUG -----
|
||||||
|
// dbg!(x) is built in and goes to stderr, so it never pollutes the .out file.
|
||||||
|
|
||||||
|
// ----- MAX MIN -----
|
||||||
|
fn remax<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b > *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn remin<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b < *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let (mut sc, mut out) = stdio("min-effort-path");
|
||||||
|
let t: usize = sc.next();
|
||||||
|
for _ in 0..t {
|
||||||
|
let grid: Vec<Vec<i32>> = sc.leet_grid();
|
||||||
|
println!("{grid:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
[[1,2,3],[2,3,4]]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
[[1,2,3],[2,3,4]]
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
2
|
||||||
|
3
|
||||||
|
1
|
||||||
|
4
|
||||||
|
3
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
1
|
||||||
|
5
|
||||||
|
0
|
||||||
|
2
|
||||||
|
0
|
||||||
|
1
|
||||||
|
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
#![allow(dead_code, unused_imports, unused_macros, unused_mut, unused_variables)]
|
||||||
|
use std::cmp::{max, min, Ordering, Reverse};
|
||||||
|
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufWriter, Read, Write};
|
||||||
|
|
||||||
|
// ----- TYPES -----
|
||||||
|
type Ll = i64;
|
||||||
|
type Pii = (i32, i32);
|
||||||
|
|
||||||
|
// ----- FILE IO -----
|
||||||
|
// stdio("queue-with-stack") picks the mode from the directory it runs in:
|
||||||
|
// name.in exists -> read name.in, write name.out (USACO + `usaco run/test`)
|
||||||
|
// name.in missing -> read stdin, write stdout (Codeforces submission)
|
||||||
|
// So the same file works locally and when pasted into a judge.
|
||||||
|
fn stdio(name: &str) -> (Scanner, Box<dyn Write>) {
|
||||||
|
let infile = format!("{name}.in");
|
||||||
|
if std::path::Path::new(&infile).exists() {
|
||||||
|
let src = std::fs::read_to_string(&infile).expect("could not read .in file");
|
||||||
|
let out = File::create(format!("{name}.out")).expect("could not create .out file");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(out)))
|
||||||
|
} else {
|
||||||
|
let mut src = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_to_string(&mut src)
|
||||||
|
.expect("could not read stdin");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(io::stdout())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- INPUT -----
|
||||||
|
// Slurps the whole input, then hands out whitespace-separated tokens.
|
||||||
|
// let n: usize = sc.next(); one token
|
||||||
|
// let (a, b) = (sc.next::<i64>(), sc.next::<i64>());
|
||||||
|
// let v: Vec<i64> = sc.vec(n); n tokens
|
||||||
|
// let s: Vec<u8> = sc.bytes(); a word as bytes (s[i] - b'a')
|
||||||
|
struct Scanner {
|
||||||
|
toks: std::str::SplitAsciiWhitespace<'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scanner {
|
||||||
|
fn new(src: &str) -> Scanner {
|
||||||
|
let leaked: &'static str = Box::leak(src.to_owned().into_boxed_str());
|
||||||
|
Scanner {
|
||||||
|
toks: leaked.split_ascii_whitespace(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn next<T: std::str::FromStr>(&mut self) -> T {
|
||||||
|
let tok = self.toks.next().expect("ran out of input");
|
||||||
|
tok.parse().ok().expect("could not parse token")
|
||||||
|
}
|
||||||
|
fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
|
||||||
|
(0..n).map(|_| self.next()).collect()
|
||||||
|
}
|
||||||
|
fn pair<T: std::str::FromStr, U: std::str::FromStr>(&mut self) -> (T, U) {
|
||||||
|
(self.next(), self.next())
|
||||||
|
}
|
||||||
|
fn pairs<T: std::str::FromStr, U: std::str::FromStr>(&mut self, n: usize) -> Vec<(T, U)> {
|
||||||
|
(0..n).map(|_| self.pair()).collect()
|
||||||
|
}
|
||||||
|
fn bytes(&mut self) -> Vec<u8> {
|
||||||
|
self.next::<String>().into_bytes()
|
||||||
|
}
|
||||||
|
fn chars(&mut self) -> Vec<char> {
|
||||||
|
self.next::<String>().chars().collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- OUTPUT -----
|
||||||
|
// Write with writeln!(out, "{}", x). These help with the common shapes:
|
||||||
|
fn yesno(b: bool) -> &'static str {
|
||||||
|
if b {
|
||||||
|
"YES"
|
||||||
|
} else {
|
||||||
|
"NO"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// join(&v, " ") -> "1 2 3"
|
||||||
|
fn join<T: std::fmt::Display>(v: &[T], sep: &str) -> String {
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(sep)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- DEBUG -----
|
||||||
|
// dbg!(x) is built in and goes to stderr, so it never pollutes the .out file.
|
||||||
|
|
||||||
|
// ----- MAX MIN -----
|
||||||
|
fn remax<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b > *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn remin<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b < *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MyQueue {
|
||||||
|
in_stk: VecDeque<i32>,
|
||||||
|
out_stk: VecDeque<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyQueue {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
in_stk: VecDeque::new(),
|
||||||
|
out_stk: VecDeque::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push(&mut self, x: i32) {
|
||||||
|
self.in_stk.push_back(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop(&mut self) -> i32 {
|
||||||
|
let val = self.peek();
|
||||||
|
self.out_stk.pop_back();
|
||||||
|
val
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peek(&mut self) -> i32 {
|
||||||
|
if self.out_stk.is_empty() {
|
||||||
|
while let Some(x) = self.in_stk.pop_back() {
|
||||||
|
self.out_stk.push_back(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.out_stk.back().copied().unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn empty(&self) -> bool {
|
||||||
|
self.in_stk.is_empty() && self.out_stk.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let (mut sc, mut out) = stdio("queue-with-stack");
|
||||||
|
let t: usize = sc.next();
|
||||||
|
let mut queue: MyQueue = MyQueue::new();
|
||||||
|
for _ in 0..t {
|
||||||
|
let command: String = sc.next();
|
||||||
|
match command.as_str() {
|
||||||
|
"MyQueue" => {
|
||||||
|
queue = MyQueue::new();
|
||||||
|
}
|
||||||
|
"push" => {
|
||||||
|
let x = sc.next();
|
||||||
|
queue.push(x);
|
||||||
|
}
|
||||||
|
"peek" => {
|
||||||
|
writeln!(out, "{}", queue.peek()).unwrap();
|
||||||
|
}
|
||||||
|
"pop" => {
|
||||||
|
writeln!(out, "{}", queue.pop()).unwrap();
|
||||||
|
}
|
||||||
|
"empty" => {
|
||||||
|
writeln!(out, "{}", queue.empty()).unwrap();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
writeln!(out, "?").unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
6
|
||||||
|
MyQueue
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
6
|
||||||
|
MyQueue
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
1
|
||||||
|
1
|
||||||
|
false
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
9
|
||||||
|
MyQueue
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
peek
|
||||||
|
push 3
|
||||||
|
pop
|
||||||
|
pop
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
1
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
true
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
12
|
||||||
|
MyQueue
|
||||||
|
empty
|
||||||
|
push 5
|
||||||
|
empty
|
||||||
|
pop
|
||||||
|
empty
|
||||||
|
push 7
|
||||||
|
push 8
|
||||||
|
pop
|
||||||
|
empty
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
true
|
||||||
|
false
|
||||||
|
5
|
||||||
|
true
|
||||||
|
7
|
||||||
|
false
|
||||||
|
8
|
||||||
|
true
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
11
|
||||||
|
MyQueue
|
||||||
|
push 10
|
||||||
|
push 20
|
||||||
|
peek
|
||||||
|
peek
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
peek
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
10
|
||||||
|
10
|
||||||
|
10
|
||||||
|
10
|
||||||
|
20
|
||||||
|
20
|
||||||
|
20
|
||||||
|
true
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
21
|
||||||
|
MyQueue
|
||||||
|
push 1
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
|
push 2
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
|
push 3
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
|
push 4
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
|
push 5
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
1
|
||||||
|
1
|
||||||
|
true
|
||||||
|
2
|
||||||
|
2
|
||||||
|
true
|
||||||
|
3
|
||||||
|
3
|
||||||
|
true
|
||||||
|
4
|
||||||
|
4
|
||||||
|
true
|
||||||
|
5
|
||||||
|
5
|
||||||
|
true
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
11
|
||||||
|
MyQueue
|
||||||
|
push -2147483648
|
||||||
|
push 0
|
||||||
|
push 2147483647
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
peek
|
||||||
|
pop
|
||||||
|
empty
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
-2147483648
|
||||||
|
-2147483648
|
||||||
|
0
|
||||||
|
0
|
||||||
|
2147483647
|
||||||
|
2147483647
|
||||||
|
true
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,502 @@
|
|||||||
|
1
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
||||||
|
11
|
||||||
|
12
|
||||||
|
13
|
||||||
|
14
|
||||||
|
15
|
||||||
|
16
|
||||||
|
17
|
||||||
|
18
|
||||||
|
19
|
||||||
|
20
|
||||||
|
21
|
||||||
|
22
|
||||||
|
23
|
||||||
|
24
|
||||||
|
25
|
||||||
|
26
|
||||||
|
27
|
||||||
|
28
|
||||||
|
29
|
||||||
|
30
|
||||||
|
31
|
||||||
|
32
|
||||||
|
33
|
||||||
|
34
|
||||||
|
35
|
||||||
|
36
|
||||||
|
37
|
||||||
|
38
|
||||||
|
39
|
||||||
|
40
|
||||||
|
41
|
||||||
|
42
|
||||||
|
43
|
||||||
|
44
|
||||||
|
45
|
||||||
|
46
|
||||||
|
47
|
||||||
|
48
|
||||||
|
49
|
||||||
|
50
|
||||||
|
51
|
||||||
|
52
|
||||||
|
53
|
||||||
|
54
|
||||||
|
55
|
||||||
|
56
|
||||||
|
57
|
||||||
|
58
|
||||||
|
59
|
||||||
|
60
|
||||||
|
61
|
||||||
|
62
|
||||||
|
63
|
||||||
|
64
|
||||||
|
65
|
||||||
|
66
|
||||||
|
67
|
||||||
|
68
|
||||||
|
69
|
||||||
|
70
|
||||||
|
71
|
||||||
|
72
|
||||||
|
73
|
||||||
|
74
|
||||||
|
75
|
||||||
|
76
|
||||||
|
77
|
||||||
|
78
|
||||||
|
79
|
||||||
|
80
|
||||||
|
81
|
||||||
|
82
|
||||||
|
83
|
||||||
|
84
|
||||||
|
85
|
||||||
|
86
|
||||||
|
87
|
||||||
|
88
|
||||||
|
89
|
||||||
|
90
|
||||||
|
91
|
||||||
|
92
|
||||||
|
93
|
||||||
|
94
|
||||||
|
95
|
||||||
|
96
|
||||||
|
97
|
||||||
|
98
|
||||||
|
99
|
||||||
|
100
|
||||||
|
101
|
||||||
|
102
|
||||||
|
103
|
||||||
|
104
|
||||||
|
105
|
||||||
|
106
|
||||||
|
107
|
||||||
|
108
|
||||||
|
109
|
||||||
|
110
|
||||||
|
111
|
||||||
|
112
|
||||||
|
113
|
||||||
|
114
|
||||||
|
115
|
||||||
|
116
|
||||||
|
117
|
||||||
|
118
|
||||||
|
119
|
||||||
|
120
|
||||||
|
121
|
||||||
|
122
|
||||||
|
123
|
||||||
|
124
|
||||||
|
125
|
||||||
|
126
|
||||||
|
127
|
||||||
|
128
|
||||||
|
129
|
||||||
|
130
|
||||||
|
131
|
||||||
|
132
|
||||||
|
133
|
||||||
|
134
|
||||||
|
135
|
||||||
|
136
|
||||||
|
137
|
||||||
|
138
|
||||||
|
139
|
||||||
|
140
|
||||||
|
141
|
||||||
|
142
|
||||||
|
143
|
||||||
|
144
|
||||||
|
145
|
||||||
|
146
|
||||||
|
147
|
||||||
|
148
|
||||||
|
149
|
||||||
|
150
|
||||||
|
151
|
||||||
|
152
|
||||||
|
153
|
||||||
|
154
|
||||||
|
155
|
||||||
|
156
|
||||||
|
157
|
||||||
|
158
|
||||||
|
159
|
||||||
|
160
|
||||||
|
161
|
||||||
|
162
|
||||||
|
163
|
||||||
|
164
|
||||||
|
165
|
||||||
|
166
|
||||||
|
167
|
||||||
|
168
|
||||||
|
169
|
||||||
|
170
|
||||||
|
171
|
||||||
|
172
|
||||||
|
173
|
||||||
|
174
|
||||||
|
175
|
||||||
|
176
|
||||||
|
177
|
||||||
|
178
|
||||||
|
179
|
||||||
|
180
|
||||||
|
181
|
||||||
|
182
|
||||||
|
183
|
||||||
|
184
|
||||||
|
185
|
||||||
|
186
|
||||||
|
187
|
||||||
|
188
|
||||||
|
189
|
||||||
|
190
|
||||||
|
191
|
||||||
|
192
|
||||||
|
193
|
||||||
|
194
|
||||||
|
195
|
||||||
|
196
|
||||||
|
197
|
||||||
|
198
|
||||||
|
199
|
||||||
|
200
|
||||||
|
201
|
||||||
|
202
|
||||||
|
203
|
||||||
|
204
|
||||||
|
205
|
||||||
|
206
|
||||||
|
207
|
||||||
|
208
|
||||||
|
209
|
||||||
|
210
|
||||||
|
211
|
||||||
|
212
|
||||||
|
213
|
||||||
|
214
|
||||||
|
215
|
||||||
|
216
|
||||||
|
217
|
||||||
|
218
|
||||||
|
219
|
||||||
|
220
|
||||||
|
221
|
||||||
|
222
|
||||||
|
223
|
||||||
|
224
|
||||||
|
225
|
||||||
|
226
|
||||||
|
227
|
||||||
|
228
|
||||||
|
229
|
||||||
|
230
|
||||||
|
231
|
||||||
|
232
|
||||||
|
233
|
||||||
|
234
|
||||||
|
235
|
||||||
|
236
|
||||||
|
237
|
||||||
|
238
|
||||||
|
239
|
||||||
|
240
|
||||||
|
241
|
||||||
|
242
|
||||||
|
243
|
||||||
|
244
|
||||||
|
245
|
||||||
|
246
|
||||||
|
247
|
||||||
|
248
|
||||||
|
249
|
||||||
|
250
|
||||||
|
251
|
||||||
|
252
|
||||||
|
253
|
||||||
|
254
|
||||||
|
255
|
||||||
|
256
|
||||||
|
257
|
||||||
|
258
|
||||||
|
259
|
||||||
|
260
|
||||||
|
261
|
||||||
|
262
|
||||||
|
263
|
||||||
|
264
|
||||||
|
265
|
||||||
|
266
|
||||||
|
267
|
||||||
|
268
|
||||||
|
269
|
||||||
|
270
|
||||||
|
271
|
||||||
|
272
|
||||||
|
273
|
||||||
|
274
|
||||||
|
275
|
||||||
|
276
|
||||||
|
277
|
||||||
|
278
|
||||||
|
279
|
||||||
|
280
|
||||||
|
281
|
||||||
|
282
|
||||||
|
283
|
||||||
|
284
|
||||||
|
285
|
||||||
|
286
|
||||||
|
287
|
||||||
|
288
|
||||||
|
289
|
||||||
|
290
|
||||||
|
291
|
||||||
|
292
|
||||||
|
293
|
||||||
|
294
|
||||||
|
295
|
||||||
|
296
|
||||||
|
297
|
||||||
|
298
|
||||||
|
299
|
||||||
|
300
|
||||||
|
301
|
||||||
|
302
|
||||||
|
303
|
||||||
|
304
|
||||||
|
305
|
||||||
|
306
|
||||||
|
307
|
||||||
|
308
|
||||||
|
309
|
||||||
|
310
|
||||||
|
311
|
||||||
|
312
|
||||||
|
313
|
||||||
|
314
|
||||||
|
315
|
||||||
|
316
|
||||||
|
317
|
||||||
|
318
|
||||||
|
319
|
||||||
|
320
|
||||||
|
321
|
||||||
|
322
|
||||||
|
323
|
||||||
|
324
|
||||||
|
325
|
||||||
|
326
|
||||||
|
327
|
||||||
|
328
|
||||||
|
329
|
||||||
|
330
|
||||||
|
331
|
||||||
|
332
|
||||||
|
333
|
||||||
|
334
|
||||||
|
335
|
||||||
|
336
|
||||||
|
337
|
||||||
|
338
|
||||||
|
339
|
||||||
|
340
|
||||||
|
341
|
||||||
|
342
|
||||||
|
343
|
||||||
|
344
|
||||||
|
345
|
||||||
|
346
|
||||||
|
347
|
||||||
|
348
|
||||||
|
349
|
||||||
|
350
|
||||||
|
351
|
||||||
|
352
|
||||||
|
353
|
||||||
|
354
|
||||||
|
355
|
||||||
|
356
|
||||||
|
357
|
||||||
|
358
|
||||||
|
359
|
||||||
|
360
|
||||||
|
361
|
||||||
|
362
|
||||||
|
363
|
||||||
|
364
|
||||||
|
365
|
||||||
|
366
|
||||||
|
367
|
||||||
|
368
|
||||||
|
369
|
||||||
|
370
|
||||||
|
371
|
||||||
|
372
|
||||||
|
373
|
||||||
|
374
|
||||||
|
375
|
||||||
|
376
|
||||||
|
377
|
||||||
|
378
|
||||||
|
379
|
||||||
|
380
|
||||||
|
381
|
||||||
|
382
|
||||||
|
383
|
||||||
|
384
|
||||||
|
385
|
||||||
|
386
|
||||||
|
387
|
||||||
|
388
|
||||||
|
389
|
||||||
|
390
|
||||||
|
391
|
||||||
|
392
|
||||||
|
393
|
||||||
|
394
|
||||||
|
395
|
||||||
|
396
|
||||||
|
397
|
||||||
|
398
|
||||||
|
399
|
||||||
|
400
|
||||||
|
401
|
||||||
|
402
|
||||||
|
403
|
||||||
|
404
|
||||||
|
405
|
||||||
|
406
|
||||||
|
407
|
||||||
|
408
|
||||||
|
409
|
||||||
|
410
|
||||||
|
411
|
||||||
|
412
|
||||||
|
413
|
||||||
|
414
|
||||||
|
415
|
||||||
|
416
|
||||||
|
417
|
||||||
|
418
|
||||||
|
419
|
||||||
|
420
|
||||||
|
421
|
||||||
|
422
|
||||||
|
423
|
||||||
|
424
|
||||||
|
425
|
||||||
|
426
|
||||||
|
427
|
||||||
|
428
|
||||||
|
429
|
||||||
|
430
|
||||||
|
431
|
||||||
|
432
|
||||||
|
433
|
||||||
|
434
|
||||||
|
435
|
||||||
|
436
|
||||||
|
437
|
||||||
|
438
|
||||||
|
439
|
||||||
|
440
|
||||||
|
441
|
||||||
|
442
|
||||||
|
443
|
||||||
|
444
|
||||||
|
445
|
||||||
|
446
|
||||||
|
447
|
||||||
|
448
|
||||||
|
449
|
||||||
|
450
|
||||||
|
451
|
||||||
|
452
|
||||||
|
453
|
||||||
|
454
|
||||||
|
455
|
||||||
|
456
|
||||||
|
457
|
||||||
|
458
|
||||||
|
459
|
||||||
|
460
|
||||||
|
461
|
||||||
|
462
|
||||||
|
463
|
||||||
|
464
|
||||||
|
465
|
||||||
|
466
|
||||||
|
467
|
||||||
|
468
|
||||||
|
469
|
||||||
|
470
|
||||||
|
471
|
||||||
|
472
|
||||||
|
473
|
||||||
|
474
|
||||||
|
475
|
||||||
|
476
|
||||||
|
477
|
||||||
|
478
|
||||||
|
479
|
||||||
|
480
|
||||||
|
481
|
||||||
|
482
|
||||||
|
483
|
||||||
|
484
|
||||||
|
485
|
||||||
|
486
|
||||||
|
487
|
||||||
|
488
|
||||||
|
489
|
||||||
|
490
|
||||||
|
491
|
||||||
|
492
|
||||||
|
493
|
||||||
|
494
|
||||||
|
495
|
||||||
|
496
|
||||||
|
497
|
||||||
|
498
|
||||||
|
499
|
||||||
|
500
|
||||||
|
true
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1 @@
|
|||||||
|
https://judge.yosupo.jp/problem/set_xor_min
|
||||||
@@ -0,0 +1,209 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
// ----- TYPES -----
|
||||||
|
typedef long long ll;
|
||||||
|
typedef pair<int, int> pii;
|
||||||
|
typedef vector<int> vi;
|
||||||
|
|
||||||
|
// ----- FILE IO -----
|
||||||
|
#define STDIN(filename) freopen(filename, "r", stdin)
|
||||||
|
#define STDOUT(filename) freopen(filename, "w", stdout)
|
||||||
|
#define FASTIO cin.tie(0)->sync_with_stdio(0)
|
||||||
|
#define STDIO(filename) \
|
||||||
|
FASTIO; \
|
||||||
|
string filenamee(filename); \
|
||||||
|
STDIN((filenamee + ".in").c_str()); \
|
||||||
|
STDOUT((filenamee + ".out").c_str())
|
||||||
|
#define NL '\n'
|
||||||
|
#define CM ", "
|
||||||
|
#define SP " "
|
||||||
|
|
||||||
|
// ----- FOR LOOPS -----
|
||||||
|
#define F(i, L, R) for (int i = L; i < R; i++)
|
||||||
|
#define FE(i, L, R) for (int i = L; i <= R; i++)
|
||||||
|
#define FD(i, L, R) for (int i = L; i > R; i--)
|
||||||
|
#define FDE(i, L, R) for (int i = L; i >= R; i--)
|
||||||
|
|
||||||
|
// ----- WEZ -----
|
||||||
|
#define takeI(n) \
|
||||||
|
int n; \
|
||||||
|
cin >> n
|
||||||
|
#define takeII(n, m) \
|
||||||
|
int n, m; \
|
||||||
|
cin >> n >> m
|
||||||
|
#define takeIII(n, m, k) \
|
||||||
|
int n, m, k; \
|
||||||
|
cin >> n >> m >> k
|
||||||
|
#define takeL(n) \
|
||||||
|
ll n; \
|
||||||
|
cin >> n
|
||||||
|
#define takeS(s) \
|
||||||
|
string s; \
|
||||||
|
cin >> s
|
||||||
|
#define takeV(v, n) \
|
||||||
|
vi v(n); \
|
||||||
|
for (auto &x : v) \
|
||||||
|
cin >> x
|
||||||
|
#define takeVL(v, n) \
|
||||||
|
vector<ll> v(n); \
|
||||||
|
for (auto &x : v) \
|
||||||
|
cin >> x
|
||||||
|
#define whileZ \
|
||||||
|
takeI(T); \
|
||||||
|
while (T--)
|
||||||
|
//
|
||||||
|
// ----- MAX MIN -----
|
||||||
|
#define REMAX(a, b) (a) = max((a), (b))
|
||||||
|
#define REMIN(a, b) (a) = min((a), (b))
|
||||||
|
|
||||||
|
// ----- PAIRS -----
|
||||||
|
#define fi first
|
||||||
|
#define se second
|
||||||
|
|
||||||
|
// ----- OUTPUT -----
|
||||||
|
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
|
||||||
|
|
||||||
|
// ----- DEBUG -----
|
||||||
|
#define DBG(vari) cerr << #vari << " = " << (vari) << endl
|
||||||
|
#define DBG2(a, b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
|
||||||
|
#define printA(a, L, R) FE(i, L, R) cerr << a[i] << (i == R ? '\n' : ' ')
|
||||||
|
#define printV(a) printA(a, 0, SZ(a) - 1)
|
||||||
|
|
||||||
|
// ----- STL DS
|
||||||
|
#define ALL(c) (c).begin(), (c).end()
|
||||||
|
#define SZ(c) ((int)(c).size())
|
||||||
|
#define SORT(c) sort(ALL(c))
|
||||||
|
#define RSORT(c) sort(ALL(c), greater<>())
|
||||||
|
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
|
||||||
|
#define pb push_back
|
||||||
|
|
||||||
|
// struct TrieNode {
|
||||||
|
// bool end = false;
|
||||||
|
// TrieNode* children[2];
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// void insert_into_trie(TrieNode* node, uint x) {
|
||||||
|
// // If we have exhausted x, this current node must be finished
|
||||||
|
// // For example if we have 0
|
||||||
|
// if (!x) {
|
||||||
|
// node->end = true;
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Get last digit of x
|
||||||
|
// bool last_digit = x & 1;
|
||||||
|
//
|
||||||
|
// // Ensure child of this type exists
|
||||||
|
// if (!node->children[last_digit]) {
|
||||||
|
// node->children[last_digit] = new TrieNode;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// insert_into_trie(node->children[last_digit], x >> 1);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// bool remove_from_trie(TrieNode* node, uint x) {
|
||||||
|
// if (!x) {
|
||||||
|
// node->end = false;
|
||||||
|
// } else {
|
||||||
|
// bool last_digit = x & 1;
|
||||||
|
// if (node->children[last_digit] && remove_from_trie(node->children[last_digit], x >> 1)) {
|
||||||
|
// node->children[last_digit] = nullptr;
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// if (!node->children[0] && !node->children[1] && !node->end) {
|
||||||
|
// delete node;
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// int find_min_from_trie(TrieNode* node, uint x) {
|
||||||
|
// if (!x) {
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// bool last_digit = x & 1;
|
||||||
|
//
|
||||||
|
// if (node->children[last_digit]) {
|
||||||
|
// return (find_min_from_trie(node->children[last_digit], x >> 1)) ^ (last_digit << 1);
|
||||||
|
// } else if (node->children[!last_digit]) {
|
||||||
|
// return (find_min_from_trie(node->children[!last_digit], x >> 1)) ^ (!last_digit << 1);
|
||||||
|
// } else {
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
struct Trie {
|
||||||
|
int cnt = 0;
|
||||||
|
Trie* ch[2] = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
void insert(Trie* root, int x) {
|
||||||
|
Trie* cur = root;
|
||||||
|
for (int i = 30; i >= 0; i--) {
|
||||||
|
bool digit = x & (1 << i);
|
||||||
|
if (!cur->ch[digit]) {
|
||||||
|
cur->ch[digit] = new Trie;
|
||||||
|
}
|
||||||
|
cur->ch[digit]->cnt++;
|
||||||
|
cur = cur->ch[digit];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(Trie* root, int x) {
|
||||||
|
Trie* cur = root;
|
||||||
|
for (int i = 30; i >= 0; i--) {
|
||||||
|
bool digit = x & (1 << i);
|
||||||
|
cur->ch[digit]->cnt--;
|
||||||
|
cur = cur->ch[digit];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int query(Trie* root, int x) {
|
||||||
|
Trie* cur = root;
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 30; i >= 0; i--) {
|
||||||
|
bool digit = x & (1 << i);
|
||||||
|
if (cur->ch[digit] && cur->ch[digit]->cnt > 0) {
|
||||||
|
cur = cur->ch[digit];
|
||||||
|
} else {
|
||||||
|
cur = cur->ch[!digit];
|
||||||
|
res += 1 << i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
FASTIO;
|
||||||
|
takeI(t);
|
||||||
|
Trie* root = new Trie;
|
||||||
|
unordered_set<int> s;
|
||||||
|
while (t--) {
|
||||||
|
takeI(op);
|
||||||
|
uint x; cin >> x;
|
||||||
|
|
||||||
|
if (op == 0) {
|
||||||
|
// Insert into trie
|
||||||
|
if (!s.count(x)) {
|
||||||
|
s.insert(x);
|
||||||
|
insert(root, x);
|
||||||
|
}
|
||||||
|
} else if (op == 1) {
|
||||||
|
// Remove from trie
|
||||||
|
if (s.count(x)) {
|
||||||
|
s.erase(x);
|
||||||
|
remove(root, x);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Query trie
|
||||||
|
cout << query(root, x) << NL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
#![allow(dead_code, unused_imports, unused_macros, unused_mut, unused_variables)]
|
||||||
|
use std::cmp::{max, min, Ordering, Reverse};
|
||||||
|
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufWriter, Read, Write};
|
||||||
|
|
||||||
|
// ----- TYPES -----
|
||||||
|
type Ll = i64;
|
||||||
|
type Pii = (i32, i32);
|
||||||
|
|
||||||
|
// ----- FILE IO -----
|
||||||
|
// stdio("name") picks the mode from the directory it runs in:
|
||||||
|
// name.in exists -> read name.in, write name.out (USACO + `usaco run/test`)
|
||||||
|
// name.in missing -> read stdin, write stdout (Codeforces submission)
|
||||||
|
// So the same file works locally and when pasted into a judge.
|
||||||
|
fn stdio(name: &str) -> (Scanner, Box<dyn Write>) {
|
||||||
|
let infile = format!("{name}.in");
|
||||||
|
if std::path::Path::new(&infile).exists() {
|
||||||
|
let src = std::fs::read_to_string(&infile).expect("could not read .in file");
|
||||||
|
let out = File::create(format!("{name}.out")).expect("could not create .out file");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(out)))
|
||||||
|
} else {
|
||||||
|
let mut src = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_to_string(&mut src)
|
||||||
|
.expect("could not read stdin");
|
||||||
|
(Scanner::new(&src), Box::new(BufWriter::new(io::stdout())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- INPUT -----
|
||||||
|
// Slurps the whole input, then hands out whitespace-separated tokens.
|
||||||
|
// let n: usize = sc.next(); one token
|
||||||
|
// let (a, b) = (sc.next::<i64>(), sc.next::<i64>());
|
||||||
|
// let v: Vec<i64> = sc.vec(n); n tokens
|
||||||
|
// let s: Vec<u8> = sc.bytes(); a word as bytes (s[i] - b'a')
|
||||||
|
struct Scanner {
|
||||||
|
toks: std::str::SplitAsciiWhitespace<'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scanner {
|
||||||
|
fn new(src: &str) -> Scanner {
|
||||||
|
let leaked: &'static str = Box::leak(src.to_owned().into_boxed_str());
|
||||||
|
Scanner {
|
||||||
|
toks: leaked.split_ascii_whitespace(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn next<T: std::str::FromStr>(&mut self) -> T {
|
||||||
|
let tok = self.toks.next().expect("ran out of input");
|
||||||
|
tok.parse().ok().expect("could not parse token")
|
||||||
|
}
|
||||||
|
fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
|
||||||
|
(0..n).map(|_| self.next()).collect()
|
||||||
|
}
|
||||||
|
fn pair<T: std::str::FromStr, U: std::str::FromStr>(&mut self) -> (T, U) {
|
||||||
|
(self.next(), self.next())
|
||||||
|
}
|
||||||
|
fn pairs<T: std::str::FromStr, U: std::str::FromStr>(&mut self, n: usize) -> Vec<(T, U)> {
|
||||||
|
(0..n).map(|_| self.pair()).collect()
|
||||||
|
}
|
||||||
|
fn bytes(&mut self) -> Vec<u8> {
|
||||||
|
self.next::<String>().into_bytes()
|
||||||
|
}
|
||||||
|
fn chars(&mut self) -> Vec<char> {
|
||||||
|
self.next::<String>().chars().collect()
|
||||||
|
}
|
||||||
|
fn leet_vec<T: std::str::FromStr>(&mut self) -> Vec<T> {
|
||||||
|
let s: String = self.next();
|
||||||
|
s.trim_matches(|c| c == '[' || c == ']')
|
||||||
|
.split(',')
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.parse().ok().expect("leet vec parse err"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
fn leet_grid<T: std::str::FromStr>(&mut self) -> Vec<Vec<T>> {
|
||||||
|
let s: String = self.next();
|
||||||
|
let s = s.trim_start_matches('[').trim_end_matches(']');
|
||||||
|
let mut res = Vec::new();
|
||||||
|
for row in s.split("],[") {
|
||||||
|
let cur = row
|
||||||
|
.split(',')
|
||||||
|
.filter(|c| !c.is_empty())
|
||||||
|
.map(|c| c.parse().ok().expect("leet grid error"))
|
||||||
|
.collect();
|
||||||
|
res.push(cur);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- OUTPUT -----
|
||||||
|
// Write with writeln!(out, "{}", x). These help with the common shapes:
|
||||||
|
fn yesno(b: bool) -> &'static str {
|
||||||
|
if b {
|
||||||
|
"YES"
|
||||||
|
} else {
|
||||||
|
"NO"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// join(&v, " ") -> "1 2 3"
|
||||||
|
fn join<T: std::fmt::Display>(v: &[T], sep: &str) -> String {
|
||||||
|
v.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(sep)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----- DEBUG -----
|
||||||
|
// dbg!(x) is built in and goes to stderr, so it never pollutes the .out file.
|
||||||
|
|
||||||
|
// ----- MAX MIN -----
|
||||||
|
fn remax<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b > *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn remin<T: PartialOrd>(a: &mut T, b: T) -> bool {
|
||||||
|
if b < *a {
|
||||||
|
*a = b;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let (mut sc, mut out) = stdio("problem");
|
||||||
|
let t: usize = sc.next();
|
||||||
|
for _ in 0..t {}
|
||||||
|
}
|
||||||
@@ -9,32 +9,40 @@
|
|||||||
# r / t / e act on the current problem
|
# r / t / e act on the current problem
|
||||||
# !cmd run a shell command in the problem dir
|
# !cmd run a shell command in the problem dir
|
||||||
# q quit
|
# q quit
|
||||||
# usaco new <name> [io-name] [--url <link>]
|
# usaco new <name> [io-name] [--url <link>] [--rust|--cpp]
|
||||||
# Create solving/<name> from template.cpp
|
# Create solving/<name> from template.cpp or template.rs
|
||||||
# (io-name sets freopen filenames, defaults to <name>)
|
# (io-name sets the file-io name, defaults to <name>;
|
||||||
|
# language defaults to 'usaco lang', see below)
|
||||||
# usaco new <leetcode-url> Import a LeetCode problem (alias: from): statement in
|
# usaco new <leetcode-url> Import a LeetCode problem (alias: from): statement in
|
||||||
# problem.md, C++ starter + generated driver.h, the examples
|
# problem.md, C++ starter + generated driver.h, the examples
|
||||||
# as tests/ with expected outputs
|
# as tests/ with expected outputs
|
||||||
# usaco daily Same, for today's LeetCode daily question
|
# usaco daily Same, for today's LeetCode daily question
|
||||||
# usaco run <name> Build main.cpp and run it (in the problem dir)
|
# usaco lang [cpp|rust] Show or set the default language for 'usaco new'
|
||||||
# usaco build <name> Build main.cpp only
|
# ($USACO_LANG wins for a single shell)
|
||||||
|
# usaco run <name> Build the solution and run it (in the problem dir)
|
||||||
|
# usaco build <name> Build only
|
||||||
# usaco done <name> Move solving/<name> -> solved/<name> and git commit
|
# usaco done <name> Move solving/<name> -> solved/<name> and git commit
|
||||||
# usaco undo <name> Move solved/ or skipped/<name> -> solving/<name>
|
# usaco undo <name> Move solved/ or skipped/<name> -> solving/<name>
|
||||||
# usaco skip <name> Move solving/<name> -> skipped/<name>
|
# usaco skip <name> Move solving/<name> -> skipped/<name>
|
||||||
# usaco io <name> [io-name] Show the problem's io name, or set it: renames <old>.in/.out
|
# usaco io <name> [io-name] Show the problem's io name, or set it: renames <old>.in/.out
|
||||||
# and rewires STDIO("...") in main.cpp (adds it if missing)
|
# and rewires STDIO("...") / stdio("...") in the source
|
||||||
# usaco cat <name> Print the problem's main.cpp to stdout (pipeable)
|
# usaco cat <name> Print the problem's solution to stdout (pipeable)
|
||||||
# usaco copy <name> Copy the problem's main.cpp to the clipboard (alias: cp)
|
# usaco copy <name> Copy the problem's solution to the clipboard (alias: cp)
|
||||||
# usaco vim <name> Open the problem's main.cpp in $EDITOR (aliases: edit, e)
|
# usaco vim <name> Open the problem's solution in $EDITOR (aliases: edit, e)
|
||||||
# usaco open <name> Open the problem's saved URL in a browser
|
# usaco open <name> Open the problem's saved URL in a browser
|
||||||
# usaco list List problems by status
|
# usaco list List problems by status
|
||||||
# usaco stats Show solve counts and recent activity
|
# usaco stats Show solve counts and recent activity
|
||||||
# usaco path <name> Print the problem's directory (used by the ucd shell helper)
|
# usaco path <name> Print the problem's directory (used by the ucd shell helper)
|
||||||
# usaco clean Delete compiled binaries and backups from all problem dirs
|
# usaco clean Delete compiled binaries and backups from all problem dirs
|
||||||
# usaco sync git pull --rebase (picks up edits made on the website), then push
|
# usaco sync git pull --rebase (picks up edits made on the website), then push
|
||||||
# usaco reset <name> [-y] Reset the solution: fresh main.cpp from template (STDIO wired up,
|
# usaco lint [name...] Run clippy over the rust solutions (all of them, or just the
|
||||||
# old one saved as main.cpp.bak), delete binary + .out; keeps tests/
|
# named ones; "template" lints template.rs) and refresh the
|
||||||
# and .in. Asks first unless -y (aliases: rm, delete, template, tpl)
|
# Cargo.toml rust-analyzer reads
|
||||||
|
# usaco reset <name> [lang] [-y]
|
||||||
|
# Reset the solution: fresh source from template (io wired up,
|
||||||
|
# old one saved as .bak), delete binary + .out; keeps tests/ and
|
||||||
|
# .in. cpp/rust switches the problem's language. Asks first
|
||||||
|
# unless -y (aliases: rm, delete, template, tpl)
|
||||||
# usaco test create <name> Save a test case: uses the current .in (or lets you paste one),
|
# usaco test create <name> Save a test case: uses the current .in (or lets you paste one),
|
||||||
# then asks for the expected output. Stored in tests/N.in + N.out
|
# then asks for the expected output. Stored in tests/N.in + N.out
|
||||||
# usaco test <name> [nums...] Build and run every saved test case (or just the given ones),
|
# usaco test <name> [nums...] Build and run every saved test case (or just the given ones),
|
||||||
@@ -74,6 +82,8 @@ case "$cmd" in
|
|||||||
path|p) cmd=path ;;
|
path|p) cmd=path ;;
|
||||||
clean) cmd=clean ;;
|
clean) cmd=clean ;;
|
||||||
sync|sy) cmd=sync ;;
|
sync|sy) cmd=sync ;;
|
||||||
|
lang) cmd=lang ;;
|
||||||
|
lint) cmd=lint ;;
|
||||||
reset|rm|delete|template|tpl) cmd=reset ;;
|
reset|rm|delete|template|tpl) cmd=reset ;;
|
||||||
*) usage ;;
|
*) usage ;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
@@ -34,13 +34,13 @@ _usaco() {
|
|||||||
'n:create a problem from template'
|
'n:create a problem from template'
|
||||||
'from:import a LeetCode problem from its link'
|
'from:import a LeetCode problem from its link'
|
||||||
"daily:import today's LeetCode daily question"
|
"daily:import today's LeetCode daily question"
|
||||||
'run:build and run main.cpp'
|
'run:build and run the solution'
|
||||||
'r:build and run main.cpp'
|
'r:build and run the solution'
|
||||||
'build:build main.cpp'
|
'build:build the solution'
|
||||||
'b:build main.cpp'
|
'b:build the solution'
|
||||||
'vim:open main.cpp in $EDITOR'
|
'vim:open the solution in $EDITOR'
|
||||||
'edit:open main.cpp in $EDITOR'
|
'edit:open the solution in $EDITOR'
|
||||||
'e:open main.cpp in $EDITOR'
|
'e:open the solution in $EDITOR'
|
||||||
'test:run saved test cases (test create <name> to add one)'
|
'test:run saved test cases (test create <name> to add one)'
|
||||||
't:run saved test cases'
|
't:run saved test cases'
|
||||||
'done:mark solved (auto-commits)'
|
'done:mark solved (auto-commits)'
|
||||||
@@ -49,10 +49,10 @@ _usaco() {
|
|||||||
'u:move back to solving'
|
'u:move back to solving'
|
||||||
'skip:set aside for later'
|
'skip:set aside for later'
|
||||||
'sk:set aside for later'
|
'sk:set aside for later'
|
||||||
'io:show or set the freopen io name'
|
'io:show or set the file-io name'
|
||||||
'cat:print main.cpp to stdout'
|
'cat:print the solution to stdout'
|
||||||
'copy:copy main.cpp to the clipboard'
|
'copy:copy the solution to the clipboard'
|
||||||
'cp:copy main.cpp to the clipboard'
|
'cp:copy the solution to the clipboard'
|
||||||
'open:open the saved problem URL'
|
'open:open the saved problem URL'
|
||||||
'o:open the saved problem URL'
|
'o:open the saved problem URL'
|
||||||
'list:list problems by status'
|
'list:list problems by status'
|
||||||
@@ -65,17 +65,20 @@ _usaco() {
|
|||||||
'clean:delete binaries and backups'
|
'clean:delete binaries and backups'
|
||||||
'sync:pull website edits, push local commits'
|
'sync:pull website edits, push local commits'
|
||||||
'sy:pull website edits, push local commits'
|
'sy:pull website edits, push local commits'
|
||||||
|
'lint:run clippy over the rust solutions and template.rs'
|
||||||
'template:same as reset'
|
'template:same as reset'
|
||||||
'tpl:same as reset'
|
'tpl:same as reset'
|
||||||
'reset:fresh main.cpp from template, delete binary/.out'
|
'reset:fresh solution from template (cpp/rust switches language), delete binary/.out'
|
||||||
'rm:fresh main.cpp from template, delete binary/.out'
|
'rm:fresh solution from template (cpp/rust switches language), delete binary/.out'
|
||||||
'delete:fresh main.cpp from template, delete binary/.out'
|
'delete:fresh solution from template (cpp/rust switches language), delete binary/.out'
|
||||||
|
'lang:show or set the default language for new problems'
|
||||||
)
|
)
|
||||||
if (( CURRENT == 2 )); then
|
if (( CURRENT == 2 )); then
|
||||||
_describe 'command' cmds
|
_describe 'command' cmds
|
||||||
elif (( CURRENT == 3 )); then
|
elif (( CURRENT == 3 )); then
|
||||||
case "$words[2]" in
|
case "$words[2]" in
|
||||||
i|interactive|repl|new|n|from|daily|list|l|ls|stats|st|clean|sync|sy) ;; # no problem-name argument
|
i|interactive|repl|new|n|from|daily|list|l|ls|stats|st|clean|sync|sy) ;; # no problem-name argument
|
||||||
|
lang) _values 'language' cpp rust ;;
|
||||||
test|t)
|
test|t)
|
||||||
local -a subs
|
local -a subs
|
||||||
subs=('create:save a new test case' 'add:save a new test case' 'new:save a new test case')
|
subs=('create:save a new test case' 'add:save a new test case' 'new:save a new test case')
|
||||||
@@ -83,8 +86,11 @@ _usaco() {
|
|||||||
_usaco_problems ;;
|
_usaco_problems ;;
|
||||||
*) _usaco_problems ;;
|
*) _usaco_problems ;;
|
||||||
esac
|
esac
|
||||||
elif (( CURRENT == 4 )) && [[ "$words[2]" == (test|t) && "$words[3]" == (create|add|new) ]]; then
|
elif (( CURRENT == 4 )); then
|
||||||
_usaco_problems
|
case "$words[2]" in
|
||||||
|
test|t) [[ "$words[3]" == (create|add|new) ]] && _usaco_problems ;;
|
||||||
|
template|tpl|reset|rm|delete) _values 'language' cpp rust ;;
|
||||||
|
esac
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
if (( $+functions[compdef] )); then
|
if (( $+functions[compdef] )); then
|
||||||
|
|||||||
Reference in New Issue
Block a user