From acabee731fe5c966386017e0067bdfe85cbdf766 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Sat, 21 Feb 2026 14:00:11 -0300 Subject: [PATCH 1/2] More stuff --- .../C. Needle in a Haystack.cpp | 148 ++++++++++++ .../A. Perfect Root.cpp | 110 +++++++++ .../A. Sieve of Erato67henes.cpp | 112 +++++++++ .../B. Heapify 1.cpp | 152 ++++++++++++ .../C. Dice Roll Sequence.cpp | 128 +++++++++++ .../D. Absolute Cinema.cpp | 123 ++++++++++ .../E. Permutation Game.cpp | 153 ++++++++++++ .../D. Counting Factorizations.cpp | 205 +++++++++++++++++ .../E. Data Structures Fan.cpp | 217 ++++++++++++++++++ .../C. Array Game.cpp | 156 +++++++++++++ .../D. Lonely Mountain Dungeons.cpp | 136 +++++++++++ .../E. Final Countdown.cpp | 132 +++++++++++ .../D. Ingenuity-2.cpp | 185 +++++++++++++++ ...e the Largest Component (Easy Version).cpp | 212 +++++++++++++++++ .../B. Kar Salesman.cpp | 121 ++++++++++ TODO.md | 10 + problemlist.md | 6 + 17 files changed, 2306 insertions(+) create mode 100644 Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp create mode 100644 Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp create mode 100644 Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp create mode 100644 Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp create mode 100644 Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp create mode 100644 Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp create mode 100644 Codeforces Round 839 (Div. 3)/E. Permutation Game.cpp create mode 100644 Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp create mode 100644 Codeforces Round 895 (Div. 3)/E. Data Structures Fan.cpp create mode 100644 Codeforces Round 914 (Div. 2)/C. Array Game.cpp create mode 100644 Codeforces Round 924 (Div. 2)/D. Lonely Mountain Dungeons.cpp create mode 100644 Codeforces Round 927 (Div. 3)/E. Final Countdown.cpp create mode 100644 Codeforces Round 946 (Div. 3)/D. Ingenuity-2.cpp create mode 100644 Codeforces Round 952 (Div. 4)/H1. Maximize the Largest Component (Easy Version).cpp create mode 100644 Codeforces Round 978 (Div. 2)/B. Kar Salesman.cpp diff --git a/Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp b/Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp new file mode 100644 index 0000000..4a27640 --- /dev/null +++ b/Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/contest/2175/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + string a, b; + cin >> a >> b; + + vi count(26); + vi count2(26); + + repv(i, a) { + count2[i - 'a']++; + } + + repv(i, b) { + count[i - 'a']++; + } + + rep(i, 26) { + if (count[i] < count2[i]) { + cout << "Impossible\n"; + return; + } + count[i] -= count2[i]; + } + + int cur = 0; + string ans; + + rep(i, 26) { + while (cur < a.size() && a[cur] <= i + 'a') { + ans.push_back(a[cur]); + cur++; + } + + while (count[i]) { + count[i]--; + ans.push_back(i + 'a'); + } + } + + while (cur < a.size()) { + ans.push_back(a[cur]); + cur++; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp b/Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp new file mode 100644 index 0000000..97bf0ac --- /dev/null +++ b/Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/contest/2185/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + nrep(i, 1, n + 1) { + cout << i * i << " \n"[i == n]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp b/Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp new file mode 100644 index 0000000..2bf1363 --- /dev/null +++ b/Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/2195/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + cin >> a; + + cout << (count(all(a), 67) ? "YES\n" : "NO\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp b/Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp new file mode 100644 index 0000000..38fe007 --- /dev/null +++ b/Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/contest/2195/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vvi s; + vi st; + V vis(n); + + rep(i, n) { + if (vis[i]) { + continue; + } + + s.emplace_back(); + st.push_back(i); + for (int j = i; j < n; j = (j + 1) * 2 - 1) { + vis[j] = true; + s.back().push_back(j + 1); + } + } + + repv(i, s) { + sort(all(i), [&](int i, int j){ + return a[i - 1] < a[j - 1]; + }); + } + + vi ans(n); + + rep(i, s.size()) { + int start = st[i]; + auto &v = s[i]; + + for (int j = 0; j < v.size(); j++, start = (start + 1) * 2 - 1) { + ans[start] = a[v[j] - 1]; + } + } + + nrep(i, 1, n) { + if (ans[i] < ans[i - 1]) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp b/Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp new file mode 100644 index 0000000..3864267 --- /dev/null +++ b/Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/2195/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vvl dp(n + 1, vl(6, OO)); + fill(all(dp[0]), 0); + + nrep(i, 1, n + 1) { + int cur = a[i - 1] - 1; + + rep(j, 6) { + int add = cur != j; + rep(k, 6) { + if (k == j || k == 6 - j - 1) { + continue; + } + rmin(dp[i][j], add + dp[i - 1][k]); + } + } + } + + cout << *min_element(all(dp.back())) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp b/Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp new file mode 100644 index 0000000..0e2ab1f --- /dev/null +++ b/Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2195/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + ll sum = a[0] + a.back(); + for (int i = n - 1; i > 0; i--) { + a[i] = a[i - 1] - a[i]; + } + a[0] = sum / (n - 1); + + vl ans(n); + ans.back() = (a[0] + a.back()) / 2; + rep(i, n - 1) { + ans[i] = (a[i] - a[i + 1]) / 2; + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 839 (Div. 3)/E. Permutation Game.cpp b/Codeforces Round 839 (Div. 3)/E. Permutation Game.cpp new file mode 100644 index 0000000..d49944f --- /dev/null +++ b/Codeforces Round 839 (Div. 3)/E. Permutation Game.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1772/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vi p1(n); + vi p2(n); + + rep(i, n) { + p1[i] = (a[i] != (i + 1)); + p2[i] = (a[i] != (n - i)); + } + + int c[2] = {0, 0}; + int d = 0; + rep(i, n) { + if (p1[i] && p1[i] == p2[i]) { + d++; + continue; + } + + c[0] += p1[i]; + c[1] += p2[i]; + } + + int cur = 0; + while (1) { + if (c[cur]) { + c[cur]--; + cur ^= 1; + continue; + } + + if (d) { + if (d == 1 && c[cur^1] == 0) { + cout << "Tie\n"; + return; + } + d--; + cur ^= 1; + continue; + } + + break; + } + + string win[] = {"First", "Second"}; + cout << win[cur] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp b/Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp new file mode 100644 index 0000000..699a799 --- /dev/null +++ b/Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp @@ -0,0 +1,205 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1794/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const ll mod = 998244353; + +constexpr MAXN = 1e6 + 10; +bool prime[MAXN]; +bool vis[MAXN]; + +vl fact[MAXN]; +vl inv[MAXN]; + + +void pre() +{ + fill(prime, prime + MAXN, true); + prime[0] = false; + prime[1] = false; + + nrep(i, 2, MAXN) { + if (!prime[i]) { + continue; + } + + for (int j = i * 2; j < MAXN; j += i) { + prime[j] = false; + } + } + + fact[0] = 1; + fact[1] = 1; + nrep(i, 2, MAXN) { + fact[i] = fact[i - 1] * i % mod; + } + + inv[0] = 1; + inv[1] = 1; + nrep(i, 2, MAXN) { + inv[i] = (mod - mod / i) * inv[mod % i] % mod; + } + + nrep(i, 2, MAXN) { + inv[i] = inv[i - 1] * inv[i] % mod; + } +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi act(n << 1); + cin >> act; + + set up; + + vi ot; + vi primes; + + rep(i, n << 1) { + if (prime[act[i]]) { + if (vis[act[i]]) { + ot.push_back(act[i]); + } else { + up.insert(act[i]); + vis[act[i]] = true; + primes.push_back(act[i]); + } + continue; + } + + ot.push_back(act[i]); + } + + if (primes.size() < n) { + cout << "0\n"; + return; + } + + auto comb = [&](int n, int k) { + return fact[n] * inv[k] % mod * inv[n - k] % mod; + }; + + ll ans = comb(primes.size(), n); + + if (ot.empty()) { + cout << ans << '\n'; + return; + } + + sortv(ot); + + int c = 1; + vi tmp; + + nrep(i, 1, ot.size()) { + if (ot[i] == ot[i - 1]) { + c++; + continue; + } + + tmp.push_back(c); + c = 1; + } + tmp.push_back(c); + + + + repv(i, up) { + vis[i] = false; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 895 (Div. 3)/E. Data Structures Fan.cpp b/Codeforces Round 895 (Div. 3)/E. Data Structures Fan.cpp new file mode 100644 index 0000000..927e07d --- /dev/null +++ b/Codeforces Round 895 (Div. 3)/E. Data Structures Fan.cpp @@ -0,0 +1,217 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1872/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + cin >> a; + + string b; + cin >> b; + + while (__builtin_popcount(n) != 1) { + n++; + a.push_back(0); + b.push_back('0'); + } + + vl seg0(n << 1); + vl seg1(n << 1); + vi lazy(n << 1); + + nrep(i, n, n << 1) { + if (b[i - n] == '0') { + seg0[i] = a[i - n]; + } else { + seg1[i] = a[i - n]; + } + } + + for (int i = n - 1; i > 0; i--) { + seg0[i] = seg0[i * 2] ^ seg0[i * 2 + 1]; + seg1[i] = seg1[i * 2] ^ seg1[i * 2 + 1]; + } + + auto propagate = [&](int i) { + if (!lazy[i]) { + return; + } + + lazy[i] = 0; + + swap(seg0[i], seg1[i]); + + if (i < n) { + lazy[i * 2] ^= 1; + lazy[i * 2 + 1] ^= 1; + } + }; + + function(int, int, int, int, int)> update = [&](int i, int l, int r, int tl, int tr) -> pair { + propagate(i); + + if (l > tr || r < tl) { + return {seg0[i], seg1[i]}; + } + + if (l >= tl && r <= tr) { + lazy[i] ^= 1; + propagate(i); + return {seg0[i], seg1[i]}; + } + + int mid = (l + r) >> 1; + + auto a1 = update(i * 2, l, mid, tl, tr); + auto a2 = update(i * 2 + 1, mid + 1, r, tl, tr); + + seg0[i] = a1.first ^ a2.first; + seg1[i] = a1.second ^ a2.second; + + return {seg0[i], seg1[i]}; + }; + + function(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) -> pair { + if (l > tr || r < tl) { + return {0, 0}; + } + + propagate(i); + + if (l >= tl && r <= tr) { + return {seg0[i], seg1[i]}; + } + + int mid = (l + r) >> 1; + + auto a1 = query(i * 2, l, mid, tl, tr); + auto a2 = query(i * 2 + 1, mid + 1, r, tl, tr); + + return {a1.first ^ a2.first, a1.second ^ a2.second}; + }; + + int q; + cin >> q; + while (q--) { + int op; + cin >> op; + if (op == 1) { + int l, r; + cin >> l >> r; + update(1, 0, n - 1, l - 1, r - 1); + continue; + } + + int g; + cin >> g; + + if (g == 0) { + cout << query(1, 0, n - 1, 0, n - 1).first << ' '; + } else { + cout << query(1, 0, n - 1, 0, n - 1).second << ' '; + } + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 914 (Div. 2)/C. Array Game.cpp b/Codeforces Round 914 (Div. 2)/C. Array Game.cpp new file mode 100644 index 0000000..d35a7ba --- /dev/null +++ b/Codeforces Round 914 (Div. 2)/C. Array Game.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1904/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll k; + cin >> n >> k; + + vl a(n); + cin >> a; + if (k >= 3) { + cout << "0\n"; + return; + } + + sort(all(a), greater<>()); + + ll ans = *min_element(all(a)); + + vl ot; + + rep(i, n - 1) { + nrep(j, i + 1, n) { + rmin(ans, max(a[i] - a[j] * k, a[i] % a[j])); + + if (k > 1) { + ll cur = a[i] - a[j]; + int ind = lower_bound(all(a), cur, greater<>()) - a.begin(); + if (ind < n) { + rmin(ans, abs(a[ind] - cur)); + } + + if (ind < n - 1) { + rmin(ans, abs(a[ind + 1] - cur)); + } + + if (ind < n - 2) { + rmin(ans, abs(a[ind + 2] - cur)); + } + + if (ind > 0) { + rmin(ans, abs(a[ind - 1] - cur)); + } + + if (ind > 1) { + rmin(ans, abs(a[ind - 2] - cur)); + } + + rmin(ans, abs(cur - a[j])); + rmin(ans, abs(cur - a[i])); + } + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 924 (Div. 2)/D. Lonely Mountain Dungeons.cpp b/Codeforces Round 924 (Div. 2)/D. Lonely Mountain Dungeons.cpp new file mode 100644 index 0000000..f7749eb --- /dev/null +++ b/Codeforces Round 924 (Div. 2)/D. Lonely Mountain Dungeons.cpp @@ -0,0 +1,136 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1928/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll b; + ll x; + cin >> n >> b >> x; + + vl a(n); + int m = *max_element(all(a)); + + vi s(n); + vl ac(n); + + vvi t(m); + ll total = -x * (m - 1); + rep(i, n) { + rep(j, a[i]) { + t[j].push_back(i); + } + total += a[i] * (a[i - 1]) / 2 * b; + ac[i] = a[i] * (a[i - 1]) / 2 * b; + } + + ll ans = total; + for (int i = m - 1; i > 0; i--) { + repv(j, t[i]) { + total -= ac[j]; + ll sz = i; + ll cmp = a[i] % sz; + sz -= cmp; + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 927 (Div. 3)/E. Final Countdown.cpp b/Codeforces Round 927 (Div. 3)/E. Final Countdown.cpp new file mode 100644 index 0000000..7154f87 --- /dev/null +++ b/Codeforces Round 927 (Div. 3)/E. Final Countdown.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1932/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string a; + cin >> a; + + reverse(all(a)); + + vi tmp(n + 1); + for (int i = n - 1; i >= 0; i--) { + tmp[i] = tmp[i + 1] + (a[i] - '0'); + } + + string res; + + int c = 0; + rep(i, n) { + c += tmp[i]; + res.push_back(c % 10 + '0'); + c /= 10; + } + + res.push_back(c + '0'); + while (res.back() == '0') { + res.pop_back(); + } + reverse(all(res)); + cout << res << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 946 (Div. 3)/D. Ingenuity-2.cpp b/Codeforces Round 946 (Div. 3)/D. Ingenuity-2.cpp new file mode 100644 index 0000000..32f80e1 --- /dev/null +++ b/Codeforces Round 946 (Div. 3)/D. Ingenuity-2.cpp @@ -0,0 +1,185 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1974/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string a; + cin >> a; + + int x = 0; + int y = 0; + + pair var[256]; + var['N'] = {0, 1}; + var['S'] = {0, -1}; + var['E'] = {1, 0}; + var['W'] = {-1, 0}; + + repv(i, a) { + x += var[i].first; + y += var[i].second; + } + + if ((x & 1) || (y & 1)) { + cout << "NO\n"; + return; + } + + x >>= 1; + y >>= 1; + + if (x == 0 && y == 0) { + if (a.size() == 2) { + cout << "NO\n"; + return; + } + + int ind1 = find(all(a), 'N') - a.begin(); + int ind2 = find(all(a), 'S') - a.begin(); + if (ind1 == n || ind2 == n) { + ind1 = find(all(a), 'E') - a.begin(); + ind2 = find(all(a), 'W') - a.begin(); + } + + rep(i, n) { + if (i == ind1 || i == ind2) { + cout << 'H'; + continue; + } + + cout << "R"; + } + cout << '\n'; + return; + } + + repv(i, a) { + if (i == 'N' && y > 0) { + y--; + cout << "R"; + continue; + } + + if (i == 'S' && y < 0) { + y++; + cout << "R"; + continue; + } + + if (i == 'E' && x > 0) { + x--; + cout << "R"; + continue; + } + + if (i == 'W' && x < 0) { + x++; + cout << "R"; + continue; + } + + cout << "H"; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 952 (Div. 4)/H1. Maximize the Largest Component (Easy Version).cpp b/Codeforces Round 952 (Div. 4)/H1. Maximize the Largest Component (Easy Version).cpp new file mode 100644 index 0000000..ed64b3d --- /dev/null +++ b/Codeforces Round 952 (Div. 4)/H1. Maximize the Largest Component (Easy Version).cpp @@ -0,0 +1,212 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1985/H1 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + V a(n); + cin >> a; + + vi dsu(n * m); + vi size(n * m); + rep(i, n * m) { + dsu[i] = i; + size[i] = 1; + } + + stack> s; + auto save = [&]() { + s.emplace(-1, -1); + }; + + auto roll = [&]() { + while (s.top().first != -1) { + auto [i, j] = s.top(); + size[i] -= size[j]; + dsu[j] = j; + s.pop(); + } + }; + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return find_p(dsu[i]); + }; + + int ans = 1; + + auto join = [&](int i, int j) { + i = find_p(i); + j = find_p(j); + + if (i == j) { + return; + } + + if (size[i] < size[j]) { + swap(i, j); + } + + s.emplace(i, j); + size[i] += size[j]; + rmax(ans, size[i]); + dsu[j] = i; + }; + + rep(i, n) { + rep(j, m) { + if (a[i][j] == '.') { + continue; + } + + if (i > 0 && a[i - 1][j] == '#') { + join(i * m + j, (i - 1) * m + j); + } + + if (j > 0 && a[i][j - 1] == '#') { + join(i * m + j, i * m + j - 1); + } + } + } + + rep(i, n) { + save(); + rep(j, m) { + if (i > 0 && a[i - 1][j] == '#') { + join(i * m + j, (i - 1) * m + j); + } + + if (i < n - 1 && a[i + 1][j] == '#') { + join(i * m + j, (i + 1) * m + j); + } + + if (j > 0) { + join(i * m + j, i * m + j - 1); + } + } + roll(); + } + + rep(j, m) { + save(); + rep(i, n) { + if (i > 0) { + join(i * m + j, (i - 1) * m + j); + } + + if (j > 0 && a[i][j - 1] == '#') { + join(i * m + j, i * m + j - 1); + } + + if (j < m - 1 && a[i][j + 1] == '#') { + join(i * m + j, i * m + j + 1); + } + } + roll(); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 978 (Div. 2)/B. Kar Salesman.cpp b/Codeforces Round 978 (Div. 2)/B. Kar Salesman.cpp new file mode 100644 index 0000000..a95edfd --- /dev/null +++ b/Codeforces Round 978 (Div. 2)/B. Kar Salesman.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2022/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, x; + cin >> n >> x; + + ll ans = 0; + + vl a(n); + cin >> a; + + ll ma = 0; + ll total = 0; + repv(i, a) { + rmax(ma, i); + total += i; + } + + cout << max(ma, (total + x - 1) / x) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/TODO.md b/TODO.md index e9f8774..5c130c0 100644 --- a/TODO.md +++ b/TODO.md @@ -111,6 +111,16 @@ Official divs from codeforces Another problem that's kinda interesting, but I guess my head is not working recently, I'll get back to it once I can finally work again. + - [ ] [D. Counting Factorizations](https://codeforces.com/problemset/problem/1794/D) + + I didn't have enough time and just wanted to do something else when I was doing + this problem, I should get back to it when I have the head for it. + + - [ ] [D. Lonely Mountain Dungeons](https://codeforces.com/problemset/problem/1928/D) + + I got everything I need to do it, but I still need to find the equation to solve + every single case... + - Div 3 - [ ] [G. Cry Me a River](https://codeforces.com/contest/2137/problem/G) diff --git a/problemlist.md b/problemlist.md index 40d3a77..1159135 100644 --- a/problemlist.md +++ b/problemlist.md @@ -112,3 +112,9 @@ A little collection of interesting problems that I randomly decided to do. Not so hard if you know how to deal with C++ or any other language with big integer, but it's interesting to think about regardless, can also be solved with a binary search, but it's not required. + +## DSU + +- [https://codeforces.com/problemset/problem/1985/H1](https://codeforces.com/problemset/problem/1985/H1) + + DSU with rollback problem, cool to think about and not really that difficult. From 3174264022ca518501c8602e92a2317ab0164a74 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Sun, 22 Feb 2026 22:39:00 -0300 Subject: [PATCH 2/2] More stuff --- ... Bessie's Birthday Cake (Hard Version).cpp | 164 ++++++++++++++++++ .../A. DBMB and the Array.cpp | 121 +++++++++++++ .../B. Reverse a Permutation.cpp | 130 ++++++++++++++ .../C. Replace and Sum.cpp | 127 ++++++++++++++ .../D. Monster Game.cpp | 138 +++++++++++++++ .../E. Alternating String.cpp | 157 +++++++++++++++++ .../D. Satyam and Counting.cpp | 135 ++++++++++++++ 7 files changed, 972 insertions(+) create mode 100644 CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp create mode 100644 Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp create mode 100644 Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp create mode 100644 Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp create mode 100644 Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp create mode 100644 Codeforces Round 970 (Div. 3)/E. Alternating String.cpp create mode 100644 Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp diff --git a/CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp b/CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp new file mode 100644 index 0000000..387fe21 --- /dev/null +++ b/CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1942/C2 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, x, y; + cin >> n >> x >> y; + + ll ini = y; + + vi a(x); + cin >> a; + sortv(a); + + ll ans = x - 2; + + ll even = 0; + vl odds; + nrep(i, 1, x) { + if (a[i] - a[i - 1] - 1 <= 1) { + ans += a[i] - a[i - 1] - 1; + continue; + } + + ll g = a[i] - a[i - 1] - 1; + if (g & 1) { + odds.push_back(g >> 1); + continue; + } + + even += g >> 1; + } + + ll g = n - (a.back() - a[0] + 1); + + if (g <= 1) { + ans += g; + } else if (g & 1) { + odds.push_back(g >> 1); + } else { + even += g >> 1; + } + + + sortv(odds); + + repv(i, odds) { + if (y >= i) { + y -= i; + ans += i + 1; + continue; + } + + ans += y; + y = 0; + break; + } + + ll ev = min(even, (ll)y); + y -= ev; + ans += ev; + ll us = ini - y; + ans += us; + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp b/Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp new file mode 100644 index 0000000..2e8b275 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2193/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, s, x; + cin >> n >> s >> x; + + int cur = 0; + while (n--) { + int x; + cin >> x; + cur += x; + } + + if (cur > s || (cur - s) % x) { + cout << "NO\n"; + return; + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp b/Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp new file mode 100644 index 0000000..1e5ba96 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/2193/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi p(n); + cin >> p; + int cur = n; + int inv = n; + rep(i, n) { + if (p[i] == cur) { + cur--; + continue; + } + + inv = i; + break; + } + + int l = inv; + while (l < n && p[l] != cur) { + l++; + } + + reverse(p.begin() + inv, p.begin() + l + 1); + + cout << p; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp b/Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp new file mode 100644 index 0000000..2964513 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2193/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + vl a(n); + vl b(n); + cin >> a >> b; + rmax(a[n - 1], b[n - 1]); + + for (int i = n - 2; i >= 0; i--) { + a[i] = max({a[i], a[i + 1], b[i]}); + } + + vl pref(n + 1); + nrep(i, 1, n + 1) { + pref[i] = pref[i - 1] + a[i - 1]; + } + + while (q--) { + int l, r; + cin >> l >> r; + cout << pref[r] - pref[l - 1] << ' '; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp b/Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp new file mode 100644 index 0000000..d049410 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/contest/2193/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + vi b(n); + cin >> a >> b; + + priority_queue s; + repv(i, a) { + s.push(i); + } + + ll best = OO; + + ll ans = 0; + rep(i, n) { + while (b[i]) { + if (s.empty()) { + break; + } + + rmin(best, s.top()); + s.pop(); + b[i]--; + } + + if (b[i]) { + break; + } + + rmax(ans, (i + 1) * best); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 970 (Div. 3)/E. Alternating String.cpp b/Codeforces Round 970 (Div. 3)/E. Alternating String.cpp new file mode 100644 index 0000000..d117f0a --- /dev/null +++ b/Codeforces Round 970 (Div. 3)/E. Alternating String.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2008/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + string a; + cin >> n >> a; + + if (a.size() & 1) { + vvvi suf(n + 1, vvi(26, vi(2))); + for (int i = n - 1; i >= 0; i--) { + suf[i][a[i] - 'a'][i & 1]++; + + rep(j, 26) { + suf[i][j][0] += suf[i + 1][j][0]; + suf[i][j][1] += suf[i + 1][j][1]; + } + } + + int c = n >> 1; + + int ans = oo; + + vvi pref(26, vi(2)); + rep(i, n) { + int mi0 = oo; + int mi1 = oo; + rep(j, 26) { + rmin(mi0, c - pref[j][0] - suf[i + 1][j][1]); + rmin(mi1, c - pref[j][1] - suf[i + 1][j][0]); + } + + rmin(ans, mi0 + mi1); + + pref[a[i] - 'a'][i & 1]++; + } + + cout << ans + 1 << '\n'; + return; + } + + vvi c(26, vi(2)); + rep(i, n) { + c[a[i] - 'a'][i & 1]++; + } + + int cc = n >> 1; + + int mi0 = oo; + int mi1 = oo; + rep(j, 26) { + rmin(mi0, cc - c[j][0]); + rmin(mi1, cc - c[j][1]); + } + + cout << mi0 + mi1 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp b/Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp new file mode 100644 index 0000000..6a553d4 --- /dev/null +++ b/Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2009/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + V> p(2, V(n + 1)); + + rep(i, n) { + int x, y; + cin >> x >> y; + p[y][x] = true; + } + + ll ans = 0; + n++; + + rep(i, n) { + if (p[0][i] && p[1][i]) { + ans += n - 3; + } + + if (i > 0 && i < n - 1) { + if (p[0][i] && p[1][i - 1] && p[1][i + 1]) { + ans++; + } + + if (p[1][i] && p[0][i - 1] && p[0][i + 1]) { + ans++; + } + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +}