diff --git a/codeforces/1020/.clang-format b/codeforces/1020/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/1020/.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/1020/.clangd b/codeforces/1020/.clangd new file mode 100644 index 0000000..5b59a31 --- /dev/null +++ b/codeforces/1020/.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/1020/a.cc b/codeforces/1020/a.cc new file mode 100644 index 0000000..0ca0fbf --- /dev/null +++ b/codeforces/1020/a.cc @@ -0,0 +1,94 @@ +#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 d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + cin >> n; + string s; + cin >> s; + + u32 ans = 0; + for (auto c : s) + ans += c == '1'; + + ans *= n; + + for (u32 i = 0; i < n; ++i) { + if (s[i] == '0') + ++ans; + else + --ans; + } + + 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/1020/b.cc b/codeforces/1020/b.cc new file mode 100644 index 0000000..5d5164b --- /dev/null +++ b/codeforces/1020/b.cc @@ -0,0 +1,92 @@ +#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 d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, x; + cin >> n >> x; + + if (n == 1) { + cout << "0\n"; + return; + } + + u32 count = n; + for (u32 i = 0; i < x && count; ++i, --count) + cout << i << ' '; + for (u32 i = n - 1; i > x && count; --i, --count) + cout << i << ' '; + if (count) + cout << x << '\n'; + else + 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/1020/c.cc b/codeforces/1020/c.cc new file mode 100644 index 0000000..3a723e5 --- /dev/null +++ b/codeforces/1020/c.cc @@ -0,0 +1,119 @@ +#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 d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n; + i64 k; + cin >> n >> k; + + vec a(n), b(n); + for (auto& e : a) + cin >> e; + + i64 x; + bool x_found = false; + u32 neg_count = 0; + bool bad = false; + for (u32 i = 0; i < n; ++i) { + cin >> b[i]; + if (b[i] == -1) { + ++neg_count; + } else { + auto tx = a[i] + b[i]; + if (x_found && tx != x) { + bad = true; + continue; + } + x_found = true; + x = tx; + } + } + + if (bad) { + cout << "0\n"; + return; + } + + if (neg_count == n) { + cout << *min_element(all(a)) + k - *max_element(all(a)) + 1 << '\n'; + } else { + for (u32 i = 0; i < n; ++i) { + if (!(0 <= x - a[i] && x - a[i] <= k)) { + cout << "0\n"; + return; + } + } + + cout << "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/1020/compile_flags.txt b/codeforces/1020/compile_flags.txt new file mode 100644 index 0000000..5725a8c --- /dev/null +++ b/codeforces/1020/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++23 diff --git a/codeforces/1020/d.cc b/codeforces/1020/d.cc new file mode 100644 index 0000000..1071f27 --- /dev/null +++ b/codeforces/1020/d.cc @@ -0,0 +1,121 @@ +#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 d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + i64 n, m; + cin >> n >> m; + + vector a(n), b(m); + for (auto& e : a) + cin >> e; + for (auto& e : b) + cin >> e; + a.emplace_back(MIN); + + i64 ans = MAX; + + vec post(m + 1, -1); + post[m] = n; + + i64 i = n - 1; + for (i64 j = m - 1; j >= 0 && i >= 0; --j) { + while (i >= 0 && a[i] < b[j]) { + --i; + } + + if (post[j] == -1) + post[j] = i; + --i; + } + + i64 j = 0; + i64 matched = 0; + for (i = 0; i <= n && j < m; ++i) { + if (post[j + 1] >= i) { + ans = min(ans, b[j]); + } + + if (a[i] >= b[j]) { + if (++matched == m) { + ans = 0; + break; + } + ++j; + } + } + + if (ans == MAX) + cout << "-1\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/1020/debug_flags.txt b/codeforces/1020/debug_flags.txt new file mode 100644 index 0000000..03cba1b --- /dev/null +++ b/codeforces/1020/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/1020/e.cc b/codeforces/1020/e.cc new file mode 100644 index 0000000..9ed8757 --- /dev/null +++ b/codeforces/1020/e.cc @@ -0,0 +1,128 @@ +#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 d64 = double; +using d128 = long double; +template +using vec = std::vector; +template +using arr = std::array; +template +using pai = std::pair; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, q; + cin >> n >> q; + vec a(n); + vec m(n + 1); + for (u32 i = 0; i < n; ++i) { + cin >> a[i]; + m[a[i]] = i; + } + + while (q--) { + i64 l, r; + i64 k; + cin >> l >> r >> k; + --l; + --r; + + u32 index = m[k]; + + if (!(l <= index && index <= r)) { + cout << "-1 "; + } else { + u32 L = l, R = r; + u32 lt_need = 0, gt_need = 0, lt_keep = 0, gt_keep = 0; + + while (L <= R) { + u32 M = L + (R - L) / 2; + + if (M == index) { + if (gt_need + k + gt_keep > n || lt_need + lt_keep >= k) { + cout << "-1 "; + } else { + cout << max(lt_need, gt_need) * 2 << ' '; + } + break; + } else if (M < index) { + if (a[M] > k) { + ++lt_need; + } else { + ++lt_keep; + } + L = M + 1; + } else { + if (a[M] < k) { + ++gt_need; + } else { + ++gt_keep; + } + R = M - 1; + } + } + } + } + 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/1020/io/a.in b/codeforces/1020/io/a.in new file mode 100644 index 0000000..50c9bba --- /dev/null +++ b/codeforces/1020/io/a.in @@ -0,0 +1,11 @@ +5 +3 +101 +1 +1 +5 +00000 +2 +11 +3 +010 diff --git a/codeforces/1020/io/a.out b/codeforces/1020/io/a.out new file mode 100644 index 0000000..43f03a9 --- /dev/null +++ b/codeforces/1020/io/a.out @@ -0,0 +1,8 @@ +5 +0 +5 +2 +4 + +[code]: 0 +[time]: 4.38595 ms \ No newline at end of file diff --git a/codeforces/1020/io/b.in b/codeforces/1020/io/b.in new file mode 100644 index 0000000..692bf10 --- /dev/null +++ b/codeforces/1020/io/b.in @@ -0,0 +1,8 @@ +7 +4 2 +4 0 +5 0 +1 1 +3 3 +1 0 +4 3 diff --git a/codeforces/1020/io/b.out b/codeforces/1020/io/b.out new file mode 100644 index 0000000..604b36f --- /dev/null +++ b/codeforces/1020/io/b.out @@ -0,0 +1,10 @@ +0 1 3 2 +3 2 1 0 +4 3 2 1 0 +0 +0 1 2 +0 +0 1 2 3 + +[code]: 0 +[time]: 4.17566 ms \ No newline at end of file diff --git a/codeforces/1020/io/c.in b/codeforces/1020/io/c.in new file mode 100644 index 0000000..ab49182 --- /dev/null +++ b/codeforces/1020/io/c.in @@ -0,0 +1,22 @@ +7 +3 10 +1 3 2 +-1 -1 1 +5 1 +0 1 0 0 1 +-1 0 1 0 -1 +5 1 +0 1 0 0 1 +-1 1 -1 1 -1 +5 10 +1 3 2 5 4 +-1 -1 -1 -1 -1 +5 4 +1 3 2 1 3 +1 -1 -1 1 -1 +5 4 +1 3 2 1 3 +2 -1 -1 2 0 +5 5 +5 0 5 4 3 +5 -1 -1 -1 -1 diff --git a/codeforces/1020/io/c.out b/codeforces/1020/io/c.out new file mode 100644 index 0000000..89949a3 --- /dev/null +++ b/codeforces/1020/io/c.out @@ -0,0 +1,10 @@ +1 +0 +0 +7 +0 +1 +0 + +[code]: 0 +[time]: 3.83496 ms \ No newline at end of file diff --git a/codeforces/1020/io/d.in b/codeforces/1020/io/d.in new file mode 100644 index 0000000..1f1ef67 --- /dev/null +++ b/codeforces/1020/io/d.in @@ -0,0 +1,22 @@ +7 +9 5 +3 5 2 3 3 5 8 1 2 +4 6 2 4 6 +6 3 +1 2 6 8 2 1 +5 4 3 +5 3 +4 3 5 4 3 +7 4 5 +6 3 +8 4 2 1 2 5 +6 1 4 +5 5 +1 2 3 4 5 +5 4 3 2 1 +6 3 +1 2 3 4 5 6 +9 8 7 +5 5 +7 7 6 7 7 +7 7 7 7 7 diff --git a/codeforces/1020/io/d.out b/codeforces/1020/io/d.out new file mode 100644 index 0000000..0750d70 --- /dev/null +++ b/codeforces/1020/io/d.out @@ -0,0 +1,10 @@ +6 +3 +7 +0 +-1 +-1 +7 + +[code]: 0 +[time]: 3.83306 ms \ No newline at end of file diff --git a/codeforces/1020/io/e.in b/codeforces/1020/io/e.in new file mode 100644 index 0000000..a92aa00 --- /dev/null +++ b/codeforces/1020/io/e.in @@ -0,0 +1,30 @@ +8 +5 3 +1 2 3 4 5 +1 5 4 +1 3 4 +3 4 4 +7 4 +3 1 5 2 7 6 4 +3 4 2 +2 3 5 +1 5 6 +1 7 3 +2 1 +2 1 +1 2 1 +1 1 +1 +1 1 1 +7 1 +3 4 2 5 7 1 6 +1 7 1 +16 1 +16 10 12 6 13 9 14 3 8 11 15 2 7 1 5 4 +1 16 4 +16 1 +14 1 3 15 4 5 6 16 7 8 9 10 11 12 13 2 +1 16 14 +13 1 +12 13 10 9 8 4 11 5 7 6 2 1 3 +1 13 2 diff --git a/codeforces/1020/io/e.out b/codeforces/1020/io/e.out new file mode 100644 index 0000000..9be654c --- /dev/null +++ b/codeforces/1020/io/e.out @@ -0,0 +1,11 @@ +0 -1 0 +2 0 -1 4 +-1 +0 +-1 +-1 +-1 +-1 + +[code]: 0 +[time]: 4.73356 ms \ No newline at end of file diff --git a/codeforces/1020/makefile b/codeforces/1020/makefile new file mode 100644 index 0000000..9c5450b --- /dev/null +++ b/codeforces/1020/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/1020/new b/codeforces/1020/new new file mode 100644 index 0000000..541f540 --- /dev/null +++ b/codeforces/1020/new @@ -0,0 +1,51 @@ +12 13 10 9 8 4 11 5 7 6 2 1 3 +l m r + +index at 11 + +M = 7 < index, A[M] =11 > k=2, need one < 2 + +L = 8, R = 13 M = 10 +12 13 10 9 8 4 11 5 7 6 2 1 3 +l M r + +M = 8 < index, A[M] = 6 > k = 2, need one < 2 + +L = 11, R = 13, M = 12, + +M > index, a[M] < k -> need one > 2 +L = 11, R = 11 -> good + +overall, needed 2 lt 2, 1 gt 2 + +12 13 10 9 8 4 1 5 7 6 2 1 3 + +there aren't 2 lt 2 -> no + +1 2 3 4 5 6 7 8 9 10 11 12 13 +12 13 10 9 8 4 11 5 7 6 2 1 3 +1 13 2 + +M = 7, 1 lt (11 > 2, Left) +M = 10, 2 lt (6 > 2, Right) +L = 11, R = 13, M = 12, 1 gt + +done + + +7 4 +3 1 5 2 7 6 4 +3 4 2 +2 3 5 +1 5 6 +1 7 3 + + + +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 +14 1 3 15 4 5 6 16 7 8 9 10 11 12 13 2 +1 16 14 + +L = 1, R = 16, M = 8 -> 16, go left (no cost) +L = 1, R = 7, M = 4 -> 15, left (no cost) +L = 1, R = 3, M = 2 -> go left (1 gt) diff --git a/codeforces/1020/scripts/debug.sh b/codeforces/1020/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/1020/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/1020/scripts/run.sh b/codeforces/1020/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/1020/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/1020/scripts/utils.sh b/codeforces/1020/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/1020/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/970/io/c.out b/codeforces/970/io/c.out index 994290d..27d695a 100644 --- a/codeforces/970/io/c.out +++ b/codeforces/970/io/c.out @@ -5,4 +5,4 @@ 44721 [code]: 0 -[time]: 3.74532 ms \ No newline at end of file +[time]: 4.08649 ms \ No newline at end of file