From 3153490991d976f29f701b0f3f77215c56a68754 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 18 Apr 2025 21:59:45 -0400 Subject: [PATCH] feat(cses); refactor structure --- codeforces/835/.clang-format | 9 ++ codeforces/835/.clangd | 34 +++++++ codeforces/835/a.cc | 40 ++++++++ codeforces/835/b.cc | 43 +++++++++ codeforces/835/c.cc | 56 +++++++++++ codeforces/835/compile_flags.txt | 31 ++++++ codeforces/835/d.cc | 51 ++++++++++ codeforces/835/debug_flags.txt | 12 +++ codeforces/835/e.cc | 68 ++++++++++++++ codeforces/835/f.cc | 67 +++++++++++++ codeforces/835/g.cc | 94 +++++++++++++++++++ codeforces/835/h.cc | 35 +++++++ .../835/io/.in | 0 codeforces/835/io/.out | 0 codeforces/835/io/a.in | 10 ++ codeforces/835/io/a.out | 12 +++ codeforces/835/io/b.in | 11 +++ codeforces/835/io/b.out | 8 ++ codeforces/835/io/c.in | 11 +++ codeforces/835/io/c.out | 8 ++ codeforces/835/io/d.in | 13 +++ codeforces/835/io/d.out | 9 ++ codeforces/835/io/e.in | 11 +++ codeforces/835/io/e.out | 8 ++ codeforces/835/io/f.in | 13 +++ codeforces/835/io/f.out | 3 + codeforces/835/io/g.in | 14 +++ codeforces/835/io/g.out | 6 ++ codeforces/835/io/h.in | 0 codeforces/835/io/h.out | 0 codeforces/835/makefile | 28 ++++++ codeforces/835/scripts/debug.sh | 29 ++++++ codeforces/835/scripts/run.sh | 29 ++++++ codeforces/835/scripts/utils.sh | 53 +++++++++++ codeforces/937/f.cc | 3 +- codeforces/937/io/b.out | 2 +- codeforces/937/io/c.out | 2 +- codeforces/937/io/d.out | 2 +- cses/introductory-problems/compile_flags.txt | 25 +++++ cses/introductory-problems/debug_flags.txt | 12 +++ .../{ => io}/apple-division.in | 0 .../{ => io}/apple-division.out | 0 .../{ => io}/chessboard-and-queens.in | 0 .../{ => io}/chessboard-and-queens.out | 0 .../{ => io}/coin-piles.in | 0 .../{ => io}/coin-piles.out | 0 .../{ => io}/creating-strings.in | 0 .../{ => io}/creating-strings.out | 0 .../{ => io}/digit-queries.in | 0 .../{ => io}/digit-queries.out | 0 .../{ => io}/gray-code.in | 0 .../{ => io}/gray-code.out | 0 .../{ => io}/grid-paths.in | 0 .../{ => io}/grid-paths.out | 0 .../{ => io}/increasing-array.in | 0 .../{ => io}/increasing-array.out | 0 .../{ => io}/missing-number.in | 0 .../{ => io}/missing-number.out | 0 .../{ => io}/number-spiral.in | 0 .../{ => io}/number-spiral.out | 0 .../{ => io}/palindrome-reorder.in | 0 .../{ => io}/palindrome-reorder.out | 0 .../{ => io}/permutations.in | 0 .../{ => io}/permutations.out | 0 .../{ => io}/repetitions.in | 0 .../{ => io}/repetitions.out | 0 .../{ => io}/tower-of-hanoi.in | 0 .../{ => io}/tower-of-hanoi.out | 0 .../{ => io}/trailing-zeroes.in | 0 .../{ => io}/trailing-zeroes.out | 0 .../{ => io}/weird-algorithm.in | 0 .../{ => io}/weird-algorithm.out | 0 cses/introductory-problems/makefile | 28 ++++++ cses/introductory-problems/scripts/debug.sh | 29 ++++++ cses/introductory-problems/scripts/run.sh | 29 ++++++ cses/introductory-problems/scripts/utils.sh | 53 +++++++++++ cses/range-queries/.clangd | 38 ++++++-- cses/range-queries/compile_flags.txt | 25 +++++ cses/range-queries/debug_flags.txt | 12 +++ .../{ => io}/dynamic-range-minimum-queries.in | 0 .../dynamic-range-minimum-queries.out | 0 .../{ => io}/dynamic-range-sum-queries.in | 0 .../{ => io}/dynamic-range-sum-queries.out | 0 cses/range-queries/{ => io}/forest-queries.in | 0 .../range-queries/{ => io}/forest-queries.out | 0 cses/range-queries/{ => io}/hotel-queries.in | 0 cses/range-queries/{ => io}/hotel-queries.out | 0 cses/range-queries/{ => io}/list-removals.in | 0 cses/range-queries/{ => io}/list-removals.out | 0 .../{ => io}/prefix-sum-queries.in | 0 cses/range-queries/io/prefix-sum-queries.out | 0 .../{ => io}/range-update-queries.in | 0 .../{ => io}/range-update-queries.out | 0 .../{ => io}/range-xor-queries.in | 0 .../{ => io}/range-xor-queries.out | 0 .../{ => io}/salary-queries-fenwick.in | 0 .../{ => io}/salary-queries-fenwick.out | 0 .../{ => io}/salary-queries-pbds.in | 0 .../{ => io}/salary-queries-pbds.out | 0 .../{ => io}/static-range-minimum-queries.in | 0 .../{ => io}/static-range-minimum-queries.out | 0 .../{ => io}/static-sum-range-queries.in | 0 .../{ => io}/static-sum-range-queries.out | 0 cses/range-queries/makefile | 28 ++++++ cses/range-queries/rui | 55 ----------- cses/range-queries/scripts/debug.sh | 29 ++++++ cses/range-queries/scripts/run.sh | 29 ++++++ cses/range-queries/scripts/utils.sh | 53 +++++++++++ cses/sorting-and-searching/compile_flags.txt | 25 +++++ cses/sorting-and-searching/debug_flags.txt | 12 +++ .../{ => io}/apartments.in | 0 .../{ => io}/apartments.out | 0 .../{ => io}/collecting-numbers.in | 0 .../{ => io}/collecting-numbers.out | 0 .../{ => io}/concert-tickets.in | 0 .../{ => io}/concert-tickets.out | 0 .../{ => io}/distinct-numbers.in | 0 .../{ => io}/distinct-numbers.out | 0 .../{ => io}/ferris-wheel.in | 0 .../{ => io}/ferris-wheel.out | 0 .../{ => io}/maximum-subarray-sum.in | 0 .../{ => io}/maximum-subarray-sum.out | 0 .../{ => io}/missing-coin-sum.in | 0 .../{ => io}/missing-coin-sum.out | 0 .../{ => io}/movie-festival.in | 0 .../{ => io}/movie-festival.out | 0 .../{ => io}/playlist.in | 0 .../{ => io}/playlist.out | 0 .../{ => io}/restaurant-customers.in | 0 .../{ => io}/restaurant-customers.out | 0 .../{ => io}/stick-lengths.in | 0 .../{ => io}/stick-lengths.out | 0 .../{ => io}/sum-of-two-values.in | 0 .../{ => io}/sum-of-two-values.out | 0 cses/sorting-and-searching/{ => io}/towers.in | 0 .../sorting-and-searching/{ => io}/towers.out | 0 .../{ => io}/traffic-lights.in | 0 .../{ => io}/traffic-lights.out | 0 cses/sorting-and-searching/makefile | 28 ++++++ cses/sorting-and-searching/scripts/debug.sh | 29 ++++++ cses/sorting-and-searching/scripts/run.sh | 29 ++++++ cses/sorting-and-searching/scripts/utils.sh | 53 +++++++++++ 142 files changed, 1380 insertions(+), 66 deletions(-) create mode 100644 codeforces/835/.clang-format create mode 100644 codeforces/835/.clangd create mode 100644 codeforces/835/a.cc create mode 100644 codeforces/835/b.cc create mode 100644 codeforces/835/c.cc create mode 100644 codeforces/835/compile_flags.txt create mode 100644 codeforces/835/d.cc create mode 100644 codeforces/835/debug_flags.txt create mode 100644 codeforces/835/e.cc create mode 100644 codeforces/835/f.cc create mode 100644 codeforces/835/g.cc create mode 100644 codeforces/835/h.cc rename cses/range-queries/prefix-sum-queries.out => codeforces/835/io/.in (100%) create mode 100644 codeforces/835/io/.out create mode 100644 codeforces/835/io/a.in create mode 100644 codeforces/835/io/a.out create mode 100644 codeforces/835/io/b.in create mode 100644 codeforces/835/io/b.out create mode 100644 codeforces/835/io/c.in create mode 100644 codeforces/835/io/c.out create mode 100644 codeforces/835/io/d.in create mode 100644 codeforces/835/io/d.out create mode 100644 codeforces/835/io/e.in create mode 100644 codeforces/835/io/e.out create mode 100644 codeforces/835/io/f.in create mode 100644 codeforces/835/io/f.out create mode 100644 codeforces/835/io/g.in create mode 100644 codeforces/835/io/g.out create mode 100644 codeforces/835/io/h.in create mode 100644 codeforces/835/io/h.out create mode 100644 codeforces/835/makefile create mode 100644 codeforces/835/scripts/debug.sh create mode 100644 codeforces/835/scripts/run.sh create mode 100644 codeforces/835/scripts/utils.sh create mode 100644 cses/introductory-problems/debug_flags.txt rename cses/introductory-problems/{ => io}/apple-division.in (100%) rename cses/introductory-problems/{ => io}/apple-division.out (100%) rename cses/introductory-problems/{ => io}/chessboard-and-queens.in (100%) rename cses/introductory-problems/{ => io}/chessboard-and-queens.out (100%) rename cses/introductory-problems/{ => io}/coin-piles.in (100%) rename cses/introductory-problems/{ => io}/coin-piles.out (100%) rename cses/introductory-problems/{ => io}/creating-strings.in (100%) rename cses/introductory-problems/{ => io}/creating-strings.out (100%) rename cses/introductory-problems/{ => io}/digit-queries.in (100%) rename cses/introductory-problems/{ => io}/digit-queries.out (100%) rename cses/introductory-problems/{ => io}/gray-code.in (100%) rename cses/introductory-problems/{ => io}/gray-code.out (100%) rename cses/introductory-problems/{ => io}/grid-paths.in (100%) rename cses/introductory-problems/{ => io}/grid-paths.out (100%) rename cses/introductory-problems/{ => io}/increasing-array.in (100%) rename cses/introductory-problems/{ => io}/increasing-array.out (100%) rename cses/introductory-problems/{ => io}/missing-number.in (100%) rename cses/introductory-problems/{ => io}/missing-number.out (100%) rename cses/introductory-problems/{ => io}/number-spiral.in (100%) rename cses/introductory-problems/{ => io}/number-spiral.out (100%) rename cses/introductory-problems/{ => io}/palindrome-reorder.in (100%) rename cses/introductory-problems/{ => io}/palindrome-reorder.out (100%) rename cses/introductory-problems/{ => io}/permutations.in (100%) rename cses/introductory-problems/{ => io}/permutations.out (100%) rename cses/introductory-problems/{ => io}/repetitions.in (100%) rename cses/introductory-problems/{ => io}/repetitions.out (100%) rename cses/introductory-problems/{ => io}/tower-of-hanoi.in (100%) rename cses/introductory-problems/{ => io}/tower-of-hanoi.out (100%) rename cses/introductory-problems/{ => io}/trailing-zeroes.in (100%) rename cses/introductory-problems/{ => io}/trailing-zeroes.out (100%) rename cses/introductory-problems/{ => io}/weird-algorithm.in (100%) rename cses/introductory-problems/{ => io}/weird-algorithm.out (100%) create mode 100644 cses/introductory-problems/makefile create mode 100644 cses/introductory-problems/scripts/debug.sh create mode 100644 cses/introductory-problems/scripts/run.sh create mode 100644 cses/introductory-problems/scripts/utils.sh create mode 100644 cses/range-queries/debug_flags.txt rename cses/range-queries/{ => io}/dynamic-range-minimum-queries.in (100%) rename cses/range-queries/{ => io}/dynamic-range-minimum-queries.out (100%) rename cses/range-queries/{ => io}/dynamic-range-sum-queries.in (100%) rename cses/range-queries/{ => io}/dynamic-range-sum-queries.out (100%) rename cses/range-queries/{ => io}/forest-queries.in (100%) rename cses/range-queries/{ => io}/forest-queries.out (100%) rename cses/range-queries/{ => io}/hotel-queries.in (100%) rename cses/range-queries/{ => io}/hotel-queries.out (100%) rename cses/range-queries/{ => io}/list-removals.in (100%) rename cses/range-queries/{ => io}/list-removals.out (100%) rename cses/range-queries/{ => io}/prefix-sum-queries.in (100%) create mode 100644 cses/range-queries/io/prefix-sum-queries.out rename cses/range-queries/{ => io}/range-update-queries.in (100%) rename cses/range-queries/{ => io}/range-update-queries.out (100%) rename cses/range-queries/{ => io}/range-xor-queries.in (100%) rename cses/range-queries/{ => io}/range-xor-queries.out (100%) rename cses/range-queries/{ => io}/salary-queries-fenwick.in (100%) rename cses/range-queries/{ => io}/salary-queries-fenwick.out (100%) rename cses/range-queries/{ => io}/salary-queries-pbds.in (100%) rename cses/range-queries/{ => io}/salary-queries-pbds.out (100%) rename cses/range-queries/{ => io}/static-range-minimum-queries.in (100%) rename cses/range-queries/{ => io}/static-range-minimum-queries.out (100%) rename cses/range-queries/{ => io}/static-sum-range-queries.in (100%) rename cses/range-queries/{ => io}/static-sum-range-queries.out (100%) create mode 100644 cses/range-queries/makefile delete mode 100644 cses/range-queries/rui create mode 100644 cses/range-queries/scripts/debug.sh create mode 100644 cses/range-queries/scripts/run.sh create mode 100644 cses/range-queries/scripts/utils.sh create mode 100644 cses/sorting-and-searching/debug_flags.txt rename cses/sorting-and-searching/{ => io}/apartments.in (100%) rename cses/sorting-and-searching/{ => io}/apartments.out (100%) rename cses/sorting-and-searching/{ => io}/collecting-numbers.in (100%) rename cses/sorting-and-searching/{ => io}/collecting-numbers.out (100%) rename cses/sorting-and-searching/{ => io}/concert-tickets.in (100%) rename cses/sorting-and-searching/{ => io}/concert-tickets.out (100%) rename cses/sorting-and-searching/{ => io}/distinct-numbers.in (100%) rename cses/sorting-and-searching/{ => io}/distinct-numbers.out (100%) rename cses/sorting-and-searching/{ => io}/ferris-wheel.in (100%) rename cses/sorting-and-searching/{ => io}/ferris-wheel.out (100%) rename cses/sorting-and-searching/{ => io}/maximum-subarray-sum.in (100%) rename cses/sorting-and-searching/{ => io}/maximum-subarray-sum.out (100%) rename cses/sorting-and-searching/{ => io}/missing-coin-sum.in (100%) rename cses/sorting-and-searching/{ => io}/missing-coin-sum.out (100%) rename cses/sorting-and-searching/{ => io}/movie-festival.in (100%) rename cses/sorting-and-searching/{ => io}/movie-festival.out (100%) rename cses/sorting-and-searching/{ => io}/playlist.in (100%) rename cses/sorting-and-searching/{ => io}/playlist.out (100%) rename cses/sorting-and-searching/{ => io}/restaurant-customers.in (100%) rename cses/sorting-and-searching/{ => io}/restaurant-customers.out (100%) rename cses/sorting-and-searching/{ => io}/stick-lengths.in (100%) rename cses/sorting-and-searching/{ => io}/stick-lengths.out (100%) rename cses/sorting-and-searching/{ => io}/sum-of-two-values.in (100%) rename cses/sorting-and-searching/{ => io}/sum-of-two-values.out (100%) rename cses/sorting-and-searching/{ => io}/towers.in (100%) rename cses/sorting-and-searching/{ => io}/towers.out (100%) rename cses/sorting-and-searching/{ => io}/traffic-lights.in (100%) rename cses/sorting-and-searching/{ => io}/traffic-lights.out (100%) create mode 100644 cses/sorting-and-searching/makefile create mode 100644 cses/sorting-and-searching/scripts/debug.sh create mode 100644 cses/sorting-and-searching/scripts/run.sh create mode 100644 cses/sorting-and-searching/scripts/utils.sh diff --git a/codeforces/835/.clang-format b/codeforces/835/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/835/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: Google +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +AllowShortLoopsOnASingleLine: false diff --git a/codeforces/835/.clangd b/codeforces/835/.clangd new file mode 100644 index 0000000..5b59a31 --- /dev/null +++ b/codeforces/835/.clangd @@ -0,0 +1,34 @@ +CompileFlags: + Add: + -O2 + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wformat=2 + -Wfloat-equal + -Wlogical-op + -Wshift-overflow=2 + -Wnon-virtual-dtor + -Wold-style-cast + -Wcast-qual + -Wuseless-cast + -Wno-sign-promotion + -Wcast-align + -Wunused + -Woverloaded-virtual + -Wconversion + -Wsign-conversion + -Wmisleading-indentation + -Wduplicated-cond + -Wduplicated-branches + -Wlogical-op + -Wnull-dereference + -Wformat=2 + -Wformat-overflow + -Wformat-truncation + -Wdouble-promotion + -Wundef + -DLOCAL + -std=c++20 + -Wno-unknown-pragmas diff --git a/codeforces/835/a.cc b/codeforces/835/a.cc new file mode 100644 index 0000000..93e247d --- /dev/null +++ b/codeforces/835/a.cc @@ -0,0 +1,40 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u4 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + vector a(3); + for (u4 i = 0; i < 3; ++i) { + cin >> a[i]; + } + sort(a.begin(), a.end()); + cout << a[1] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/835/b.cc b/codeforces/835/b.cc new file mode 100644 index 0000000..b029d7e --- /dev/null +++ b/codeforces/835/b.cc @@ -0,0 +1,43 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + u32 _; + cin >> _; + string s; + cin >> s; + i32 ans = 1; + for (auto c : s) { + ans = max(ans, c - 'a' + 1); + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/835/c.cc b/codeforces/835/c.cc new file mode 100644 index 0000000..f6ca1b1 --- /dev/null +++ b/codeforces/835/c.cc @@ -0,0 +1,56 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u4 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + u4 n; + cin >> n; + vector a(n); + for (auto& e : a) + cin >> e; + if (n == 1) { + cout << a[0] << '\n'; + return; + } + vector ans(n); + auto copy = a; + sort(copy.begin(), copy.end()); + auto big = copy.back(), big2 = copy[copy.size() - 2]; + for (u4 i = 0; i < n; ++i) { + if (a[i] == big) { + ans[i] = a[i] - big2; + } else + ans[i] = a[i] - big; + } + for (auto e : ans) + cout << e << ' '; + cout << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/835/compile_flags.txt b/codeforces/835/compile_flags.txt new file mode 100644 index 0000000..af7fb72 --- /dev/null +++ b/codeforces/835/compile_flags.txt @@ -0,0 +1,31 @@ +-O2 +-Wall +-Wextra +-Wpedantic +-Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef +-DLOCAL +-std=c++20 diff --git a/codeforces/835/d.cc b/codeforces/835/d.cc new file mode 100644 index 0000000..4085f5c --- /dev/null +++ b/codeforces/835/d.cc @@ -0,0 +1,51 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u4 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + u4 n; + cin >> n; + vector a(n); + for (auto& e : a) + cin >> e; + u4 i = 0; + while (i < n - 1 && a[i] >= a[i + 1]) { + ++i; + } + while (i < n - 1 && a[i] <= a[i + 1]) { + ++i; + } + + if (i == n - 1) + cout << "YES\n"; + else + cout << "NO\n"; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/835/debug_flags.txt b/codeforces/835/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/codeforces/835/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/codeforces/835/e.cc b/codeforces/835/e.cc new file mode 100644 index 0000000..d292fdf --- /dev/null +++ b/codeforces/835/e.cc @@ -0,0 +1,68 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + u32 n; + cin >> n; + vector a(n); + for (u32 i = 0; i < n; ++i) { + cin >> a[i]; + } + + vector prefix_one(n + 1, 0), postfix_zero(n + 2, 0); + for (u32 i = 0; i < n; ++i) { + prefix_one[i + 1] = (a[i] == 1) + prefix_one[i]; + } + for (i32 i = n - 1; i >= 0; --i) { + postfix_zero[i + 1] = postfix_zero[i + 2] + (a[i] == 0); + } + postfix_zero[0] = postfix_zero[1]; + + i64 ans = 0; + for (u32 i = 0; i < n; ++i) { + if (a[i] == 1) + ans += postfix_zero[i + 2]; + } + i64 init = ans; + + for (u32 i = 0; i < n; ++i) { + if (a[i] == 0) + ans = max(ans, init + postfix_zero[i + 2] - prefix_one[i]); + else if (a[i] == 1) + ans = max(ans, init + prefix_one[i] - postfix_zero[i + 2]); + // } else { // NOTE: forgot 0->1 is possible + // } + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} + diff --git a/codeforces/835/f.cc b/codeforces/835/f.cc new file mode 100644 index 0000000..e9746b7 --- /dev/null +++ b/codeforces/835/f.cc @@ -0,0 +1,67 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + i64 n, c, d; + cin >> n >> c >> d; + + vector a(n); + for (auto& e : a) + cin >> e; + sort(a.rbegin(), a.rend()); + + i64 low = 1, high = d + 2; + i64 ans = -1; + + while (low <= high) { + i64 k = low + (high - low) / 2; + i64 total = 0; + for (i64 i = 0; i < d; ++i) { + total += a[i % k]; + } + + if (total >= c) { + ans = k; + low = k + 1; + } else { + high = k - 1; + } + } + + if (low == 0) { + cout << "Impossible\n"; + } else if (low == d + 1) { + cout << "Infinity\n"; + } else { + cout << ans << '\n'; + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/835/g.cc b/codeforces/835/g.cc new file mode 100644 index 0000000..27261c6 --- /dev/null +++ b/codeforces/835/g.cc @@ -0,0 +1,94 @@ +#include +using namespace std; + +void solve() { + int n, a, b; + cin >> n >> a >> b; + vector>> tree(n + 1); + for (int i = 0; i < n - 1; ++i) { + int u, v, w; + cin >> u >> v >> w; + tree[u].emplace_back(v, w); + tree[v].emplace_back(u, w); + } + + unordered_set xor_values; + vector xor_a(n + 1, 0); + vector visited(n + 1, false); + bool found = false; + int target_xor = -1; + + // First DFS to compute xor_a and check if direct a->b is 0 + function dfs1 = [&](int u, int parent) { + visited[u] = true; + for (auto &[v, w] : tree[u]) { + if (v != parent && !visited[v]) { + xor_a[v] = xor_a[u] ^ w; + if (v == b) { + target_xor = xor_a[v]; + if (target_xor == 0) { + found = true; + } + } + dfs1(v, u); + } + } + }; + + dfs1(a, -1); + if (found) { + cout << "YES\n"; + return; + } + + // Second DFS to collect xor values from a, excluding b + visited.assign(n + 1, false); + function dfs2 = [&](int u, int parent) { + visited[u] = true; + if (u != b) { + xor_values.insert(xor_a[u]); + } + for (auto &[v, w] : tree[u]) { + if (v != parent && !visited[v]) { + dfs2(v, u); + } + } + }; + + dfs2(a, -1); + + // Third DFS to check if any node in b's subtree (or elsewhere) has xor_a[u] + // == target_xor + visited.assign(n + 1, false); + function dfs3 = [&](int u, int parent) { + visited[u] = true; + if (u != a && u != b && xor_values.count(xor_a[u] ^ target_xor)) { + return true; + } + for (auto &[v, w] : tree[u]) { + if (v != parent && !visited[v]) { + if (dfs3(v, u)) { + return true; + } + } + } + return false; + }; + + if (dfs3(b, -1)) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + int t; + cin >> t; + while (t--) { + solve(); + } + return 0; +} diff --git a/codeforces/835/h.cc b/codeforces/835/h.cc new file mode 100644 index 0000000..fc07d12 --- /dev/null +++ b/codeforces/835/h.cc @@ -0,0 +1,35 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +using i32 = int32_t; +using u32 = uint32_t; +using i64 = int64_t; +using u64 = uint64_t; +using f64 = double; +using f128 = long double; +// }}} + +void solve() { + +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + int tc = 1; + cin >> tc; + + for (int t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/cses/range-queries/prefix-sum-queries.out b/codeforces/835/io/.in similarity index 100% rename from cses/range-queries/prefix-sum-queries.out rename to codeforces/835/io/.in diff --git a/codeforces/835/io/.out b/codeforces/835/io/.out new file mode 100644 index 0000000..e69de29 diff --git a/codeforces/835/io/a.in b/codeforces/835/io/a.in new file mode 100644 index 0000000..20645c8 --- /dev/null +++ b/codeforces/835/io/a.in @@ -0,0 +1,10 @@ +9 +5 2 6 +14 3 4 +20 2 1 +1 2 3 +11 19 12 +10 8 20 +6 20 3 +4 1 3 +19 8 4 diff --git a/codeforces/835/io/a.out b/codeforces/835/io/a.out new file mode 100644 index 0000000..cc978f6 --- /dev/null +++ b/codeforces/835/io/a.out @@ -0,0 +1,12 @@ +5 +4 +2 +2 +12 +10 +6 +3 +8 + +[code]: 0 +[time]: 4.3807 ms \ No newline at end of file diff --git a/codeforces/835/io/b.in b/codeforces/835/io/b.in new file mode 100644 index 0000000..f0d2637 --- /dev/null +++ b/codeforces/835/io/b.in @@ -0,0 +1,11 @@ +5 +1 +a +4 +down +10 +codeforces +3 +bcf +5 +zzzzz diff --git a/codeforces/835/io/b.out b/codeforces/835/io/b.out new file mode 100644 index 0000000..6b185fa --- /dev/null +++ b/codeforces/835/io/b.out @@ -0,0 +1,8 @@ +1 +23 +19 +6 +26 + +[code]: 0 +[time]: 11.534 ms diff --git a/codeforces/835/io/c.in b/codeforces/835/io/c.in new file mode 100644 index 0000000..f5f056a --- /dev/null +++ b/codeforces/835/io/c.in @@ -0,0 +1,11 @@ +5 +4 +4 7 3 5 +2 +1 2 +5 +1 2 3 4 5 +3 +4 9 4 +4 +4 4 4 4 diff --git a/codeforces/835/io/c.out b/codeforces/835/io/c.out new file mode 100644 index 0000000..d522fa9 --- /dev/null +++ b/codeforces/835/io/c.out @@ -0,0 +1,8 @@ +-3 2 -4 -2 +-1 1 +-4 -3 -2 -1 1 +-5 5 -5 +0 0 0 0 + +[code]: 0 +[time]: 4.1337 ms diff --git a/codeforces/835/io/d.in b/codeforces/835/io/d.in new file mode 100644 index 0000000..5588b9f --- /dev/null +++ b/codeforces/835/io/d.in @@ -0,0 +1,13 @@ +6 +7 +3 2 2 1 2 2 3 +11 +1 1 1 2 3 3 4 5 6 6 6 +7 +1 2 3 4 3 2 1 +7 +9 7 4 6 9 9 10 +1 +1000000000 +8 +9 4 4 5 9 4 9 10 diff --git a/codeforces/835/io/d.out b/codeforces/835/io/d.out new file mode 100644 index 0000000..20f3ef4 --- /dev/null +++ b/codeforces/835/io/d.out @@ -0,0 +1,9 @@ +YES +YES +NO +YES +YES +NO + +[code]: 0 +[time]: 11.6994 ms diff --git a/codeforces/835/io/e.in b/codeforces/835/io/e.in new file mode 100644 index 0000000..2b0e957 --- /dev/null +++ b/codeforces/835/io/e.in @@ -0,0 +1,11 @@ +5 +4 +1 0 1 0 +6 +0 1 0 0 1 0 +2 +0 0 +8 +1 0 1 1 0 0 0 1 +3 +1 1 1 diff --git a/codeforces/835/io/e.out b/codeforces/835/io/e.out new file mode 100644 index 0000000..e9e6ad1 --- /dev/null +++ b/codeforces/835/io/e.out @@ -0,0 +1,8 @@ +3 +7 +1 +13 +2 + +[code]: 0 +[time]: 4.62437 ms diff --git a/codeforces/835/io/f.in b/codeforces/835/io/f.in new file mode 100644 index 0000000..f2292bf --- /dev/null +++ b/codeforces/835/io/f.in @@ -0,0 +1,13 @@ +6 +2 5 4 +1 2 +2 20 10 +100 10 +3 100 3 +7 2 6 +4 20 3 +4 5 6 7 +4 100000000000 2022 +8217734 927368 26389746 627896974 +2 20 4 +5 1 diff --git a/codeforces/835/io/f.out b/codeforces/835/io/f.out new file mode 100644 index 0000000..857b1c7 --- /dev/null +++ b/codeforces/835/io/f.out @@ -0,0 +1,3 @@ + +[code]: 136 (SIGFPE) +[time]: 112.421 ms diff --git a/codeforces/835/io/g.in b/codeforces/835/io/g.in new file mode 100644 index 0000000..66082db --- /dev/null +++ b/codeforces/835/io/g.in @@ -0,0 +1,14 @@ +3 +5 1 4 +1 3 1 +2 3 2 +4 3 3 +3 5 1 +2 1 2 +1 2 2 +6 2 3 +1 2 1 +2 3 1 +3 4 1 +4 5 3 +5 6 5 diff --git a/codeforces/835/io/g.out b/codeforces/835/io/g.out new file mode 100644 index 0000000..7e95645 --- /dev/null +++ b/codeforces/835/io/g.out @@ -0,0 +1,6 @@ +YES +NO +YES + +[code]: 0 +[time]: 3.91221 ms diff --git a/codeforces/835/io/h.in b/codeforces/835/io/h.in new file mode 100644 index 0000000..e69de29 diff --git a/codeforces/835/io/h.out b/codeforces/835/io/h.out new file mode 100644 index 0000000..e69de29 diff --git a/codeforces/835/makefile b/codeforces/835/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/codeforces/835/makefile @@ -0,0 +1,28 @@ +.PHONY: run debug clean setup init + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/codeforces/835/scripts/debug.sh b/codeforces/835/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/835/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/codeforces/835/scripts/run.sh b/codeforces/835/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/835/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/codeforces/835/scripts/utils.sh b/codeforces/835/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/835/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +} diff --git a/codeforces/937/f.cc b/codeforces/937/f.cc index 8b77fb3..59f911d 100644 --- a/codeforces/937/f.cc +++ b/codeforces/937/f.cc @@ -43,8 +43,7 @@ void solve() { to_take -= take; if (to_take != 0) { - cout << -1 << '\n'; - return; + break; } to_take = next_to_take; diff --git a/codeforces/937/io/b.out b/codeforces/937/io/b.out index 230211e..ed6fa4e 100644 --- a/codeforces/937/io/b.out +++ b/codeforces/937/io/b.out @@ -20,4 +20,4 @@ ..##..## [code]: 0 -[time]: 11.0681 ms \ No newline at end of file +[time]: 11.0681 ms diff --git a/codeforces/937/io/c.out b/codeforces/937/io/c.out index 57f20e7..4705320 100644 --- a/codeforces/937/io/c.out +++ b/codeforces/937/io/c.out @@ -11,4 +11,4 @@ 09:37 PM [code]: 0 -[time]: 4.08173 ms \ No newline at end of file +[time]: 4.08173 ms diff --git a/codeforces/937/io/d.out b/codeforces/937/io/d.out index e24ad61..a79947a 100644 --- a/codeforces/937/io/d.out +++ b/codeforces/937/io/d.out @@ -11,4 +11,4 @@ NO YES [code]: 0 -[time]: 12.0294 ms \ No newline at end of file +[time]: 12.0294 ms diff --git a/cses/introductory-problems/compile_flags.txt b/cses/introductory-problems/compile_flags.txt index 573a6b6..af7fb72 100644 --- a/cses/introductory-problems/compile_flags.txt +++ b/cses/introductory-problems/compile_flags.txt @@ -1,6 +1,31 @@ +-O2 -Wall -Wextra -Wpedantic -Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef -DLOCAL -std=c++20 diff --git a/cses/introductory-problems/debug_flags.txt b/cses/introductory-problems/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/cses/introductory-problems/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/cses/introductory-problems/apple-division.in b/cses/introductory-problems/io/apple-division.in similarity index 100% rename from cses/introductory-problems/apple-division.in rename to cses/introductory-problems/io/apple-division.in diff --git a/cses/introductory-problems/apple-division.out b/cses/introductory-problems/io/apple-division.out similarity index 100% rename from cses/introductory-problems/apple-division.out rename to cses/introductory-problems/io/apple-division.out diff --git a/cses/introductory-problems/chessboard-and-queens.in b/cses/introductory-problems/io/chessboard-and-queens.in similarity index 100% rename from cses/introductory-problems/chessboard-and-queens.in rename to cses/introductory-problems/io/chessboard-and-queens.in diff --git a/cses/introductory-problems/chessboard-and-queens.out b/cses/introductory-problems/io/chessboard-and-queens.out similarity index 100% rename from cses/introductory-problems/chessboard-and-queens.out rename to cses/introductory-problems/io/chessboard-and-queens.out diff --git a/cses/introductory-problems/coin-piles.in b/cses/introductory-problems/io/coin-piles.in similarity index 100% rename from cses/introductory-problems/coin-piles.in rename to cses/introductory-problems/io/coin-piles.in diff --git a/cses/introductory-problems/coin-piles.out b/cses/introductory-problems/io/coin-piles.out similarity index 100% rename from cses/introductory-problems/coin-piles.out rename to cses/introductory-problems/io/coin-piles.out diff --git a/cses/introductory-problems/creating-strings.in b/cses/introductory-problems/io/creating-strings.in similarity index 100% rename from cses/introductory-problems/creating-strings.in rename to cses/introductory-problems/io/creating-strings.in diff --git a/cses/introductory-problems/creating-strings.out b/cses/introductory-problems/io/creating-strings.out similarity index 100% rename from cses/introductory-problems/creating-strings.out rename to cses/introductory-problems/io/creating-strings.out diff --git a/cses/introductory-problems/digit-queries.in b/cses/introductory-problems/io/digit-queries.in similarity index 100% rename from cses/introductory-problems/digit-queries.in rename to cses/introductory-problems/io/digit-queries.in diff --git a/cses/introductory-problems/digit-queries.out b/cses/introductory-problems/io/digit-queries.out similarity index 100% rename from cses/introductory-problems/digit-queries.out rename to cses/introductory-problems/io/digit-queries.out diff --git a/cses/introductory-problems/gray-code.in b/cses/introductory-problems/io/gray-code.in similarity index 100% rename from cses/introductory-problems/gray-code.in rename to cses/introductory-problems/io/gray-code.in diff --git a/cses/introductory-problems/gray-code.out b/cses/introductory-problems/io/gray-code.out similarity index 100% rename from cses/introductory-problems/gray-code.out rename to cses/introductory-problems/io/gray-code.out diff --git a/cses/introductory-problems/grid-paths.in b/cses/introductory-problems/io/grid-paths.in similarity index 100% rename from cses/introductory-problems/grid-paths.in rename to cses/introductory-problems/io/grid-paths.in diff --git a/cses/introductory-problems/grid-paths.out b/cses/introductory-problems/io/grid-paths.out similarity index 100% rename from cses/introductory-problems/grid-paths.out rename to cses/introductory-problems/io/grid-paths.out diff --git a/cses/introductory-problems/increasing-array.in b/cses/introductory-problems/io/increasing-array.in similarity index 100% rename from cses/introductory-problems/increasing-array.in rename to cses/introductory-problems/io/increasing-array.in diff --git a/cses/introductory-problems/increasing-array.out b/cses/introductory-problems/io/increasing-array.out similarity index 100% rename from cses/introductory-problems/increasing-array.out rename to cses/introductory-problems/io/increasing-array.out diff --git a/cses/introductory-problems/missing-number.in b/cses/introductory-problems/io/missing-number.in similarity index 100% rename from cses/introductory-problems/missing-number.in rename to cses/introductory-problems/io/missing-number.in diff --git a/cses/introductory-problems/missing-number.out b/cses/introductory-problems/io/missing-number.out similarity index 100% rename from cses/introductory-problems/missing-number.out rename to cses/introductory-problems/io/missing-number.out diff --git a/cses/introductory-problems/number-spiral.in b/cses/introductory-problems/io/number-spiral.in similarity index 100% rename from cses/introductory-problems/number-spiral.in rename to cses/introductory-problems/io/number-spiral.in diff --git a/cses/introductory-problems/number-spiral.out b/cses/introductory-problems/io/number-spiral.out similarity index 100% rename from cses/introductory-problems/number-spiral.out rename to cses/introductory-problems/io/number-spiral.out diff --git a/cses/introductory-problems/palindrome-reorder.in b/cses/introductory-problems/io/palindrome-reorder.in similarity index 100% rename from cses/introductory-problems/palindrome-reorder.in rename to cses/introductory-problems/io/palindrome-reorder.in diff --git a/cses/introductory-problems/palindrome-reorder.out b/cses/introductory-problems/io/palindrome-reorder.out similarity index 100% rename from cses/introductory-problems/palindrome-reorder.out rename to cses/introductory-problems/io/palindrome-reorder.out diff --git a/cses/introductory-problems/permutations.in b/cses/introductory-problems/io/permutations.in similarity index 100% rename from cses/introductory-problems/permutations.in rename to cses/introductory-problems/io/permutations.in diff --git a/cses/introductory-problems/permutations.out b/cses/introductory-problems/io/permutations.out similarity index 100% rename from cses/introductory-problems/permutations.out rename to cses/introductory-problems/io/permutations.out diff --git a/cses/introductory-problems/repetitions.in b/cses/introductory-problems/io/repetitions.in similarity index 100% rename from cses/introductory-problems/repetitions.in rename to cses/introductory-problems/io/repetitions.in diff --git a/cses/introductory-problems/repetitions.out b/cses/introductory-problems/io/repetitions.out similarity index 100% rename from cses/introductory-problems/repetitions.out rename to cses/introductory-problems/io/repetitions.out diff --git a/cses/introductory-problems/tower-of-hanoi.in b/cses/introductory-problems/io/tower-of-hanoi.in similarity index 100% rename from cses/introductory-problems/tower-of-hanoi.in rename to cses/introductory-problems/io/tower-of-hanoi.in diff --git a/cses/introductory-problems/tower-of-hanoi.out b/cses/introductory-problems/io/tower-of-hanoi.out similarity index 100% rename from cses/introductory-problems/tower-of-hanoi.out rename to cses/introductory-problems/io/tower-of-hanoi.out diff --git a/cses/introductory-problems/trailing-zeroes.in b/cses/introductory-problems/io/trailing-zeroes.in similarity index 100% rename from cses/introductory-problems/trailing-zeroes.in rename to cses/introductory-problems/io/trailing-zeroes.in diff --git a/cses/introductory-problems/trailing-zeroes.out b/cses/introductory-problems/io/trailing-zeroes.out similarity index 100% rename from cses/introductory-problems/trailing-zeroes.out rename to cses/introductory-problems/io/trailing-zeroes.out diff --git a/cses/introductory-problems/weird-algorithm.in b/cses/introductory-problems/io/weird-algorithm.in similarity index 100% rename from cses/introductory-problems/weird-algorithm.in rename to cses/introductory-problems/io/weird-algorithm.in diff --git a/cses/introductory-problems/weird-algorithm.out b/cses/introductory-problems/io/weird-algorithm.out similarity index 100% rename from cses/introductory-problems/weird-algorithm.out rename to cses/introductory-problems/io/weird-algorithm.out diff --git a/cses/introductory-problems/makefile b/cses/introductory-problems/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/cses/introductory-problems/makefile @@ -0,0 +1,28 @@ +.PHONY: run debug clean setup init + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/cses/introductory-problems/scripts/debug.sh b/cses/introductory-problems/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/cses/introductory-problems/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/introductory-problems/scripts/run.sh b/cses/introductory-problems/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/cses/introductory-problems/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/introductory-problems/scripts/utils.sh b/cses/introductory-problems/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/cses/introductory-problems/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +} diff --git a/cses/range-queries/.clangd b/cses/range-queries/.clangd index 4f8ead9..5b59a31 100644 --- a/cses/range-queries/.clangd +++ b/cses/range-queries/.clangd @@ -1,8 +1,34 @@ CompileFlags: Add: - - -Wall - - -Wextra - - -Wpedantic - - -Wshadow - - -DLOCAL - - -Wno-unknown-pragmas \ No newline at end of file + -O2 + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wformat=2 + -Wfloat-equal + -Wlogical-op + -Wshift-overflow=2 + -Wnon-virtual-dtor + -Wold-style-cast + -Wcast-qual + -Wuseless-cast + -Wno-sign-promotion + -Wcast-align + -Wunused + -Woverloaded-virtual + -Wconversion + -Wsign-conversion + -Wmisleading-indentation + -Wduplicated-cond + -Wduplicated-branches + -Wlogical-op + -Wnull-dereference + -Wformat=2 + -Wformat-overflow + -Wformat-truncation + -Wdouble-promotion + -Wundef + -DLOCAL + -std=c++20 + -Wno-unknown-pragmas diff --git a/cses/range-queries/compile_flags.txt b/cses/range-queries/compile_flags.txt index 573a6b6..af7fb72 100644 --- a/cses/range-queries/compile_flags.txt +++ b/cses/range-queries/compile_flags.txt @@ -1,6 +1,31 @@ +-O2 -Wall -Wextra -Wpedantic -Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef -DLOCAL -std=c++20 diff --git a/cses/range-queries/debug_flags.txt b/cses/range-queries/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/cses/range-queries/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/cses/range-queries/dynamic-range-minimum-queries.in b/cses/range-queries/io/dynamic-range-minimum-queries.in similarity index 100% rename from cses/range-queries/dynamic-range-minimum-queries.in rename to cses/range-queries/io/dynamic-range-minimum-queries.in diff --git a/cses/range-queries/dynamic-range-minimum-queries.out b/cses/range-queries/io/dynamic-range-minimum-queries.out similarity index 100% rename from cses/range-queries/dynamic-range-minimum-queries.out rename to cses/range-queries/io/dynamic-range-minimum-queries.out diff --git a/cses/range-queries/dynamic-range-sum-queries.in b/cses/range-queries/io/dynamic-range-sum-queries.in similarity index 100% rename from cses/range-queries/dynamic-range-sum-queries.in rename to cses/range-queries/io/dynamic-range-sum-queries.in diff --git a/cses/range-queries/dynamic-range-sum-queries.out b/cses/range-queries/io/dynamic-range-sum-queries.out similarity index 100% rename from cses/range-queries/dynamic-range-sum-queries.out rename to cses/range-queries/io/dynamic-range-sum-queries.out diff --git a/cses/range-queries/forest-queries.in b/cses/range-queries/io/forest-queries.in similarity index 100% rename from cses/range-queries/forest-queries.in rename to cses/range-queries/io/forest-queries.in diff --git a/cses/range-queries/forest-queries.out b/cses/range-queries/io/forest-queries.out similarity index 100% rename from cses/range-queries/forest-queries.out rename to cses/range-queries/io/forest-queries.out diff --git a/cses/range-queries/hotel-queries.in b/cses/range-queries/io/hotel-queries.in similarity index 100% rename from cses/range-queries/hotel-queries.in rename to cses/range-queries/io/hotel-queries.in diff --git a/cses/range-queries/hotel-queries.out b/cses/range-queries/io/hotel-queries.out similarity index 100% rename from cses/range-queries/hotel-queries.out rename to cses/range-queries/io/hotel-queries.out diff --git a/cses/range-queries/list-removals.in b/cses/range-queries/io/list-removals.in similarity index 100% rename from cses/range-queries/list-removals.in rename to cses/range-queries/io/list-removals.in diff --git a/cses/range-queries/list-removals.out b/cses/range-queries/io/list-removals.out similarity index 100% rename from cses/range-queries/list-removals.out rename to cses/range-queries/io/list-removals.out diff --git a/cses/range-queries/prefix-sum-queries.in b/cses/range-queries/io/prefix-sum-queries.in similarity index 100% rename from cses/range-queries/prefix-sum-queries.in rename to cses/range-queries/io/prefix-sum-queries.in diff --git a/cses/range-queries/io/prefix-sum-queries.out b/cses/range-queries/io/prefix-sum-queries.out new file mode 100644 index 0000000..e69de29 diff --git a/cses/range-queries/range-update-queries.in b/cses/range-queries/io/range-update-queries.in similarity index 100% rename from cses/range-queries/range-update-queries.in rename to cses/range-queries/io/range-update-queries.in diff --git a/cses/range-queries/range-update-queries.out b/cses/range-queries/io/range-update-queries.out similarity index 100% rename from cses/range-queries/range-update-queries.out rename to cses/range-queries/io/range-update-queries.out diff --git a/cses/range-queries/range-xor-queries.in b/cses/range-queries/io/range-xor-queries.in similarity index 100% rename from cses/range-queries/range-xor-queries.in rename to cses/range-queries/io/range-xor-queries.in diff --git a/cses/range-queries/range-xor-queries.out b/cses/range-queries/io/range-xor-queries.out similarity index 100% rename from cses/range-queries/range-xor-queries.out rename to cses/range-queries/io/range-xor-queries.out diff --git a/cses/range-queries/salary-queries-fenwick.in b/cses/range-queries/io/salary-queries-fenwick.in similarity index 100% rename from cses/range-queries/salary-queries-fenwick.in rename to cses/range-queries/io/salary-queries-fenwick.in diff --git a/cses/range-queries/salary-queries-fenwick.out b/cses/range-queries/io/salary-queries-fenwick.out similarity index 100% rename from cses/range-queries/salary-queries-fenwick.out rename to cses/range-queries/io/salary-queries-fenwick.out diff --git a/cses/range-queries/salary-queries-pbds.in b/cses/range-queries/io/salary-queries-pbds.in similarity index 100% rename from cses/range-queries/salary-queries-pbds.in rename to cses/range-queries/io/salary-queries-pbds.in diff --git a/cses/range-queries/salary-queries-pbds.out b/cses/range-queries/io/salary-queries-pbds.out similarity index 100% rename from cses/range-queries/salary-queries-pbds.out rename to cses/range-queries/io/salary-queries-pbds.out diff --git a/cses/range-queries/static-range-minimum-queries.in b/cses/range-queries/io/static-range-minimum-queries.in similarity index 100% rename from cses/range-queries/static-range-minimum-queries.in rename to cses/range-queries/io/static-range-minimum-queries.in diff --git a/cses/range-queries/static-range-minimum-queries.out b/cses/range-queries/io/static-range-minimum-queries.out similarity index 100% rename from cses/range-queries/static-range-minimum-queries.out rename to cses/range-queries/io/static-range-minimum-queries.out diff --git a/cses/range-queries/static-sum-range-queries.in b/cses/range-queries/io/static-sum-range-queries.in similarity index 100% rename from cses/range-queries/static-sum-range-queries.in rename to cses/range-queries/io/static-sum-range-queries.in diff --git a/cses/range-queries/static-sum-range-queries.out b/cses/range-queries/io/static-sum-range-queries.out similarity index 100% rename from cses/range-queries/static-sum-range-queries.out rename to cses/range-queries/io/static-sum-range-queries.out diff --git a/cses/range-queries/makefile b/cses/range-queries/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/cses/range-queries/makefile @@ -0,0 +1,28 @@ +.PHONY: run debug clean setup init + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/cses/range-queries/rui b/cses/range-queries/rui deleted file mode 100644 index f0c957b..0000000 --- a/cses/range-queries/rui +++ /dev/null @@ -1,55 +0,0 @@ - - -basically allows for answering queries of a range [l, r] - -and updating values/ranges in O(lg(n)) - - -so, for example, finding the minimum of elements in range [l, r] -can be answered in log(n) rather than r - l + 1 - - -basically, you divide up the array into pieces: - - -this is the segtree -for each "segment" you store the answer for the query -[--------------- + x] -[------ + x][-------] -[--][-- + x][--][---] -[-][-][x][-][-][-] - -usually way too advanced - -its super powerful b/c you can, for example -find min/max/sum/gcd over ranges, while updating those values OR - -updating a [l, r] range of values, all in O(lgn) - -so, for example: - -- sum from l to r -- update at index i -- increase indices [l, r] by x - -^ all in log(n) -(pretty insane) - -yes, but i alr knew it before - -adv algos is tuff - -say i update something to x -then i need to go back UP and update each segment - -so it takes: O(lg(n)) time too - -the problem: intervals don't cleanly overlap - -so if i do sum[0:n/2+2] - -i first get sum[0:n/2], then combine with sum[n/2:n/2+2] -the "height" is O(log(n)), so in worst-case you traverse down -the entire depth to combine the sum - -last thing: updating a value diff --git a/cses/range-queries/scripts/debug.sh b/cses/range-queries/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/cses/range-queries/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/range-queries/scripts/run.sh b/cses/range-queries/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/cses/range-queries/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/range-queries/scripts/utils.sh b/cses/range-queries/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/cses/range-queries/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +} diff --git a/cses/sorting-and-searching/compile_flags.txt b/cses/sorting-and-searching/compile_flags.txt index 573a6b6..af7fb72 100644 --- a/cses/sorting-and-searching/compile_flags.txt +++ b/cses/sorting-and-searching/compile_flags.txt @@ -1,6 +1,31 @@ +-O2 -Wall -Wextra -Wpedantic -Wshadow +-Wformat=2 +-Wfloat-equal +-Wlogical-op +-Wshift-overflow=2 +-Wnon-virtual-dtor +-Wold-style-cast +-Wcast-qual +-Wuseless-cast +-Wno-sign-promotion +-Wcast-align +-Wunused +-Woverloaded-virtual +-Wconversion +-Wsign-conversion +-Wmisleading-indentation +-Wduplicated-cond +-Wduplicated-branches +-Wlogical-op +-Wnull-dereference +-Wformat=2 +-Wformat-overflow +-Wformat-truncation +-Wdouble-promotion +-Wundef -DLOCAL -std=c++20 diff --git a/cses/sorting-and-searching/debug_flags.txt b/cses/sorting-and-searching/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/cses/sorting-and-searching/debug_flags.txt @@ -0,0 +1,12 @@ +-g3 +-fsanitize=address,undefined +-fsanitize=float-divide-by-zero +-fsanitize=float-cast-overflow +-fno-sanitize-recover=all +-fstack-protector-all +-fstack-usage +-fno-omit-frame-pointer +-fno-inline +-ffunction-sections +-D_GLIBCXX_DEBUG +-D_GLIBCXX_DEBUG_PEDANTIC diff --git a/cses/sorting-and-searching/apartments.in b/cses/sorting-and-searching/io/apartments.in similarity index 100% rename from cses/sorting-and-searching/apartments.in rename to cses/sorting-and-searching/io/apartments.in diff --git a/cses/sorting-and-searching/apartments.out b/cses/sorting-and-searching/io/apartments.out similarity index 100% rename from cses/sorting-and-searching/apartments.out rename to cses/sorting-and-searching/io/apartments.out diff --git a/cses/sorting-and-searching/collecting-numbers.in b/cses/sorting-and-searching/io/collecting-numbers.in similarity index 100% rename from cses/sorting-and-searching/collecting-numbers.in rename to cses/sorting-and-searching/io/collecting-numbers.in diff --git a/cses/sorting-and-searching/collecting-numbers.out b/cses/sorting-and-searching/io/collecting-numbers.out similarity index 100% rename from cses/sorting-and-searching/collecting-numbers.out rename to cses/sorting-and-searching/io/collecting-numbers.out diff --git a/cses/sorting-and-searching/concert-tickets.in b/cses/sorting-and-searching/io/concert-tickets.in similarity index 100% rename from cses/sorting-and-searching/concert-tickets.in rename to cses/sorting-and-searching/io/concert-tickets.in diff --git a/cses/sorting-and-searching/concert-tickets.out b/cses/sorting-and-searching/io/concert-tickets.out similarity index 100% rename from cses/sorting-and-searching/concert-tickets.out rename to cses/sorting-and-searching/io/concert-tickets.out diff --git a/cses/sorting-and-searching/distinct-numbers.in b/cses/sorting-and-searching/io/distinct-numbers.in similarity index 100% rename from cses/sorting-and-searching/distinct-numbers.in rename to cses/sorting-and-searching/io/distinct-numbers.in diff --git a/cses/sorting-and-searching/distinct-numbers.out b/cses/sorting-and-searching/io/distinct-numbers.out similarity index 100% rename from cses/sorting-and-searching/distinct-numbers.out rename to cses/sorting-and-searching/io/distinct-numbers.out diff --git a/cses/sorting-and-searching/ferris-wheel.in b/cses/sorting-and-searching/io/ferris-wheel.in similarity index 100% rename from cses/sorting-and-searching/ferris-wheel.in rename to cses/sorting-and-searching/io/ferris-wheel.in diff --git a/cses/sorting-and-searching/ferris-wheel.out b/cses/sorting-and-searching/io/ferris-wheel.out similarity index 100% rename from cses/sorting-and-searching/ferris-wheel.out rename to cses/sorting-and-searching/io/ferris-wheel.out diff --git a/cses/sorting-and-searching/maximum-subarray-sum.in b/cses/sorting-and-searching/io/maximum-subarray-sum.in similarity index 100% rename from cses/sorting-and-searching/maximum-subarray-sum.in rename to cses/sorting-and-searching/io/maximum-subarray-sum.in diff --git a/cses/sorting-and-searching/maximum-subarray-sum.out b/cses/sorting-and-searching/io/maximum-subarray-sum.out similarity index 100% rename from cses/sorting-and-searching/maximum-subarray-sum.out rename to cses/sorting-and-searching/io/maximum-subarray-sum.out diff --git a/cses/sorting-and-searching/missing-coin-sum.in b/cses/sorting-and-searching/io/missing-coin-sum.in similarity index 100% rename from cses/sorting-and-searching/missing-coin-sum.in rename to cses/sorting-and-searching/io/missing-coin-sum.in diff --git a/cses/sorting-and-searching/missing-coin-sum.out b/cses/sorting-and-searching/io/missing-coin-sum.out similarity index 100% rename from cses/sorting-and-searching/missing-coin-sum.out rename to cses/sorting-and-searching/io/missing-coin-sum.out diff --git a/cses/sorting-and-searching/movie-festival.in b/cses/sorting-and-searching/io/movie-festival.in similarity index 100% rename from cses/sorting-and-searching/movie-festival.in rename to cses/sorting-and-searching/io/movie-festival.in diff --git a/cses/sorting-and-searching/movie-festival.out b/cses/sorting-and-searching/io/movie-festival.out similarity index 100% rename from cses/sorting-and-searching/movie-festival.out rename to cses/sorting-and-searching/io/movie-festival.out diff --git a/cses/sorting-and-searching/playlist.in b/cses/sorting-and-searching/io/playlist.in similarity index 100% rename from cses/sorting-and-searching/playlist.in rename to cses/sorting-and-searching/io/playlist.in diff --git a/cses/sorting-and-searching/playlist.out b/cses/sorting-and-searching/io/playlist.out similarity index 100% rename from cses/sorting-and-searching/playlist.out rename to cses/sorting-and-searching/io/playlist.out diff --git a/cses/sorting-and-searching/restaurant-customers.in b/cses/sorting-and-searching/io/restaurant-customers.in similarity index 100% rename from cses/sorting-and-searching/restaurant-customers.in rename to cses/sorting-and-searching/io/restaurant-customers.in diff --git a/cses/sorting-and-searching/restaurant-customers.out b/cses/sorting-and-searching/io/restaurant-customers.out similarity index 100% rename from cses/sorting-and-searching/restaurant-customers.out rename to cses/sorting-and-searching/io/restaurant-customers.out diff --git a/cses/sorting-and-searching/stick-lengths.in b/cses/sorting-and-searching/io/stick-lengths.in similarity index 100% rename from cses/sorting-and-searching/stick-lengths.in rename to cses/sorting-and-searching/io/stick-lengths.in diff --git a/cses/sorting-and-searching/stick-lengths.out b/cses/sorting-and-searching/io/stick-lengths.out similarity index 100% rename from cses/sorting-and-searching/stick-lengths.out rename to cses/sorting-and-searching/io/stick-lengths.out diff --git a/cses/sorting-and-searching/sum-of-two-values.in b/cses/sorting-and-searching/io/sum-of-two-values.in similarity index 100% rename from cses/sorting-and-searching/sum-of-two-values.in rename to cses/sorting-and-searching/io/sum-of-two-values.in diff --git a/cses/sorting-and-searching/sum-of-two-values.out b/cses/sorting-and-searching/io/sum-of-two-values.out similarity index 100% rename from cses/sorting-and-searching/sum-of-two-values.out rename to cses/sorting-and-searching/io/sum-of-two-values.out diff --git a/cses/sorting-and-searching/towers.in b/cses/sorting-and-searching/io/towers.in similarity index 100% rename from cses/sorting-and-searching/towers.in rename to cses/sorting-and-searching/io/towers.in diff --git a/cses/sorting-and-searching/towers.out b/cses/sorting-and-searching/io/towers.out similarity index 100% rename from cses/sorting-and-searching/towers.out rename to cses/sorting-and-searching/io/towers.out diff --git a/cses/sorting-and-searching/traffic-lights.in b/cses/sorting-and-searching/io/traffic-lights.in similarity index 100% rename from cses/sorting-and-searching/traffic-lights.in rename to cses/sorting-and-searching/io/traffic-lights.in diff --git a/cses/sorting-and-searching/traffic-lights.out b/cses/sorting-and-searching/io/traffic-lights.out similarity index 100% rename from cses/sorting-and-searching/traffic-lights.out rename to cses/sorting-and-searching/io/traffic-lights.out diff --git a/cses/sorting-and-searching/makefile b/cses/sorting-and-searching/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/cses/sorting-and-searching/makefile @@ -0,0 +1,28 @@ +.PHONY: run debug clean setup init + +SRC = $(word 2,$(MAKECMDGOALS)) + +.SILENT: + +run: + sh scripts/run.sh $(SRC) + +debug: + sh scripts/debug.sh $(SRC) + +clean: + rm -rf build/* + +setup: + test -d build || mkdir -p build + test -d io || mkdir -p io + test -d scripts || mkdir -p scripts + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . + test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + +init: + make setup + +%: + @: diff --git a/cses/sorting-and-searching/scripts/debug.sh b/cses/sorting-and-searching/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/cses/sorting-and-searching/scripts/debug.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +DBG_BIN="${BASE}.debug" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +DBG_BIN="build/$DBG_BIN" + +compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/sorting-and-searching/scripts/run.sh b/cses/sorting-and-searching/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/cses/sorting-and-searching/scripts/run.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +. ./scripts/utils.sh + +SRC="$1" +BASE=$(basename "$SRC" .cc) +INPUT="${BASE}.in" +OUTPUT="${BASE}.out" +RUN_BIN="${BASE}.run" + +test -d build || mkdir -p build +test -d io || mkdir -p io + +test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/" +test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/" + +test -f "io/$INPUT" || touch "io/$INPUT" +test -f "io/$OUTPUT" || touch "io/$OUTPUT" + +INPUT="io/$INPUT" +OUTPUT="io/$OUTPUT" +RUN_BIN="build/$RUN_BIN" + +compile_source "$SRC" "$RUN_BIN" "$OUTPUT" "" +CODE=$? +test $CODE -gt 0 && exit $CODE + +execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT" +exit $? diff --git a/cses/sorting-and-searching/scripts/utils.sh b/cses/sorting-and-searching/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/cses/sorting-and-searching/scripts/utils.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +execute_binary() { + binary="$1" + input="$2" + output="$3" + + start=$(date '+%s.%N') + timeout 2s ./"$binary" <"$input" >"$output" 2>&1 + CODE=$? + end=$(date '+%s.%N') + truncate -s "$(head -n 1000 "$output" | wc -c)" "$output" + + if [ $CODE -ge 124 ]; then + MSG='' + case $CODE in + 124) MSG='TIMEOUT' ;; + 128) MSG='SIGILL' ;; + 130) MSG='SIGABRT' ;; + 131) MSG='SIGBUS' ;; + 136) MSG='SIGFPE' ;; + 135) MSG='SIGSEGV' ;; + 137) MSG='SIGPIPE' ;; + 139) MSG='SIGTERM' ;; + esac + [ $CODE -ne 124 ] && sed -i '$d' "$output" + test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output" + else + printf '\n[code]: %s' "$CODE" >>"$output" + fi + + printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output + return $CODE +} + +compile_source() { + src="$1" + bin="$2" + output="$3" + flags="$4" + + test -f "$bin" && rm "$bin" || true + g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output" + CODE=$? + + if [ $CODE -gt 0 ]; then + printf '\n[code]: %s' "$CODE" >>"$output" + return $CODE + else + echo '' >"$output" + return 0 + fi +}