diff --git a/codeforces/827/.clang-format b/codeforces/827/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/827/.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/827/.clangd b/codeforces/827/.clangd new file mode 100644 index 0000000..298afc3 --- /dev/null +++ b/codeforces/827/.clangd @@ -0,0 +1,45 @@ +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 + -Wno-unknown-pragmas + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 diff --git a/codeforces/827/a.cc b/codeforces/827/a.cc new file mode 100644 index 0000000..1e746ac --- /dev/null +++ b/codeforces/827/a.cc @@ -0,0 +1,85 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 a, b, c; + cin >> a >> b >> c; + vector X{a, b, c}; + sort(all(X)); + if (X[0] + X[1] == X[2]) { + YES(); + } else { + NO(); + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/b.cc b/codeforces/827/b.cc new file mode 100644 index 0000000..688b11f --- /dev/null +++ b/codeforces/827/b.cc @@ -0,0 +1,90 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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; + vector a(n); + for (auto& e : a) + cin >> e; + sort(all(a)); + + for (u32 i = 0; i < n - 1; ++i) { + if (!(a[i] < a[i + 1])) { + NO(); + return; + } + } + YES(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/c.cc b/codeforces/827/c.cc new file mode 100644 index 0000000..dbf580f --- /dev/null +++ b/codeforces/827/c.cc @@ -0,0 +1,100 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + vector grid(8); + for (auto& e : grid) + cin >> e; + + for (u32 r = 0; r < 8; ++r) { + bool good = true; + for (u32 c = 0; c < 8; ++c) { + good &= grid[r][c] == grid[r][0]; + } + if (good && grid[r][0] == 'R') { + cout << grid[r][0] << '\n'; + return; + } + } + for (u32 c = 0; c < 8; ++c) { + bool good = true; + for (u32 r = 0; r < 8; ++r) { + good &= grid[r][c] == grid[0][c]; + } + if (good && grid[0][c] == 'B') { + cout << grid[0][c] << '\n'; + return; + } + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/compile_flags.txt b/codeforces/827/compile_flags.txt new file mode 100644 index 0000000..5373b01 --- /dev/null +++ b/codeforces/827/compile_flags.txt @@ -0,0 +1,30 @@ +-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 +-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/827/d.cc b/codeforces/827/d.cc new file mode 100644 index 0000000..ca9dcd7 --- /dev/null +++ b/codeforces/827/d.cc @@ -0,0 +1,123 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + /* + a[i] has factors S, |S| < = log2(MAX_A[i] = 1000) + + want to find rightmost j > i s.t. no common factors + + factor -> [a[i] divisible by] + + iter thru factors of a[i] + + if i have log2(a[i]) factors; + there are 10^6 factors total + there are 10 factors here + + so, factor each a[i], and manually compute + */ + u32 n; + cin >> n; + vector a(n); + for (auto& e : a) + cin >> e; + + vector> coprime(1001); + for (u32 i = 1; i <= 1000; ++i) { + for (u32 j = 1; j <= 1000; ++j) { + coprime[i][j] = gcd(i, j) == 1; + } + } + + vector right(1001, -1); + for (u32 i = 0; i < n; ++i) { + right[a[i]] = i; + } + + u32 ans = 0; + for (i32 i = 1; i <= 1000; ++i) { + for (u32 j = 1; j <= 1000; ++j) { + if (coprime[i][j] && right[i] != -1 && right[j] != -1) { + ans = max(ans, right[i] + right[j] + 2); + } + } + } + + if (ans == 0) + cout << -1; + else + cout << ans; + cout << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/debug_flags.txt b/codeforces/827/debug_flags.txt new file mode 100644 index 0000000..62a0135 --- /dev/null +++ b/codeforces/827/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++23 diff --git a/codeforces/827/e.cc b/codeforces/827/e.cc new file mode 100644 index 0000000..b052356 --- /dev/null +++ b/codeforces/827/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 f64 = double; +using f128 = long double; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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 biggest_step(n + 1, 0), prefix(n + 1, 0); + for (u32 i = 0; i < n; ++i) { + cin >> a[i]; + biggest_step[i + 1] = max(biggest_step[i], a[i]); + prefix[i + 1] = prefix[i] + a[i]; + } + u64 k; + for (u32 i = 0; i < q; ++i) { + cin >> k; + + u32 d = + distance(biggest_step.begin(), upper_bound(all(biggest_step), k)) - 1; + cout << prefix[d] << " \n"[i == q - 1]; + } + + // vec a(n + 1); + // a[0] = 0; + // for (u32 i = 1; i <= n; ++i) { + // cin >> a[i]; + // a[i] += a[i - 1]; + // } + + // u64 k; + // vec> questions(q); + // vec ans(q); + // + // for (u32 i = 0; i < q; ++i) { + // cin >> questions[i].ff; + // questions[i].ss = i; + // } + // + // sort(all(questions)); + // + // u32 j = 0; + // for (auto& [k, i] : questions) { + // u32 J = j; + // while (J <= n && a[J] <= k + (J ? a[J - 1] : 0)) + // ++J; + // + // if (J > j) { + // j = J - 1; + // } + // + // ans[i] = a[j]; + // } + // + // for (u32 i = 0; i < q; ++i) { + // cout << ans[i] << " \n"[i == q - 1]; + // } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/f.cc b/codeforces/827/f.cc new file mode 100644 index 0000000..7a02f5a --- /dev/null +++ b/codeforces/827/f.cc @@ -0,0 +1,124 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 q; + cin >> q; + vec S(26, 0), T(26, 0); + u64 scount = 1, tcount = 1; + S[0] = T[0] = 1; + u64 cmd, k; + string x; + for (u64 i = 0; i < q; ++i) { + cin >> cmd >> k >> x; + if (cmd == 1) { + for (auto c : x) { + S[c - 'a'] += k; + } + scount += k * x.size(); + } else { + for (auto c : x) { + T[c - 'a'] += k; + } + tcount += k * x.size(); + } + int loS = 0; + while (loS < 26 && S[loS] == 0) + ++loS; + + int hiT = 25; + while (hiT >= 0 && T[hiT] == 0) + --hiT; + + bool ok; + if (loS < hiT) { + ok = true; + } else if (loS > hiT) { + ok = false; + } else { + int c = loS; + + bool biggerInS = false; + for (int ch = c + 1; ch < 26 && !biggerInS; ++ch) + biggerInS = S[ch] > 0; + + if (biggerInS) + ok = false; + else + ok = S[c] < T[c]; + } + + cout << (ok ? "YES\n" : "NO\n"); + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/g.cc b/codeforces/827/g.cc new file mode 100644 index 0000000..6a670e3 --- /dev/null +++ b/codeforces/827/g.cc @@ -0,0 +1,118 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +bitset<2 * 100000 + 1> seen; +void solve() { + seen.reset(); + u32 n; + cin >> n; + vec a(n); + for (auto& e : a) + cin >> e; + + vec ans; + + u32 prefix_or = 0; + + for (u32 i = 0; i < 31; ++i) { + u32 current_or = prefix_or; + i32 index = -1; + for (u32 j = 0; j < n; ++j) { + if (seen[j]) + continue; + + if ((prefix_or | a[j]) > current_or) { + current_or = prefix_or | a[j]; + index = j; + } + } + + if (index == -1) + break; + + ans.push_back(a[index]); + seen[index] = true; + prefix_or = current_or; + } + + for (u32 i = 0; i < n; ++i) { + if (!seen[i]) { + ans.push_back(a[i]); + } + } + + for (u32 i = 0; i < n; ++i) { + cout << ans[i] << " \n"[i == n - 1]; + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/827/io/a.in b/codeforces/827/io/a.in new file mode 100644 index 0000000..66bf48b --- /dev/null +++ b/codeforces/827/io/a.in @@ -0,0 +1,9 @@ +7 +1 4 3 +2 5 8 +9 11 20 +0 0 0 +20 20 20 +4 12 3 +15 7 8 + diff --git a/codeforces/827/io/a.out b/codeforces/827/io/a.out new file mode 100644 index 0000000..be76365 --- /dev/null +++ b/codeforces/827/io/a.out @@ -0,0 +1,10 @@ +YES +NO +YES +YES +NO +NO +YES + +[code]: 0 +[time]: 11.7166 ms \ No newline at end of file diff --git a/codeforces/827/io/b.in b/codeforces/827/io/b.in new file mode 100644 index 0000000..50804bb --- /dev/null +++ b/codeforces/827/io/b.in @@ -0,0 +1,7 @@ +3 +4 +1 1 1 1 +5 +8 7 1 3 4 +1 +5 diff --git a/codeforces/827/io/b.out b/codeforces/827/io/b.out new file mode 100644 index 0000000..d5a5553 --- /dev/null +++ b/codeforces/827/io/b.out @@ -0,0 +1,6 @@ +NO +YES +YES + +[code]: 0 +[time]: 3.60322 ms \ No newline at end of file diff --git a/codeforces/827/io/c.in b/codeforces/827/io/c.in new file mode 100644 index 0000000..f50e299 --- /dev/null +++ b/codeforces/827/io/c.in @@ -0,0 +1,41 @@ +4 + + +....B... +....B... +....B... +RRRRRRRR +....B... +....B... +....B... +....B... + + +RRRRRRRB +B......B +B......B +B......B +B......B +B......B +B......B +RRRRRRRB + + +RRRRRRBB +.B.B..BB +RRRRRRBB +.B.B..BB +.B.B..BB +RRRRRRBB +.B.B..BB +.B.B..BB + + +........ +........ +........ +RRRRRRRR +........ +........ +........ +........ diff --git a/codeforces/827/io/c.out b/codeforces/827/io/c.out new file mode 100644 index 0000000..b739251 --- /dev/null +++ b/codeforces/827/io/c.out @@ -0,0 +1,7 @@ +R +B +B +R + +[code]: 0 +[time]: 10.8039 ms \ No newline at end of file diff --git a/codeforces/827/io/d.in b/codeforces/827/io/d.in new file mode 100644 index 0000000..148fb06 --- /dev/null +++ b/codeforces/827/io/d.in @@ -0,0 +1,13 @@ +6 +3 +3 2 1 +7 +1 3 5 2 4 7 7 +5 +1 2 3 4 5 +3 +2 2 4 +6 +5 4 3 15 12 16 +5 +1 2 2 3 6 diff --git a/codeforces/827/io/d.out b/codeforces/827/io/d.out new file mode 100644 index 0000000..4f323bf --- /dev/null +++ b/codeforces/827/io/d.out @@ -0,0 +1,9 @@ +6 +12 +9 +-1 +10 +7 + +[code]: 0 +[time]: 107.307 ms \ No newline at end of file diff --git a/codeforces/827/io/e.in b/codeforces/827/io/e.in new file mode 100644 index 0000000..936211c --- /dev/null +++ b/codeforces/827/io/e.in @@ -0,0 +1,10 @@ +3 +4 5 +1 2 1 5 +1 2 4 9 10 +2 2 +1 1 +0 1 +3 1 +1000000000 1000000000 1000000000 +1000000000 diff --git a/codeforces/827/io/e.out b/codeforces/827/io/e.out new file mode 100644 index 0000000..6d9ec8d --- /dev/null +++ b/codeforces/827/io/e.out @@ -0,0 +1,6 @@ +1 4 4 9 9 +0 2 +3000000000 + +[code]: 0 +[time]: 4.2367 ms \ No newline at end of file diff --git a/codeforces/827/io/f.in b/codeforces/827/io/f.in new file mode 100644 index 0000000..793497d --- /dev/null +++ b/codeforces/827/io/f.in @@ -0,0 +1,14 @@ +3 +5 +2 1 aa +1 2 a +2 3 a +1 2 b +2 3 abca +2 +1 5 mihai +2 2 buiucani +3 +1 5 b +2 3 a +2 4 paiu diff --git a/codeforces/827/io/f.out b/codeforces/827/io/f.out new file mode 100644 index 0000000..a8edb58 --- /dev/null +++ b/codeforces/827/io/f.out @@ -0,0 +1,13 @@ +YES +NO +YES +NO +YES +NO +YES +NO +NO +YES + +[code]: 0 +[time]: 3.97944 ms diff --git a/codeforces/827/io/g.in b/codeforces/827/io/g.in new file mode 100644 index 0000000..19d0270 --- /dev/null +++ b/codeforces/827/io/g.in @@ -0,0 +1,11 @@ +5 +4 +1 2 4 8 +7 +5 1 2 3 4 5 5 +2 +1 101 +6 +2 3 4 2 3 4 +8 +1 4 2 3 4 5 7 1 diff --git a/codeforces/827/io/g.out b/codeforces/827/io/g.out new file mode 100644 index 0000000..41ce9f7 --- /dev/null +++ b/codeforces/827/io/g.out @@ -0,0 +1,8 @@ +8 4 2 1 +5 2 1 3 4 5 5 +101 1 +4 3 2 2 3 4 +7 1 4 2 3 4 5 1 + +[code]: 0 +[time]: 3.94392 ms \ No newline at end of file diff --git a/codeforces/827/makefile b/codeforces/827/makefile new file mode 100644 index 0000000..3c50267 --- /dev/null +++ b/codeforces/827/makefile @@ -0,0 +1,30 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/codeforces/827/scripts/debug.sh b/codeforces/827/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/827/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/827/scripts/run.sh b/codeforces/827/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/827/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/827/scripts/utils.sh b/codeforces/827/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/827/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/886/.clangd b/codeforces/886/.clangd index 4f8ead9..3fb8fcf 100644 --- a/codeforces/886/.clangd +++ b/codeforces/886/.clangd @@ -5,4 +5,6 @@ CompileFlags: - -Wpedantic - -Wshadow - -DLOCAL - - -Wno-unknown-pragmas \ No newline at end of file + - -Wno-unknown-pragmas -std=c++23 + -std=c++23 + -std=c++23 diff --git a/codeforces/886/compile_flags.txt b/codeforces/886/compile_flags.txt index 5725a8c..5373b01 100644 --- a/codeforces/886/compile_flags.txt +++ b/codeforces/886/compile_flags.txt @@ -16,7 +16,6 @@ -Wunused -Woverloaded-virtual -Wconversion --Wsign-conversion -Wmisleading-indentation -Wduplicated-cond -Wduplicated-branches diff --git a/codeforces/886/debug_flags.txt b/codeforces/886/debug_flags.txt index 03cba1b..af1b521 100644 --- a/codeforces/886/debug_flags.txt +++ b/codeforces/886/debug_flags.txt @@ -10,3 +10,5 @@ -ffunction-sections -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC +-DLOCAL +-std=c++20 diff --git a/codeforces/886/makefile b/codeforces/886/makefile index 9c5450b..78778b6 100644 --- a/codeforces/886/makefile +++ b/codeforces/886/makefile @@ -1,5 +1,7 @@ .PHONY: run debug clean setup init +VERSION ?= 20 + SRC = $(word 2,$(MAKECMDGOALS)) .SILENT: @@ -17,9 +19,8 @@ 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 . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd init: make setup diff --git a/codeforces/903/.clang-format b/codeforces/903/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/903/.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/903/.clangd b/codeforces/903/.clangd new file mode 100644 index 0000000..1d6c778 --- /dev/null +++ b/codeforces/903/.clangd @@ -0,0 +1,45 @@ +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 + -Wno-unknown-pragmas + -std=c++23 + -std=c++20 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 diff --git a/codeforces/903/a.cc b/codeforces/903/a.cc new file mode 100644 index 0000000..98e6c51 --- /dev/null +++ b/codeforces/903/a.cc @@ -0,0 +1,91 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 n, m; + cin >> n >> m; + string N, M; + cin >> N >> M; + + string cur = N; + for (u32 ans = 0; ans <= 5; ++ans) { + if (cur.find(M) != string::npos) { + cout << ans << '\n'; + return; + } + cur += cur; + } + + cout << "-1\n"; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/903/b.cc b/codeforces/903/b.cc new file mode 100644 index 0000000..8dad31a --- /dev/null +++ b/codeforces/903/b.cc @@ -0,0 +1,106 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + u32 a, b, c; + + cin >> a >> b >> c; + + for (u32 mask = 0; mask < (1 << 9) - 1; ++mask) { + if (__builtin_popcount(mask) > 3) + continue; + + u32 right = __builtin_popcount(mask & ((1 << 3) - 1)); + u32 mid = __builtin_popcount((mask >> 3) & ((1 << 3) - 1)); + u32 left = __builtin_popcount((mask >> 6) & ((1 << 3) - 1)); + + if (left && a % (left + 1)) + continue; + if (mid && b % (mid + 1)) + continue; + if (right && c % (right + 1)) + continue; + + u32 A = left ? a / (left + 1) : a; + u32 B = mid ? b / (mid + 1) : b; + u32 C = right ? c / (right + 1) : c; + + if (A == B && B == C) { + YES(); + return; + } + } + + NO(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/903/c.cc b/codeforces/903/c.cc new file mode 100644 index 0000000..4d2b02c --- /dev/null +++ b/codeforces/903/c.cc @@ -0,0 +1,113 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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; + vector grid(n); + for (auto& e : grid) + cin >> e; + /* + core strategy: walk every char in tL region, find other 3 matching chars +rotationally + - find formula + sort, dist is sum of char distances for each smaller than biggest (can only +inc char) + + this is ans + + optimizes walking "rin" + +impl: fill in grid as we go fore easer coverage + + actually, just walk top side + */ + + u32 ans = 0; + for (i32 r = 0; r < n / 2; ++r) { + // NOTE: missed this condition + other vars, made unecessary + // // no need for pairs + // why is auto comment on, fix the autogroup + for (i32 c = r; c < n - 1 - r; ++c) { + char big = max({grid[r][c], grid[c][n - 1 - r], + grid[n - 1 - r][n - 1 - c], grid[n - 1 - c][r]}); + + ans += big - grid[r][c]; + ans += big - grid[c][n - 1 - r]; + ans += big - grid[n - 1 - r][n - 1 - c]; + ans += big - grid[n - 1 - c][r]; + } + } + cout << ans << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/903/compile_flags.txt b/codeforces/903/compile_flags.txt new file mode 100644 index 0000000..5373b01 --- /dev/null +++ b/codeforces/903/compile_flags.txt @@ -0,0 +1,30 @@ +-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 +-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/903/d.cc b/codeforces/903/d.cc new file mode 100644 index 0000000..802df45 --- /dev/null +++ b/codeforces/903/d.cc @@ -0,0 +1,132 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +unordered_map factor_counts; +vector primes; + +void solve() { + u32 n; + cin >> n; + vector a(n); + for (auto& e : a) + cin >> e; + + factor_counts.clear(); + + // NOTE: factorization skills are weak,. i.e. number theory and prime + // factorization i.e. skipping over 2 + // also could binary search + // // NOTE: thought it was "minimum operations" complicating it + for (auto e : a) { + for (const auto& p : primes) { + if (p * p > e) + break; + u32 count = 0; + while (e % p == 0) { + ++count; + e /= p; + } + if (count > 0) { + factor_counts[p] += count; + } + } + if (e > 1) { + ++factor_counts[e]; + } + } + + for (const auto& [factor, count] : factor_counts) { + if (count % n) { + NO(); + return; + } + } + YES(); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + vector P(10001, true); + P[0] = P[1] = false; + for (u32 p = 2; p * p <= 10000; ++p) { + if (P[p]) { + for (u32 i = p * p; i <= 10000; i += p) { + P[i] = false; + } + } + } + + for (u32 i = 2; i <= 10000; ++i) { + if (P[i]) { + primes.push_back(i); + } + } + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/903/debug_flags.txt b/codeforces/903/debug_flags.txt new file mode 100644 index 0000000..af1b521 --- /dev/null +++ b/codeforces/903/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++20 diff --git a/codeforces/903/e.cc b/codeforces/903/e.cc new file mode 100644 index 0000000..9fb2897 --- /dev/null +++ b/codeforces/903/e.cc @@ -0,0 +1,96 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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; + vector a(n); + for (auto& x : a) + cin >> x; + + vector dp(n + 1, n); + dp[n] = 0; + + for (i32 i = n - 1; i >= 0; --i) { + u32 dont = 1 + dp[i + 1]; + + u32 take = i + a[i] + 1; + if (take <= n) + dont = min(dont, dp[take]); + + dp[i] = dont; + } + + cout << dp[0] << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/903/f.cc b/codeforces/903/f.cc new file mode 100644 index 0000000..365d479 --- /dev/null +++ b/codeforces/903/f.cc @@ -0,0 +1,142 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +const u32 MAXN = 2 * 1e5 + 1; +bitset marked, seen; +vector> tree(MAXN); +deque q; + +void solve() { + u32 n, k; + cin >> n >> k; + + marked.reset(); + tree.assign(n + 1, vector()); + + u32 node; + for (u32 i = 0; i < k; ++i) { + cin >> node; + marked[node] = true; + } + + u32 u, v; + for (u32 i = 0; i < n - 1; ++i) { + cin >> u >> v; + tree[u].emplace_back(v); + tree[v].emplace_back(u); + } + + q.clear(); + q.push_back(node); + seen.reset(); + u32 last = node; + + while (!q.empty()) { + auto top = q.front(); + q.pop_front(); + seen[top] = true; + if (marked[top]) + last = top; + + for (auto& neighbor : tree[top]) { + if (!seen[neighbor]) { + q.push_back(neighbor); + } + } + } + + q.clear(); + q.push_back(last); + seen.reset(); + u32 ans = 0, d = 0; + + while (!q.empty()) { + ++d; + u32 size = q.size(); + for (u32 i = 0; i < size; ++i) { + auto top = q.front(); + q.pop_front(); + if (marked[top]) + ans = max(ans, d); + seen[top] = true; + for (auto& neighbor : tree[top]) { + if (!seen[neighbor]) { + q.push_back(neighbor); + } + } + } + } + + cout << ans / 2 << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/903/io/a.in b/codeforces/903/io/a.in new file mode 100644 index 0000000..307311a --- /dev/null +++ b/codeforces/903/io/a.in @@ -0,0 +1,37 @@ +12 +1 5 +a +aaaaa +5 5 +eforc +force +2 5 +ab +ababa +3 5 +aba +ababa +4 3 +babb +bbb +5 1 +aaaaa +a +4 2 +aabb +ba +2 8 +bk +kbkbkbkb +12 2 +fjdgmujlcont +tf +2 2 +aa +aa +3 5 +abb +babba +1 19 +m +mmmmmmmmmmmmmmmmmmm diff --git a/codeforces/903/io/a.out b/codeforces/903/io/a.out new file mode 100644 index 0000000..cc0d584 --- /dev/null +++ b/codeforces/903/io/a.out @@ -0,0 +1,15 @@ +3 +1 +2 +-1 +1 +0 +1 +3 +1 +0 +2 +5 + +[code]: 0 +[time]: 4.27103 ms \ No newline at end of file diff --git a/codeforces/903/io/b.in b/codeforces/903/io/b.in new file mode 100644 index 0000000..ee9cd9b --- /dev/null +++ b/codeforces/903/io/b.in @@ -0,0 +1,16 @@ +15 +1 3 2 +5 5 5 +6 36 12 +7 8 7 +6 3 3 +4 4 12 +12 6 8 +1000000000 1000000000 1000000000 +3 7 1 +9 9 1 +9 3 6 +2 8 2 +5 3 10 +8 4 8 +2 8 4 diff --git a/codeforces/903/io/b.out b/codeforces/903/io/b.out new file mode 100644 index 0000000..ca42e09 --- /dev/null +++ b/codeforces/903/io/b.out @@ -0,0 +1,18 @@ +YES +YES +NO +NO +YES +YES +NO +YES +NO +NO +YES +YES +NO +YES +NO + +[code]: 0 +[time]: 11.1175 ms \ No newline at end of file diff --git a/codeforces/903/io/c.in b/codeforces/903/io/c.in new file mode 100644 index 0000000..391fc7c --- /dev/null +++ b/codeforces/903/io/c.in @@ -0,0 +1,26 @@ +5 +4 +abba +bcbb +bccb +abba +2 +ab +ba +6 +codefo +rcesco +deforc +escode +forces +codefo +4 +baaa +abba +baba +baab +4 +bbaa +abba +aaba +abba diff --git a/codeforces/903/io/c.out b/codeforces/903/io/c.out new file mode 100644 index 0000000..9a4de67 --- /dev/null +++ b/codeforces/903/io/c.out @@ -0,0 +1,8 @@ +1 +2 +181 +5 +9 + +[code]: 0 +[time]: 4.35901 ms \ No newline at end of file diff --git a/codeforces/903/io/d.in b/codeforces/903/io/d.in new file mode 100644 index 0000000..e190d87 --- /dev/null +++ b/codeforces/903/io/d.in @@ -0,0 +1,15 @@ +7 +5 +100 2 50 10 1 +3 +1 1 1 +4 +8 2 4 2 +4 +30 50 27 20 +2 +75 40 +2 +4 4 +3 +2 3 1 diff --git a/codeforces/903/io/d.out b/codeforces/903/io/d.out new file mode 100644 index 0000000..2c5b7bc --- /dev/null +++ b/codeforces/903/io/d.out @@ -0,0 +1,10 @@ +YES +YES +NO +YES +NO +YES +NO + +[code]: 0 +[time]: 11.4608 ms \ No newline at end of file diff --git a/codeforces/903/io/e.in b/codeforces/903/io/e.in new file mode 100644 index 0000000..62f96f1 --- /dev/null +++ b/codeforces/903/io/e.in @@ -0,0 +1,15 @@ +7 +7 +3 3 4 5 2 6 1 +4 +5 6 3 2 +6 +3 4 1 6 7 7 +3 +1 4 3 +5 +1 2 3 4 5 +5 +1 2 3 1 2 +5 +4 5 5 1 5 diff --git a/codeforces/903/io/e.out b/codeforces/903/io/e.out new file mode 100644 index 0000000..237535e --- /dev/null +++ b/codeforces/903/io/e.out @@ -0,0 +1,10 @@ +0 +4 +1 +1 +2 +1 +0 + +[code]: 0 +[time]: 4.28152 ms \ No newline at end of file diff --git a/codeforces/903/io/f.in b/codeforces/903/io/f.in new file mode 100644 index 0000000..71a15da --- /dev/null +++ b/codeforces/903/io/f.in @@ -0,0 +1,49 @@ +6 +7 3 +2 6 7 +1 2 +1 3 +2 4 +2 5 +3 6 +3 7 +4 4 +1 2 3 4 +1 2 +2 3 +3 4 +5 1 +1 +1 2 +1 3 +1 4 +1 5 +5 2 +4 5 +1 2 +2 3 +1 4 +4 5 +10 8 +1 2 3 4 5 8 9 10 +2 10 +10 5 +5 3 +3 1 +1 7 +7 4 +4 9 +8 9 +6 1 +10 9 +1 2 4 5 6 7 8 9 10 +1 3 +3 9 +9 4 +4 10 +10 6 +6 7 +7 2 +2 5 +5 8 + diff --git a/codeforces/903/io/f.out b/codeforces/903/io/f.out new file mode 100644 index 0000000..1752dbb --- /dev/null +++ b/codeforces/903/io/f.out @@ -0,0 +1,9 @@ +2 +2 +0 +1 +4 +5 + +[code]: 0 +[time]: 4.84347 ms \ No newline at end of file diff --git a/codeforces/903/makefile b/codeforces/903/makefile new file mode 100644 index 0000000..3c50267 --- /dev/null +++ b/codeforces/903/makefile @@ -0,0 +1,30 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/codeforces/903/scripts/debug.sh b/codeforces/903/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/903/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/903/scripts/run.sh b/codeforces/903/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/903/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/903/scripts/utils.sh b/codeforces/903/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/903/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/981/.clang-format b/codeforces/981/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/981/.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/981/.clangd b/codeforces/981/.clangd new file mode 100644 index 0000000..cde24c0 --- /dev/null +++ b/codeforces/981/.clangd @@ -0,0 +1,40 @@ +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 + -Wno-unknown-pragmas + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 + -std=c++23 diff --git a/codeforces/981/a.cc b/codeforces/981/a.cc new file mode 100644 index 0000000..9775a8a --- /dev/null +++ b/codeforces/981/a.cc @@ -0,0 +1,102 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + i32 n; + cin >> n; + + bool s = true; + i32 pos = 0; + + i32 i = 1; + for (;;) { + i32 moves = 2 * i - 1; + if (s) { + moves *= -1; + } + pos += moves; + if (abs(pos) > n) + break; + ++i; + s = !s; + } + + if (s) { + cout << "Sakurako"; + } else { + cout << "Kosuke"; + } + + cout << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/981/b.cc b/codeforces/981/b.cc new file mode 100644 index 0000000..a65a909 --- /dev/null +++ b/codeforces/981/b.cc @@ -0,0 +1,99 @@ +#include // {{{ + +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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; + + vector> grid(n, vector(n)); + for (auto& r : grid) + for (auto& r : r) + cin >> r; + + unordered_map m; + + for (u32 i = 0; i < n; ++i) { + for (u32 j = 0; j < n; ++j) { + m[i - j] = min(grid[i][j], m[i - j]); + } + } + + u32 ans = 0; + for (auto& [_, x] : m) + ans -= x; + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/981/c.cc b/codeforces/981/c.cc new file mode 100644 index 0000000..202d6e6 --- /dev/null +++ b/codeforces/981/c.cc @@ -0,0 +1,93 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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; + vector a(n); + for (auto& e : a) + cin >> e; + + for (i32 i = n / 2 - 2; i >= 0; --i) { + if (a[i] == a[i + 1] || a[n - i - 1] == a[n - i - 2]) { + swap(a[i], a[n - i - 1]); + } + } + + u32 ans = 0; + for (u32 i = 0; i + 1 < n; ++i) + ans += a[i] == a[i + 1]; + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/981/compile_flags.txt b/codeforces/981/compile_flags.txt new file mode 100644 index 0000000..5373b01 --- /dev/null +++ b/codeforces/981/compile_flags.txt @@ -0,0 +1,30 @@ +-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 +-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/981/d.cc b/codeforces/981/d.cc new file mode 100644 index 0000000..a229492 --- /dev/null +++ b/codeforces/981/d.cc @@ -0,0 +1,98 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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; + vector a(n); + for (auto& e : a) + cin >> e; + + set prefixes; + prefixes.insert(0); + i64 prefix = 0; + u32 ans = 0; + + for (u32 i = 0; i < n; ++i) { + prefix += a[i]; + if (prefixes.contains(prefix)) { + ++ans; + prefixes.clear(); + prefix = 0; + } + prefixes.insert(prefix); + } + + cout << ans << endl; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/981/debug_flags.txt b/codeforces/981/debug_flags.txt new file mode 100644 index 0000000..62a0135 --- /dev/null +++ b/codeforces/981/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++23 diff --git a/codeforces/981/e.cc b/codeforces/981/e.cc new file mode 100644 index 0000000..068a709 --- /dev/null +++ b/codeforces/981/e.cc @@ -0,0 +1,105 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +bitset<1000001> seen; + +void solve() { + u32 n; + cin >> n; + vector a(n + 1); + for (u32 i = 1; i <= n; ++i) { + cin >> a[i]; + } + + seen.reset(); + + u32 ans = 0; + + for (u32 i = 1; i <= n; ++i) { + if (seen[i]) + continue; + + u32 pos = i; + u32 streak = 0; + while (!seen[pos]) { + ++streak; + seen[pos] = true; + pos = a[pos]; + } + + ans += (streak - 1) / 2; + } + + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/981/io/a.in b/codeforces/981/io/a.in new file mode 100644 index 0000000..1bd8d3b --- /dev/null +++ b/codeforces/981/io/a.in @@ -0,0 +1,5 @@ +4 +1 +6 +3 +98 diff --git a/codeforces/981/io/a.out b/codeforces/981/io/a.out new file mode 100644 index 0000000..9a1fe9e --- /dev/null +++ b/codeforces/981/io/a.out @@ -0,0 +1,7 @@ +Kosuke +Sakurako +Kosuke +Sakurako + +[code]: 0 +[time]: 4.4868 ms \ No newline at end of file diff --git a/codeforces/981/io/b.in b/codeforces/981/io/b.in new file mode 100644 index 0000000..2db36ad --- /dev/null +++ b/codeforces/981/io/b.in @@ -0,0 +1,17 @@ +4 +1 +1 +2 +-1 2 +3 0 +3 +1 2 3 +-2 1 -1 +0 0 -1 +5 +1 1 -1 -1 3 +-3 1 4 4 -4 +-1 -1 3 0 -5 +4 5 3 -3 -1 +3 1 -3 -1 5 + diff --git a/codeforces/981/io/b.out b/codeforces/981/io/b.out new file mode 100644 index 0000000..9c87b7f --- /dev/null +++ b/codeforces/981/io/b.out @@ -0,0 +1,7 @@ +0 +1 +4 +19 + +[code]: 0 +[time]: 4.14872 ms \ No newline at end of file diff --git a/codeforces/981/io/c.in b/codeforces/981/io/c.in new file mode 100644 index 0000000..b0a3152 --- /dev/null +++ b/codeforces/981/io/c.in @@ -0,0 +1,19 @@ +9 +5 +1 1 1 2 3 +6 +2 1 2 2 1 1 +4 +1 2 1 1 +6 +2 1 1 2 2 4 +4 +2 1 2 3 +6 +1 2 2 1 2 1 +5 +4 5 5 1 5 +7 +1 4 3 5 1 1 3 +7 +3 1 3 2 2 3 3 diff --git a/codeforces/981/io/c.out b/codeforces/981/io/c.out new file mode 100644 index 0000000..712131a --- /dev/null +++ b/codeforces/981/io/c.out @@ -0,0 +1,12 @@ +1 +2 +1 +0 +0 +1 +1 +0 +2 + +[code]: 0 +[time]: 3.97801 ms \ No newline at end of file diff --git a/codeforces/981/io/d.in b/codeforces/981/io/d.in new file mode 100644 index 0000000..ed6aeb6 --- /dev/null +++ b/codeforces/981/io/d.in @@ -0,0 +1,7 @@ +3 +5 +2 1 -3 2 1 +7 +12 -4 4 43 -3 -5 8 +6 +0 -4 0 3 0 1 diff --git a/codeforces/981/io/d.out b/codeforces/981/io/d.out new file mode 100644 index 0000000..4266907 --- /dev/null +++ b/codeforces/981/io/d.out @@ -0,0 +1,6 @@ +1 +2 +3 + +[code]: 0 +[time]: 3.10135 ms \ No newline at end of file diff --git a/codeforces/981/io/e.in b/codeforces/981/io/e.in new file mode 100644 index 0000000..8947dff --- /dev/null +++ b/codeforces/981/io/e.in @@ -0,0 +1,13 @@ +6 +5 +1 2 3 4 5 +5 +5 4 3 2 1 +5 +2 3 4 5 1 +4 +2 3 4 1 +3 +1 3 2 +7 +2 3 1 5 6 7 4 diff --git a/codeforces/981/io/e.out b/codeforces/981/io/e.out new file mode 100644 index 0000000..41cffae --- /dev/null +++ b/codeforces/981/io/e.out @@ -0,0 +1,9 @@ +0 +0 +2 +1 +0 +2 + +[code]: 0 +[time]: 4.17638 ms \ No newline at end of file diff --git a/codeforces/981/io/g.in b/codeforces/981/io/g.in new file mode 100644 index 0000000..69840f0 --- /dev/null +++ b/codeforces/981/io/g.in @@ -0,0 +1,37 @@ +3 +5 +1 2 +2 3 +3 4 +3 5 +3 +5 1 +3 1 +2 0 +9 +8 1 +1 7 +1 4 +7 3 +4 9 +3 2 +1 5 +3 6 +7 +6 0 +2 3 +6 2 +8 2 +2 4 +9 2 +6 3 +6 +2 1 +2 5 +2 4 +5 6 +4 3 +3 +3 1 +1 3 +6 5 diff --git a/codeforces/981/io/g.out b/codeforces/981/io/g.out new file mode 100644 index 0000000..85ab0f7 --- /dev/null +++ b/codeforces/981/io/g.out @@ -0,0 +1,6 @@ +2 3 2 +0 6 4 4 6 5 6 +2 3 6 + +[code]: 0 +[time]: 4.73642 ms \ No newline at end of file diff --git a/codeforces/981/makefile b/codeforces/981/makefile new file mode 100644 index 0000000..3c50267 --- /dev/null +++ b/codeforces/981/makefile @@ -0,0 +1,30 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 .clang-format || cp $(HOME)/.config/cp-template/.clang-format . + test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/codeforces/981/scripts/debug.sh b/codeforces/981/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/981/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/981/scripts/run.sh b/codeforces/981/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/981/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/981/scripts/utils.sh b/codeforces/981/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/981/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/993/.clangd b/codeforces/993/.clangd new file mode 100644 index 0000000..d02250b --- /dev/null +++ b/codeforces/993/.clangd @@ -0,0 +1,36 @@ +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 + -Wno-unknown-pragmas + -std=c++23 + -std=c++23 + -std=c++23 diff --git a/codeforces/993/compile_flags.txt b/codeforces/993/compile_flags.txt index b5d4b68..5373b01 100644 --- a/codeforces/993/compile_flags.txt +++ b/codeforces/993/compile_flags.txt @@ -1,5 +1,30 @@ --std=c++20 +-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 +-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/993/debug_flags.txt b/codeforces/993/debug_flags.txt new file mode 100644 index 0000000..af1b521 --- /dev/null +++ b/codeforces/993/debug_flags.txt @@ -0,0 +1,14 @@ +-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 +-DLOCAL +-std=c++20 diff --git a/codeforces/993/h.cc b/codeforces/993/h.cc deleted file mode 100644 index 59b83c0..0000000 --- a/codeforces/993/h.cc +++ /dev/null @@ -1,137 +0,0 @@ -#include - -// https://codeforces.com/blog/entry/96344 - -#pragma GCC optimize("O2,unroll-loops") -#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") - -using namespace std; - -template -void dbg(std::string const &str, Args &&...args) { - std::cout << std::vformat(str, - // make_format_args binds arguments to const - std::make_format_args(args...)); -} - -template -void dbg(T const &t) { - std::cout << t; -} - -template -void dbgln(T const &t) { - if constexpr (std::is_convertible_v) { - std::cout << t << '\n'; - } else { - for (auto const &e : t) { - std::cout << e << ' '; - } - std::cout << '\n'; - } -} - -template -void dbgln(std::string const &str, Args &&...args) { - dbg(str, std::forward(args)...); - cout << '\n'; -} - -template -void dbgln(T const &t) { - dbg("{}\n", t); -} - -void println() { - std::cout << '\n'; -} - -template -constexpr auto MIN = std::numeric_limits::min(); - -template -constexpr auto MAX = std::numeric_limits::min(); - -#define ff first -#define ss second -#define eb emplace_back -#define ll long long -#define ld long double -#define vec vector - -#define all(x) (x).begin(), (x).end() -#define rall(x) (r).rbegin(), (x).rend() -#define sz(x) static_cast((x).size()) -#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a)) -#define ROF(a, b, c) for (int(a) = (b); (a) > (c); --(a)) - -std::random_device rd; -std::mt19937 gen(rd()); - -void YES() { - cout << "YES\n"; -} - -void NO() { - cout << "NO\n"; -} - -void solve() { - int n, q; - cin >> n; - vec> grid(n, vec(n)); - FOR(i, 0, n) { - FOR(j, 0, n) { - cin >> grid[i][j]; - } - } - - vec> dp(n + 1, vec(n + 1, 1)); - - while (q--) { - int x1, x2, y1, y2; - cin >> x1 >> x2 >> y1 >> y2; - } - - /* - - 00 01 02 03 04 05 ... - 10 11 12 13 14 15 ... - - 1 * 00 * 2 * 01 + 3 * 02 - 4 * 01 + 5 * - - 1 * M[i][j] + 2 * M[i][j + 1] + 3 * M[i][j + 2] - + 4 * M[i + 1][j] + 5 * M[i + 1][j + 2] - - = sum r0 * n + c0 -> r1 * n + c1 - - 1 2 - 3 4 - 5 6 - - 1 2 0 0 - 1 2 + 2 2 - 1 2 + 4 4 - - 3+4^2+8^3+9^4+13^5+14^6 - - 8+9^2+13^3+14^4 - ( 8^2+9^2+13^2 ) - */ - // FIX: cout - - dbgln("hi"); -} - -int main() { - cin.tie(nullptr)->sync_with_stdio(false); - - int t = 1; - cin >> t; - - while (t--) { - solve(); - } - - return 0; -} diff --git a/codeforces/993/h.cpp b/codeforces/993/h.cpp new file mode 100644 index 0000000..b46c0bb --- /dev/null +++ b/codeforces/993/h.cpp @@ -0,0 +1,127 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +static void NO() { + std::cout << "NO\n"; +} + +static void YES() { + std::cout << "YES\n"; +} + +template +using vec = std::vector; + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define ff first +#define ss second + +#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> grid(n, vec(n)); + + for (auto& r : grid) + for (auto& c : r) + cin >> c; + + vec> one(n + 1, vec(n + 1, 0)); + auto colwise = one; + auto rowwise = one; + + for (u32 i = 1; i <= n; ++i) { + for (u32 j = 1; j <= n; ++j) { + one[i][j] = grid[i - 1][j - 1] + one[i - 1][j] + one[i][j - 1] - + one[i - 1][j - 1]; + } + } + + for (u32 i = 1; i <= n; ++i) { + for (u32 j = 1; j <= n; ++j) { + colwise[i][j] = i * grid[i - 1][j - 1] + colwise[i][j - 1] + + colwise[i - 1][j] - colwise[i - 1][j - 1]; + } + } + + for (u32 i = 1; i <= n; ++i) { + for (u32 j = 1; j <= n; ++j) { + rowwise[i][j] = j * grid[i - 1][j - 1] + rowwise[i][j - 1] + + rowwise[i - 1][j] - rowwise[i - 1][j - 1]; + } + } + + u64 x1, y1, x2, y2; + for (u32 i = 0; i < q; ++i) { + cin >> x1 >> y1 >> x2 >> y2; + + auto s = + one[x2][y2] - one[x2][y1 - 1] - one[x1 - 1][y2] + one[x1 - 1][y1 - 1]; + auto sr = colwise[x2][y2] - colwise[x2][y1 - 1] - colwise[x1 - 1][y2] + + colwise[x1 - 1][y1 - 1]; + auto sc = rowwise[x2][y2] - rowwise[x2][y1 - 1] - rowwise[x1 - 1][y2] + + rowwise[x1 - 1][y1 - 1]; + + u64 w = y2 - y1 + 1; + + u64 ans = w * (sr - (u64)x1 * s) + (sc - (u64)y1 * s) + s; + + cout << ans << " \n"[i == q - 1]; + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + + u32 tc = 1; + cin >> tc; + + for (u32 t = 0; t < tc; ++t) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/993/io/h.in b/codeforces/993/io/h.in new file mode 100644 index 0000000..f4f17a1 --- /dev/null +++ b/codeforces/993/io/h.in @@ -0,0 +1,16 @@ +2 +4 3 +1 5 2 4 +4 9 5 3 +4 5 2 3 +1 5 5 2 +1 1 4 4 +2 2 3 3 +1 2 4 3 +3 3 +1 2 3 +4 5 6 +7 8 9 +1 1 1 3 +1 3 3 3 +2 2 2 2 diff --git a/codeforces/993/io/h.out b/codeforces/993/io/h.out new file mode 100644 index 0000000..928561e --- /dev/null +++ b/codeforces/993/io/h.out @@ -0,0 +1,5 @@ +500 42 168 +14 42 5 + +[code]: 0 +[time]: 4.26412 ms diff --git a/codeforces/993/makefile b/codeforces/993/makefile new file mode 100644 index 0000000..78778b6 --- /dev/null +++ b/codeforces/993/makefile @@ -0,0 +1,29 @@ +.PHONY: run debug clean setup init + +VERSION ?= 20 + +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 . && echo -std=c++$(VERSION) >>compile_flags.txt + test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd + +init: + make setup + +%: + @: diff --git a/codeforces/993/scripts/debug.sh b/codeforces/993/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/codeforces/993/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/993/scripts/run.sh b/codeforces/993/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/codeforces/993/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/993/scripts/utils.sh b/codeforces/993/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/codeforces/993/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/usaco/bronze/complete-search/.clangd b/usaco/bronze/complete-search/.clangd index 0f141ad..8352aa9 100644 --- a/usaco/bronze/complete-search/.clangd +++ b/usaco/bronze/complete-search/.clangd @@ -35,3 +35,4 @@ CompileFlags: -std=c++17 -std=c++17 -std=c++17 + -std=c++17 diff --git a/usaco/bronze/complete-search/angry.cc b/usaco/bronze/complete-search/angry.cc new file mode 100644 index 0000000..e63778d --- /dev/null +++ b/usaco/bronze/complete-search/angry.cc @@ -0,0 +1,79 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "angry" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}} diff --git a/usaco/bronze/complete-search/io/angry.in b/usaco/bronze/complete-search/io/angry.in new file mode 100644 index 0000000..e69de29 diff --git a/usaco/bronze/complete-search/io/angry.out b/usaco/bronze/complete-search/io/angry.out new file mode 100644 index 0000000..e69de29 diff --git a/usaco/bronze/scripts/debug.sh b/usaco/bronze/scripts/debug.sh new file mode 100644 index 0000000..2979422 --- /dev/null +++ b/usaco/bronze/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/usaco/bronze/scripts/run.sh b/usaco/bronze/scripts/run.sh new file mode 100644 index 0000000..ab9aa7d --- /dev/null +++ b/usaco/bronze/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/usaco/bronze/scripts/utils.sh b/usaco/bronze/scripts/utils.sh new file mode 100644 index 0000000..e99b25b --- /dev/null +++ b/usaco/bronze/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/usaco/bronze/sorting/angry.cc b/usaco/bronze/sorting/angry.cc new file mode 100644 index 0000000..c8f50e3 --- /dev/null +++ b/usaco/bronze/sorting/angry.cc @@ -0,0 +1,113 @@ +#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; + +#if __cplusplus >= 202002L +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()); +} +#endif + +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() +#define ff first +#define ss second + +#ifdef LOCAL +#define db(...) std::print(__VA_ARGS__) +#define dbln(...) std::println(__VA_ARGS__) +#else +#define db(...) +#define dbln(...) +#endif +// }}} + +void solve() { + i32 n; + cin >> n; + vector a(n); + for (auto &e : a) + cin >> e; + sort(all(a)); + + i32 ans = 1; + for (u32 i = 0; i < n; ++i) { + i32 l = i; + i32 radius = 1; + + while (l >= 0) { + i32 j = l; + while (j >= 0 && a[j] >= a[l] - radius) { + --j; + } + ++j; + if (j == l) { + break; + } + l = j; + ++radius; + } + + i32 r = i; + radius = 1; + while (r < n) { + i32 j = r; + while (j < n && a[j] <= a[r] + radius) { + ++j; + } + --j; + if (j == r) { + break; + } + ++radius; + r = j; + } + + ans = max(ans, r - l + 1); + } + cout << ans << '\n'; +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + cin.exceptions(cin.failbit); + +#define PROBLEM_NAME "angry" + +#ifdef LOCAL + freopen("io/" PROBLEM_NAME ".in", "r", stdin); + freopen("io/" PROBLEM_NAME ".out", "w", stdout); +#else + freopen(PROBLEM_NAME ".in", "r", stdin); + freopen(PROBLEM_NAME ".out", "w", stdout); +#endif + + solve(); + + return 0; +} +// }}}