diff --git a/Codeforces Round 1050 (Div. 4)/D. Destruction of the Dandelion Fields.cpp b/Codeforces Round 1050 (Div. 4)/D. Destruction of the Dandelion Fields.cpp new file mode 100644 index 0000000..e789bc9 --- /dev/null +++ b/Codeforces Round 1050 (Div. 4)/D. Destruction of the Dandelion Fields.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/contest/2148/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl even; + vl odd; + + while (n--) { + ll now; + cin >> now; + + if (now & 1) { + odd.push_back(now); + } else { + even.push_back(now); + } + } + + if (odd.empty()) { + cout << "0\n"; + continue; + } + + sortv(odd); + + ll ans = odd.back() + accumulate(all(even), 0LL); + odd.pop_back(); + + int i = 0; + int j = odd.size() - 1; + + while (i < j) { + ans += odd[j]; + i++; + j--; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1050 (Div. 4)/E. Split.cpp b/Codeforces Round 1050 (Div. 4)/E. Split.cpp new file mode 100644 index 0000000..a0cef31 --- /dev/null +++ b/Codeforces Round 1050 (Div. 4)/E. Split.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/2148/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi fds(n); + cin >> fds; + + vi ncount(n + 1); + repv(i, fds) { + ncount[i]++; + } + + vi lim(n + 1); + + auto check = [&]() { + rep(i, n + 1) { + if (ncount[i] % k != 0) { + return false; + } + lim[i] = ncount[i] / k; + } + return true; + }; + + if (!check()) { + cout << "0\n"; + continue; + } + + vi now(n + 1); + + int l = 0; + int r = 0; + ll ans = 0; + while (r < n) { + now[fds[r]]++; + + while (l < r && now[fds[r]] > lim[fds[r]]) { + now[fds[l]]--; + l++; + } + + ans += r - l + 1; + r++; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1051 (Div. 2)/A. All Lengths Subtraction.cpp b/Codeforces Round 1051 (Div. 2)/A. All Lengths Subtraction.cpp new file mode 100644 index 0000000..0702c8c --- /dev/null +++ b/Codeforces Round 1051 (Div. 2)/A. All Lengths Subtraction.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2143/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int now = n; + + int act = 0; + while (fds[act] != now) { + act++; + } + + int i = act; + int j = act; + + bool pos = true; + + rep(k, n - 1) { + if (i > 0 && fds[i - 1] == now - 1) { + now--; + i--; + continue; + } + + if (j < n - 1 && fds[j + 1] == now - 1) { + now--; + j++; + continue; + } + + pos = false; + break; + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1051 (Div. 2)/B. Discounts.cpp b/Codeforces Round 1051 (Div. 2)/B. Discounts.cpp new file mode 100644 index 0000000..610fc84 --- /dev/null +++ b/Codeforces Round 1051 (Div. 2)/B. Discounts.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/2143/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, k; + cin >> n >> k; + + vl a(n); + cin >> a; + + vl b(k); + cin >> b; + + sort(all(a), greater<>()); + sortv(b); + + ll total = 0; + int i = 0; + int now = 0; + + while (i < n && now < k) { + int size = n - i; + if (b[now] > size) { + while (i < n) { + total += a[i]; + i++; + } + + break; + } + + int lim = i + b[now] - 1; + while (i < lim) { + total += a[i]; + i++; + } + + i++; + now++; + } + + while (i < n) { + total += a[i]; + i++; + } + + cout << total << '\n'; + } +} diff --git a/Codeforces Round 1051 (Div. 2)/C. Max Tree.cpp b/Codeforces Round 1051 (Div. 2)/C. Max Tree.cpp new file mode 100644 index 0000000..28d4801 --- /dev/null +++ b/Codeforces Round 1051 (Div. 2)/C. Max Tree.cpp @@ -0,0 +1,159 @@ +/* Problem URL: https://codeforces.com/contest/2143/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + V>> graph(n); + + rep(i, n - 1) { + int u, v; + ll x, y; + cin >> u >> v >> x >> y; + + if (u > v) { + swap(u, v); + } + + u--, v--; + + graph[u].emplace_back(v, x, y); + graph[v].emplace_back(u, 0, 0); + } + + vvi newgraph(n); + + function dfs = [&](int i, int p) { + for (auto [j, x, y] : graph[i]) { + if (i < j) { + if (x > y) { + newgraph[i].push_back(j); + continue; + } + if (y > x) { + newgraph[j].push_back(i); + } + } + + if (j == p) { + continue; + } + + dfs(j, i); + } + }; + + dfs(0, 0); + + int now = 1; + vi act(n); + + V vis(n); + + function getans = [&](int i) { + vis[i] = true; + + for (auto j : newgraph[i]) { + if (vis[j]) { + continue; + } + getans(j); + } + + act[i] = now; + now++; + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + getans(i); + } + + cout << act; + } +} diff --git a/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp b/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp new file mode 100644 index 0000000..a6509ac --- /dev/null +++ b/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2143/problem/D1 */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + cin >> a; + + ll mod = 1e9 + 7; + + vvvl memo(n, vvl(n + 1, vl(n + 1, -1))); + + function dp = [&](int i, int maximal, int mp) { + if (i >= n) { + return 1LL; + } + + ll &ans = memo[i][maximal][mp]; + if (ans != -1) { + return ans; + } + + ans = dp(i + 1, maximal, mp); + + if (a[i] < maximal && a[i] < mp) { + return ans; + } + + if (a[i] >= maximal) { + ans += dp(i + 1, a[i], mp); + ans %= mod; + } + + if (a[i] < maximal) { + ans += dp(i + 1, maximal, max(mp, a[i])); + ans %= mod; + } + + return ans; + }; + + cout << dp(0, 0, 0) << '\n'; + } +} diff --git a/Codeforces Round 1051 (Div. 2)/E. Make Good.cpp b/Codeforces Round 1051 (Div. 2)/E. Make Good.cpp new file mode 100644 index 0000000..b14086f --- /dev/null +++ b/Codeforces Round 1051 (Div. 2)/E. Make Good.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/2143/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + string par; + cin >> par; + + if (n & 1) { + cout << "-1\n"; + continue; + } + + vi count(2); + repv(i, par) { + count[i == ')']++; + } + + if (abs(count[0] - count[1]) % 4 != 0) { + cout << "-1\n"; + continue; + } + + string fds; + int c = 0; + + repv(i, par) { + fds += i; + + if (fds.size() > 1 && fds[fds.size() - 1] == fds[fds.size() - 2]) { + fds.pop_back(); + fds.pop_back(); + c++; + } + } + + if (!fds.empty() && (fds.back() == '(') && c == 0) { + cout << "-1\n"; + continue; + } + + if (c) { + fds = "((" + fds + "))"; + c -= 2; + } + + fds = string(c, '(') + fds + string(c, ')'); + cout << fds << '\n'; + } +} diff --git a/Codeforces Round 1052 (Div. 2)/A. Equal Occurrences.cpp b/Codeforces Round 1052 (Div. 2)/A. Equal Occurrences.cpp new file mode 100644 index 0000000..547c40f --- /dev/null +++ b/Codeforces Round 1052 (Div. 2)/A. Equal Occurrences.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/contest/2146/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi a(n); + cin >> a; + + vi fds(n + 1); + repv(i, a) { + fds[i]++; + } + + int ans = 0; + + + rep(i, n + 1) { + int now = 0; + + rep(j, n + 1) { + if (fds[j] >= i) { + now += i; + } + } + + rmax(ans, now); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1052 (Div. 2)/B. Merging the Sets.cpp b/Codeforces Round 1052 (Div. 2)/B. Merging the Sets.cpp new file mode 100644 index 0000000..f37d228 --- /dev/null +++ b/Codeforces Round 1052 (Div. 2)/B. Merging the Sets.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2146/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vi count(m); + vvi them(n); + + int c = 0; + + rep(i, n) { + int l; + cin >> l; + + while (l--) { + int a; + cin >> a; + count[a - 1]++; + them[i].push_back(a - 1); + } + } + + if (*min_element(all(count)) <= 0) { + cout << "NO\n"; + continue; + } + + rep(i, n) { + bool pos = true; + repv(j, them[i]) { + if (count[j] == 1) { + pos = false; + break; + } + } + + if (pos) { + c++; + } + } + + cout << (c >= 2 ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1052 (Div. 2)/C. Wrong Binary Search.cpp b/Codeforces Round 1052 (Div. 2)/C. Wrong Binary Search.cpp new file mode 100644 index 0000000..5a56ecb --- /dev/null +++ b/Codeforces Round 1052 (Div. 2)/C. Wrong Binary Search.cpp @@ -0,0 +1,136 @@ +/* Problem URL: https://codeforces.com/contest/2146/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + string a; + cin >> a; + + vi perm(n, -1); + int c = 0; + bool pos = true; + + rep(i, n) { + if (a[i] == '1') { + perm[i] = i + 1; + c++; + continue; + } + + if ((i == 0 || a[i - 1] == '1') && (i == n - 1 || a[i + 1] == '1')) { + pos = false; + break; + } + } + + if (!pos) { + cout << "NO\n"; + continue; + } + + int i = 0; + int now = 0; + + while (i < n) { + while (i < n && a[i] == '0') { + i++; + } + + int tmp = i - 1; + while (tmp >= 0 && a[tmp] == '0') { + perm[tmp] = now + 1; + tmp--; + now++; + } + + now++; + i++; + } + + cout << "YES\n"; + cout << perm; + } +} diff --git a/Codeforces Round 1052 (Div. 2)/D1. Max Sum OR (Easy Version).cpp b/Codeforces Round 1052 (Div. 2)/D1. Max Sum OR (Easy Version).cpp new file mode 100644 index 0000000..46fdfcd --- /dev/null +++ b/Codeforces Round 1052 (Div. 2)/D1. Max Sum OR (Easy Version).cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/contest/2146/problem/D1 */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int l, r; + cin >> l >> r; + + if (r == 0) { + cout << "0\n0\n"; + continue; + } + + int n = r + 1; + + vi perm(n, -1); + + set all; + rep(i, n) { + all.insert(i); + } + + int log = 32 - __builtin_clz(r); + // cout << log << '\n'; + + int now = r; + rep(i, n) { + if (perm[now] != -1) { + now--; + continue; + } + + int act = 0; + rep(j, log) { + if (!((now >> j) & 1)) { + act |= 1 << j; + } + } + + while (act > 0 && !all.count(act)) { + act -= 1 << (31 - __builtin_clz(act)); + } + + if (!all.count(act)) { + now--; + continue; + } + + // cout << now << ' ' << act << '\n'; + + perm[now] = act; + perm[act] = now; + all.erase(now); + all.erase(act); + now--; + } + + rep(i, n) { + if (perm[i] != -1) { + continue; + } + + perm[i] = *prev(all.end()); + all.erase(prev(all.end())); + } + + ll total = 0; + rep(i, n) { + total += perm[i] | i; + } + cout << total << '\n'; + cout << perm; + } +} diff --git a/Codeforces Round 1052 (Div. 2)/D2. Max Sum OR (Hard Version).cpp b/Codeforces Round 1052 (Div. 2)/D2. Max Sum OR (Hard Version).cpp new file mode 100644 index 0000000..77a8d59 --- /dev/null +++ b/Codeforces Round 1052 (Div. 2)/D2. Max Sum OR (Hard Version).cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/contest/2146/problem/D2 */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + V> trie(1); + V> count(1); + + auto add = [&](ll num) { + int now = 0; + rep(i, 31) { + int bit = (num >> i) & 1; + count[now][bit]++; + if (trie[now][bit] == 0) { + trie[now][bit] = trie.size(); + count.emplace_back(); + trie.emplace_back(); + } + + now = trie[now][bit]; + } + }; + + auto rem = [&](ll num) { + int now = 0; + rep(i, 31) { + int bit = (num >> i) & 1; + count[now][bit]--; + now = trie[now][bit]; + } + }; + + ll l, r; + cin >> l >> r; + + nrep(i, l, r + 1) { + add(i); + } + + int n = r - l + 1; + vl ans(n); + + for (int i = r; i >= l; i--) { + ll tmp = 0; + function find = [&](ll now, int j) { + if (j > 30) { + return; + } + + int bit = (i >> j) & 1; + if (count[now][bit^1] == 0) { + tmp |= (bit << j); + find(trie[now][bit], j + 1); + return; + } + + tmp |= ((bit^1) << j); + find(trie[now][bit^1], j + 1); + }; + + find(0, 0); + + rem(tmp); + ans[i - l] = tmp; + } + + ll total = 0; + rep(i, n) { + total += ans[i] | (i + l); + } + + cout << total << '\n'; + cout << ans; + } +} diff --git a/Codeforces Round 1052 (Div. 2)/E. Yet Another MEX Problem.cpp b/Codeforces Round 1052 (Div. 2)/E. Yet Another MEX Problem.cpp new file mode 100644 index 0000000..50a3084 --- /dev/null +++ b/Codeforces Round 1052 (Div. 2)/E. Yet Another MEX Problem.cpp @@ -0,0 +1,184 @@ +/* Problem URL: https://codeforces.com/contest/2146/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int prev = n; + n++; + fds.push_back(0); + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(0); + } + + vvi segm(n * 2); + + function build = [&](int i, int l, int r) { + if (l == r) { + segm[i] = {fds[i - n]}; + return; + } + + int mid = (l + r) >> 1; + + build(i * 2, l, mid); + build(i * 2 + 1, mid + 1, r); + + merge(all(segm[i * 2]), all(segm[i * 2 + 1]), back_inserter(segm[i])); + }; + + build(1, 0, n - 1); + + function querym = [&](int i, int l, int r, int tl, int tr, int v) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + auto upper = upper_bound(all(segm[i]), v); + return (int)(segm[i].end() - upper); + } + + int mid = (l + r) >> 1; + + return querym(i * 2, l, mid, tl, tr, v) + querym(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + vi seg(n * 2, -1); + + auto update = [&](int i, int v) { + i += n; + seg[i] = v; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + int oo = INT32_MAX >> 1; + if (l > tr || r < tl) { + return oo; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return min(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + vi ans(prev); + rep(i, prev) { + update(fds[i], i); + + int mex = 0; + int ans = 0; + + while (mex <= n) { + int minimal = query(1, 0, n - 1, 0, mex); + + int tmp = querym(1, 0 , n - 1, minimal, i, mex); + + if (tmp >= ans) { + ans = tmp; + mex++; + continue; + } + + break; + } + + cout << ans << " \n"[i == prev - 1]; + } + } +} diff --git a/Codeforces Round 1053 (Div. 2)/A. Incremental Subarray.cpp b/Codeforces Round 1053 (Div. 2)/A. Incremental Subarray.cpp new file mode 100644 index 0000000..b9e3b52 --- /dev/null +++ b/Codeforces Round 1053 (Div. 2)/A. Incremental Subarray.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://codeforces.com/contest/2151/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vi fds(m); + cin >> fds; + + auto getans = [&]() { + for (int i = m - 1; i > 0; i--) { + if (fds[i] <= fds[i - 1]) { + return 1; + } + } + + return n - fds.back() + 1; + }; + + cout << getans() << '\n'; + } +} diff --git a/Codeforces Round 1053 (Div. 2)/B. Incremental Path.cpp b/Codeforces Round 1053 (Div. 2)/B. Incremental Path.cpp new file mode 100644 index 0000000..ba56979 --- /dev/null +++ b/Codeforces Round 1053 (Div. 2)/B. Incremental Path.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://codeforces.com/contest/2151/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + int lim = 100 * n; + + V black(lim); + string op; + cin >> op; + + set rem; + + while (m--) { + int now; + cin >> now; + + if (now > lim) { + rem.insert(now); + continue; + } + + black[now - 1] = true; + } + + int now = 0; + rep(i, n) { + now++; + if (op[i] == 'A') { + black[now] = true; + continue; + } + + int prev = now; + while (black[now]) { + now++; + } + + black[now] = true; + + if (black[prev]) { + while (black[now]) { + now++; + } + continue; + } + + now = prev; + now--; + } + + vi ans; + rep(i, lim) { + if (black[i]) { + ans.push_back(i + 1); + } + } + + cout << ans.size() + rem.size() << '\n'; + repv(i, ans) { + cout << i << ' '; + } + repv(i, rem) { + cout << i << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1053 (Div. 2)/C. Incremental Stay.cpp b/Codeforces Round 1053 (Div. 2)/C. Incremental Stay.cpp new file mode 100644 index 0000000..9150ecf --- /dev/null +++ b/Codeforces Round 1053 (Div. 2)/C. Incremental Stay.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/2151/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + n <<= 1; + + vl fds(n); + cin >> fds; + + vl count(2); + + for (int i = 0; i < n; i += 2) { + count[0] += fds[i]; + count[1] += fds[i + 1]; + } + + ll ans = 0; + rep(k, n >> 1) { + cout << count[(k & 1) ^ 1] - count[k & 1] + ans << ' '; + count[k & 1] -= fds[k]; + count[(k & 1) ^ 1] -= fds[n - k - 1]; + ans += fds[n - k - 1] - fds[k]; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1053 (Div. 2)/D. Grid Counting.cpp b/Codeforces Round 1053 (Div. 2)/D. Grid Counting.cpp new file mode 100644 index 0000000..db1612a --- /dev/null +++ b/Codeforces Round 1053 (Div. 2)/D. Grid Counting.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/contest/2151/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + const ll mod = 998244353; + vl fact(2e5 + 1); + vl inv(2e5 + 1); + + fact[0] = 1; + fact[1] = 1; + + nrep(i, 2, 2e5 + 1) { + fact[i] = (fact[i - 1] * i) % mod; + } + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 30) { + if ((p >> i) & 1) { + ans *= a; + ans %= mod; + } + + a *= a; + a %= mod; + } + + return ans; + }; + + auto comb = [&](ll n, ll k) { + return (fact[n] * fpow((fact[k] * fact[n - k]) % mod, mod - 2)) % mod; + }; + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi count(n); + cin >> count; + + vi lim(n); + rep(i, n >> 1) { + lim[i] = 2; + } + + if (n & 1) { + lim[n >> 1] = 1; + } + + auto getans = [&]() { + if (accumulate(all(count), 0LL) != n) { + return 0LL; + } + + ll ans = 1; + + int total = 0; + for (int i = n - 1; i >= 0; i--) { + int c = lim[i]; + total += c; + + if (total < count[i]) { + return 0LL; + } + + ans *= comb(total, count[i]); + ans %= mod; + total -= count[i]; + } + + return ans; + }; + + cout << getans() << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/A. Be Positive.cpp b/Codeforces Round 1054 (Div. 3)/A. Be Positive.cpp new file mode 100644 index 0000000..1157e95 --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/A. Be Positive.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/2149/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + sortv(fds); + + ll ans = 0; + int neg = 0; + rep(i, n) { + if (fds[i] == 0) { + ans++; + continue; + } + + if (fds[i] < 0) { + neg++; + } + } + + if (neg & 1) { + ans += -fds[0] + 1; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/B. Unconventional Pairs.cpp b/Codeforces Round 1054 (Div. 3)/B. Unconventional Pairs.cpp new file mode 100644 index 0000000..7d20bb1 --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/B. Unconventional Pairs.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/contest/2149/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi fds(n); + cin >> fds; + sortv(fds); + + int ans = 0; + for (int i = 0; i < n; i += 2) { + rmax(ans, fds[i + 1] - fds[i]); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/C. MEX rose.cpp b/Codeforces Round 1054 (Div. 3)/C. MEX rose.cpp new file mode 100644 index 0000000..aad898b --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/C. MEX rose.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/2149/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi count(n + 1); + rep(i, n) { + int n; + cin >> n; + count[n]++; + } + + int ans = 0; + int add = 0; + rep(i, k) { + if (count[i] == 0) { + add++; + ans++; + } + } + + ans += max(0, count[k] - add); + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/D. A and B.cpp b/Codeforces Round 1054 (Div. 3)/D. A and B.cpp new file mode 100644 index 0000000..c773fca --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/D. A and B.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/contest/2149/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + string a; + cin >> a; + + vi act(n); + rep(i, n) { + act[i] = a[i] - 'a'; + } + + vl apref(n + 1); + vl asuf(n + 1); + vl bpref(n + 1); + vl bsuf(n + 1); + + int c = 0; + rep(i, n) { + apref[i + 1] = apref[i]; + if (act[i]) { + apref[i + 1] += i - c; + c++; + } + } + + c = 0; + for (int i = n - 1; i >= 0; i--) { + asuf[i] = asuf[i + 1]; + if (act[i]) { + asuf[i] += n - i - 1 - c; + c++; + } + } + + c = 0; + rep(i, n) { + bpref[i + 1] = bpref[i]; + if (!act[i]) { + bpref[i + 1] += i - c; + c++; + } + } + + c = 0; + for (int i = n - 1; i >= 0; i--) { + bsuf[i] = bsuf[i + 1]; + if (!act[i]) { + bsuf[i] += n - i - c - 1; + c++; + } + } + + + ll ans = INT64_MAX >> 1; + + rep(i, n) { + if (act[i]) { + rmin(ans, bpref[i] + bsuf[i + 1]); + continue; + } + + rmin(ans, apref[i] + asuf[i + 1]); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/E. Hidden Knowledge of the Ancients.cpp b/Codeforces Round 1054 (Div. 3)/E. Hidden Knowledge of the Ancients.cpp new file mode 100644 index 0000000..79c63dd --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/E. Hidden Knowledge of the Ancients.cpp @@ -0,0 +1,175 @@ +/* Problem URL: https://codeforces.com/contest/2149/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k, l, r; + cin >> n >> k >> l >> r; + + map var; + vi fds(n); + repv(i, fds) { + cin >> i; + var[i] = 0; + } + + int v = 0; + repv(i, var) { + i.second = v; + v++; + } + + repv(i, fds) { + i = var[i]; + } + + vi elem1(v); + vi elem2(v); + + int c1 = 0; + int c2 = 0; + + auto add = [&](vi &elem, int i, int &c) { + elem[i]++; + if (elem[i] == 1) { + c++; + } + }; + + auto rem = [&](vi &elem, int i, int &c) { + elem[i]--; + if (elem[i] == 0) { + c--; + } + }; + + int l1 = 0; + int l2 = 0; + + rep(i, l - 1) { + add(elem1, fds[i], c1); + add(elem2, fds[i], c2); + } + + auto reorder1 = [&](int start, int lim) { + while (l1 < start) { + rem(elem1, fds[l1], c1); + l1++; + } + + while (c1 > k && l1 < lim) { + rem(elem1, fds[l1], c1); + l1++; + } + }; + + auto reorder2 = [&](int start, int lim) { + while (l2 < start) { + rem(elem2, fds[l2], c2); + l2++; + } + + while (c2 >= k && l2 <= lim) { + rem(elem2, fds[l2], c2); + l2++; + } + }; + + ll ans = 0; + + nrep(i, l - 1, n) { + add(elem1, fds[i], c1); + add(elem2, fds[i], c2); + reorder1(i - r + 1, i - l + 1); + reorder2(i - r + 1, i - l + 1); + + if (c1 != k) { + continue; + } + + ans += l2 - l1; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/F. Nezuko in the Clearing.cpp b/Codeforces Round 1054 (Div. 3)/F. Nezuko in the Clearing.cpp new file mode 100644 index 0000000..0d177c4 --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/F. Nezuko in the Clearing.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2149/problem/F */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll h, d; + cin >> h >> d; + + ll add = 0; + if (h == 1) { + add = 1; + } else { + h--; + } + + ll low = 0; + ll high = d - 1; + ll ans = d; + while (low <= high) { + ll mid = (high + low) >> 1; + + ll div = d / (mid + 1); + ll rem = d % (mid + 1); + + ll tmp = (div * (div + 1) / 2) * (mid - rem + 1) + ((div + 1) * (div + 2) / 2) * rem - mid; + + if (tmp <= h) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + }; + + cout << ans + d + add << '\n'; + } +} diff --git a/Codeforces Round 1054 (Div. 3)/G. Buratsuta 3.cpp b/Codeforces Round 1054 (Div. 3)/G. Buratsuta 3.cpp new file mode 100644 index 0000000..7c0e840 --- /dev/null +++ b/Codeforces Round 1054 (Div. 3)/G. Buratsuta 3.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/contest/2149/problem/G */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + mt19937 seed(chrono::steady_clock::now().time_since_epoch().count()); + + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + + vl fds(n); + + map var; + repv(i, fds) { + cin >> i; + var[i] = 0; + } + + int v = 0; + vi inv(var.size()); + repv(i, var) { + i.second = v; + inv[v] = i.first; + v++; + } + + vvi pos(var.size()); + + rep(i, n) { + fds[i] = var[fds[i]]; + pos[fds[i]].push_back(i); + } + + while (q--) { + set ans; + + int l, r; + cin >> l >> r; + l--, r--; + + uniform_int_distribution<> rng(l, r); + + int lim = (r - l + 1) / 3; + + rep(i, 50) { + int now = rng(seed); + int count = upper_bound(all(pos[fds[now]]), r) - lower_bound(all(pos[fds[now]]), l); + + if (count > lim) { + ans.insert(inv[fds[now]]); + } + } + + if (ans.empty()) { + cout << "-1\n"; + } + + repv(i, ans) { + cout << i << ' '; + } + cout << '\n'; + } + } +} diff --git a/Codeforces Round 112 (Div. 2)/E. Compatible Numbers.cpp b/Codeforces Round 112 (Div. 2)/E. Compatible Numbers.cpp new file mode 100644 index 0000000..2313552 --- /dev/null +++ b/Codeforces Round 112 (Div. 2)/E. Compatible Numbers.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/165/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + const int log = 22; + V sos(1 << log); + repv(i, fds) { + sos[i] = true; + } + + rep(i, log) { + for (int j = 0; j < (1 << log); j++) { + if (sos[j]) { + continue; + } + + if ((j >> i) & 1) { + sos[j] = sos[j ^ (1 << i)]; + } + } + } + + repv(i, fds) { + ll now = ~i & ((1 << log) - 1); + rep(j, log) { + if (((now >> j) & 1) && sos[now ^ (1 << j)]) { + now ^= (1 << j); + } + } + + cout << (sos[now] ? now : -1) << ' '; + } + cout << '\n'; +} diff --git a/Codeforces Round 225 (Div. 1)/E. Vowels.cpp b/Codeforces Round 225 (Div. 1)/E. Vowels.cpp new file mode 100644 index 0000000..43c19df --- /dev/null +++ b/Codeforces Round 225 (Div. 1)/E. Vowels.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/383/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + const int log = 24; + vl sos(1 << log); + + int n; + cin >> n; + while (n--) { + string a; + cin >> a; + + int mask = 0; + + repv(i, a) { + mask |= 1 << (i - 'a'); + } + + for (int i = mask; i > 0; i = (i - 1)&mask) { + sos[i] += __builtin_popcount(i) & 1 ? 1 : -1; + } + } + + rep(i, log) { + rep(j, 1 << log) { + if ((j >> i) & 1) { + sos[j] += sos[j ^ (1 << i)]; + } + } + } + + ll ans = 0; + rep(j, 1 << log) { + ans ^= sos[j] * sos[j]; + } + + cout << ans << '\n'; +} diff --git a/Codeforces Round 271 (Div. 2)/F. Ant colony.cpp b/Codeforces Round 271 (Div. 2)/F. Ant colony.cpp new file mode 100644 index 0000000..940139d --- /dev/null +++ b/Codeforces Round 271 (Div. 2)/F. Ant colony.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/problemset/problem/474/F */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vvi sparse(20, vi(n)); + + rep(i, n) { + sparse[0][i] = fds[i]; + } + + nrep(j, 1, 20) { + rep(i, n - (1 << (j - 1))) { + sparse[j][i] = __gcd(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto squery = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return __gcd(sparse[log][l], sparse[log][r - (1 << log) + 1]); + }; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(1); + } + + vvi seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = {fds[i - n]}; + } + + for (int i = n - 1; i > 0; i--) { + merge(all(seg[i * 2]), all(seg[i * 2 + 1]), back_inserter(seg[i])); + } + + function query = [&](int i, int l, int r, int tl, int tr, int v) -> int { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return upper_bound(all(seg[i]), v) - lower_bound(all(seg[i]), v); + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr, v) + query(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + int q; + cin >> q; + while (q--) { + int l, r; + cin >> l >> r; + l--, r--; + cout << (r - l + 1) - query(1, 0, n - 1, l, r, squery(l, r)) << '\n'; + } +} diff --git a/Educational Codeforces Round 1/A. Tricky Sum.cpp b/Educational Codeforces Round 1/A. Tricky Sum.cpp new file mode 100644 index 0000000..07f7d18 --- /dev/null +++ b/Educational Codeforces Round 1/A. Tricky Sum.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/contest/598/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + ll ans = n * (n + 1) / 2; + + ll now = 1; + while (now <= n) { + ans -= now * 2; + now <<= 1; + } + + cout << ans << '\n'; + } +} diff --git a/Educational Codeforces Round 1/B. Queries on a String.cpp b/Educational Codeforces Round 1/B. Queries on a String.cpp new file mode 100644 index 0000000..2244cb5 --- /dev/null +++ b/Educational Codeforces Round 1/B. Queries on a String.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/contest/598/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string s; + cin >> s; + + int m; + cin >> m; + while (m--) { + int l, r, k; + cin >> l >> r >> k; + l--, r--; + + k = (k - 1) % (r - l + 1) + 1; + + string ans = s.substr(r - k + 1, k) + s.substr(l, (r - l + 1) - k); + rep(i, ans.size()) { + s[l + i] = ans[i]; + } + } + + cout << s << '\n'; +} diff --git a/Educational Codeforces Round 1/C. Nearest vectors.cpp b/Educational Codeforces Round 1/C. Nearest vectors.cpp new file mode 100644 index 0000000..c2ca24d --- /dev/null +++ b/Educational Codeforces Round 1/C. Nearest vectors.cpp @@ -0,0 +1,183 @@ +/* Problem URL: https://codeforces.com/contest/598/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; +} + +#define sq(x) ((x)*(ll)(x)) + +struct pt { // ponto + ll x, y; + pt(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} + bool operator < (const pt p) const { + if (x != p.x) return x < p.x; + return y < p.y; + } + bool operator == (const pt p) const { + return x == p.x and y == p.y; + } + pt operator + (const pt p) const { return pt(x+p.x, y+p.y); } + pt operator - (const pt p) const { return pt(x-p.x, y-p.y); } + pt operator * (const int c) const { return pt(x*c, y*c); } + ll operator * (const pt p) const { return x*(ll)p.x + y*(ll)p.y; } + ll operator ^ (const pt p) const { return x*(ll)p.y - y*(ll)p.x; } + friend istream& operator >> (istream& in, pt& p) { + return in >> p.x >> p.y; + } +}; + +struct line { // reta + pt p, q; + line() {} + line(pt p_, pt q_) : p(p_), q(q_) {} + friend istream& operator >> (istream& in, line& r) { + return in >> r.p >> r.q; + } +}; + +// PONTO & VETOR + +ll dist2(pt p, pt q) { // quadrado da distancia + return sq(p.x - q.x) + sq(p.y - q.y); +} + +ll sarea2(pt p, pt q, pt r) { // 2 * area com sinal + return (q-p)^(r-q); +} + +bool col(pt p, pt q, pt r) { // se p, q e r sao colin. + return sarea2(p, q, r) == 0; +} + +bool ccw(pt p, pt q, pt r) { // se p, q, r sao ccw + return sarea2(p, q, r) > 0; +} + +int quad(pt p) { // quadrante de um ponto + return (p.x<0)^3*(p.y<0); +} + +bool compare_angle(pt p, pt q) { // retorna se ang(p) < ang(q) + if (quad(p) != quad(q)) return quad(p) < quad(q); + return ccw(q, pt(0, 0), p); +} + +pt rotate90(pt p) { // rotaciona 90 graus + return pt(-p.y, p.x); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> fds(n); + rep(i, n) { + cin >> fds[i].first; + fds[i].second = i + 1; + } + + sort(all(fds), [&](pair &a, pair &b) { + return compare_angle(a.first, b.first); + }); + + auto better = [&](pt &a, pt &b, pt &c, pt &d) { + pt p1(a * b, abs(a ^ b)); + pt p2(c * d, abs(c ^ d)); + return (p1 ^ p2) > 0; + }; + + int ans1 = 0; + int ans2 = 1; + nrep(i, 1, n - 1) { + pt &one = fds[i].first; + pt &two = fds[i + 1].first; + if (better(one, two, fds[ans1].first, fds[ans2].first)) { + ans1 = i; + ans2 = i + 1; + } + } + + pt &one = fds.back().first; + pt &two = fds[0].first; + if (better(one, two, fds[ans1].first, fds[ans2].first)) { + ans1 = n - 1; + ans2 = 0; + } + + cout << fds[ans1].second << ' ' << fds[ans2].second << '\n'; +} diff --git a/Educational Codeforces Round 1/D. Igor In the Museum.cpp b/Educational Codeforces Round 1/D. Igor In the Museum.cpp new file mode 100644 index 0000000..97d9ec4 --- /dev/null +++ b/Educational Codeforces Round 1/D. Igor In the Museum.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/contest/598/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + + V fds(n); + cin >> fds; + + V> vis(n, V(m)); + + function dfs = [&](int i, int j) { + if (fds[i][j] == '*') { + return 1; + } + + vis[i][j] = true; + + int ans = 0; + + int add[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + if (id >= 0 && id < n && jd >= 0 && jd < m && !vis[id][jd]) { + ans += dfs(id, jd); + } + } + + return ans; + }; + + vvi ans(n, vi(m)); + + function flood = [&](int i, int j, int v) { + fds[i][j] = '*'; + ans[i][j] = v; + + int add[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + if (id >= 0 && id < n && jd >= 0 && jd < m && fds[id][jd] == '.') { + flood(id, jd, v); + } + } + }; + + rep(i, n) { + rep(j, m) { + if (fds[i][j] == '.' && !vis[i][j]) { + int tmp = dfs(i, j); + flood(i, j, tmp); + } + } + } + + while (k--) { + int i, j; + cin >> i >> j; + cout << ans[i - 1][j - 1] << '\n'; + } +} diff --git a/Educational Codeforces Round 182 (Rated for Div. 2)/C. Non-Descending Arrays.cpp b/Educational Codeforces Round 182 (Rated for Div. 2)/C. Non-Descending Arrays.cpp new file mode 100644 index 0000000..7d0f01d --- /dev/null +++ b/Educational Codeforces Round 182 (Rated for Div. 2)/C. Non-Descending Arrays.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/contest/2144/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + vi b(n); + cin >> a >> b; + + ll mod = 998244353; + vvl dp(n, vl(2)); + + dp[0][0] = 1; + dp[0][1] = 1; + + nrep(i, 1, n) { + if (a[i] >= a[i - 1] && b[i] >= b[i - 1]) { + dp[i][0] += dp[i - 1][0]; + dp[i][0] %= mod; + dp[i][1] += dp[i - 1][1]; + dp[i][1] %= mod; + } + + if (a[i] >= b[i - 1] && b[i] >= a[i - 1]) { + dp[i][0] += dp[i - 1][1]; + dp[i][0] %= mod; + dp[i][1] += dp[i - 1][0]; + dp[i][1] %= mod; + } + } + + cout << (dp.back()[0] + dp.back()[1]) % mod << '\n'; + } +} diff --git a/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp b/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp new file mode 100644 index 0000000..f19d75d --- /dev/null +++ b/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp @@ -0,0 +1,115 @@ +/* Problem URL: https://codeforces.com/contest/2144/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll y; + cin >> n >> y; + + set divs; + vl fds(n); + cin >> fds; + + auto getdivs = [&](ll v) { + for (ll i = 1; i * i <= v; i++) { + if (v % i == 0) { + divs.insert(i); + divs.insert(v / i); + } + } + }; + + repv(i, fds) { + getdivs(i); + } + + divs.erase(1); + + ll ans = -1e17; + + repv(j, divs) { + + } + } +} diff --git a/Educational Codeforces Round 40 (Rated for Div. 2)/A. Diagonal Walking.cpp b/Educational Codeforces Round 40 (Rated for Div. 2)/A. Diagonal Walking.cpp new file mode 100644 index 0000000..2afa4b8 --- /dev/null +++ b/Educational Codeforces Round 40 (Rated for Div. 2)/A. Diagonal Walking.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/contest/954/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + string a; + cin >> n >> a; + + map valid; + valid['U'] = 'R'; + valid['R'] = 'U'; + + int ans = 0; + + nrep(i, 1, n) { + ans++; + if (a[i] == valid[a[i - 1]]) { + a[i] = ' '; + ans--; + } + } + + cout << ans + 1 << '\n'; +} diff --git a/Educational Codeforces Round 40 (Rated for Div. 2)/B. String Typing.cpp b/Educational Codeforces Round 40 (Rated for Div. 2)/B. String Typing.cpp new file mode 100644 index 0000000..5045f4d --- /dev/null +++ b/Educational Codeforces Round 40 (Rated for Div. 2)/B. String Typing.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/954/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + string a; + cin >> n >> a; + + vvi dp(n, vi(2)); + dp[0][0] = 1; + dp[0][1] = 1; + + nrep(i, 1, n) { + dp[i][0] = dp[i - 1][0] + 1; + dp[i][1] = dp[i - 1][1] + 1; + + if (!(i & 1)) { + continue; + } + + int mid = i >> 1; + + if (a.substr(0, mid + 1) == a.substr(mid + 1, mid + 1)) { + rmin(dp[i][1], dp[mid][0] + 1); + } + } + + cout << min(dp[n - 1][1], dp[n - 1][0]) << '\n'; +} diff --git a/Educational Codeforces Round 40 (Rated for Div. 2)/C. Matrix Walk.cpp b/Educational Codeforces Round 40 (Rated for Div. 2)/C. Matrix Walk.cpp new file mode 100644 index 0000000..7f1648e --- /dev/null +++ b/Educational Codeforces Round 40 (Rated for Div. 2)/C. Matrix Walk.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/contest/954/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + if (n == 1) { + cout << "YES\n"; + cout << "1 1000000000\n"; + return 0; + } + + ll m = -1; + + nrep(i, 1, n) { + if (fds[i] == fds[i - 1]) { + cout << "NO\n"; + return 0; + } + + if (abs(fds[i] - fds[i - 1]) == 1) { + continue; + } + + if (m == -1) { + m = abs(fds[i] - fds[i - 1]); + continue; + } + + if (abs(fds[i] - fds[i - 1]) != m) { + cout << "NO\n"; + return 0; + } + } + + if (m == -1) { + cout << "YES\n"; + cout << "1 1000000000\n"; + return 0; + } + + ll i = (fds[0] - 1) / m; + ll j = (fds[0] - 1) % m; + + ll nans = i + 1; + + nrep(k, 1, n) { + if (abs(fds[k] - fds[k - 1]) == 1) { + ll jd = j + fds[k] - fds[k - 1]; + ll id = i; + + if (jd >= m || jd < 0) { + cout << "NO\n"; + return 0; + } + + i = id; + j = jd; + continue; + } + + ll id = i + (fds[k] - fds[k - 1]) / m; + ll jd = j; + + if (id < 0) { + cout << "NO\n"; + return 0; + } + + rmax(nans, id + 1); + i = id; + j = jd; + } + + if (nans > 1e9 || m > 1e9) { + cout << "NO\n"; + return 0; + } + + cout << "YES\n"; + cout << nans << ' ' << m << '\n'; +} diff --git a/Educational Codeforces Round 40 (Rated for Div. 2)/D. Fight Against Traffic.cpp b/Educational Codeforces Round 40 (Rated for Div. 2)/D. Fight Against Traffic.cpp new file mode 100644 index 0000000..30c1b22 --- /dev/null +++ b/Educational Codeforces Round 40 (Rated for Div. 2)/D. Fight Against Traffic.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/contest/954/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, s, t; + cin >> n >> m >> s >> t; + s--, t--; + + vvi graph(n); + vvi adj(n, vi(n)); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + adj[a][b] = 1; + adj[b][a] = 1; + } + + int oo = INT32_MAX >> 1; + + auto bfs = [&](int s, vi &depth){ + depth[s] = 0; + queue q; + q.push(s); + + while (!q.empty()) { + auto i = q.front(); + q.pop(); + + for (auto j : graph[i]) { + if (depth[j] > depth[i] + 1) { + depth[j] = depth[i] + 1; + q.push(j); + } + } + } + }; + + vi depth1(n, oo); + vi depth2(n, oo); + + bfs(s, depth1); + bfs(t, depth2); + + int minimal = depth1[t]; + + V ispath(n); + + ll ans = 0; + rep(i, n) { + nrep(j, i + 1, n) { + if (adj[i][j]) { + continue; + } + + auto check = [&](int u, int v) { + int ne1 = min(depth1[u], depth1[v] + 1); + int ne2 = min(depth2[u], depth2[v] + 1); + + return ne1 + ne2 >= minimal; + }; + + if (check(i, j) && check(j, i)) { + ans++; + } + } + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 40 (Rated for Div. 2)/E. Water Taps.cpp b/Educational Codeforces Round 40 (Rated for Div. 2)/E. Water Taps.cpp new file mode 100644 index 0000000..bb171ec --- /dev/null +++ b/Educational Codeforces Round 40 (Rated for Div. 2)/E. Water Taps.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://codeforces.com/contest/954/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + long double t; + cin >> n >> t; + + V> a(n); + repv(i, a) { + cin >> i.second; + } + repv(i, a) { + cin >> i.first; + } + + sort(all(a), [&](pair &a, pair &b) { + if (a.first == b.first) { + return a.second > b.second; + } + return a.first < b.first; + }); + + long double start = 0; + long double test = 0; + rep(i, n) { + start += a[i].second; + test += a[i].second * a[i].first; + } + + if (test / start < t) { + cout << "0\n"; + return 0; + } + + long double low = 0; + long double high = start; + rep(i, 300) { + long double mid = (high + low) / 2.0L; + + long double ans = 0; + + long double tmp = mid; + long double total = 0; + + rep(i, n) { + if (a[i].second >= tmp) { + ans += tmp * a[i].first; + total += tmp; + break; + } + + ans += a[i].second * a[i].first; + total += a[i].second; + tmp -= a[i].second; + } + + ans /= total; + + // cout << high - low << ' ' << mid << ' ' << ans << '\n'; + + if (ans <= t) { + low = mid; + continue; + } + + high = mid; + } + + long double tmp = low; + long double total = 0; + long double temp = 0; + + rep(i, n) { + if (a[i].second >= tmp) { + temp += tmp * a[i].first; + total += tmp; + break; + } + + temp += a[i].second * a[i].first; + total += a[i].second; + tmp -= a[i].second; + } + + temp /= total; + + // long double eps = 1e-9; + // if (abs(t - temp) >= eps) { + // cout << "0\n"; + // return 0; + // } + + cout << setprecision(9) << low << '\n'; +} diff --git a/Educational Codeforces Round 41 (Rated for Div. 2)/A. Tetris.cpp b/Educational Codeforces Round 41 (Rated for Div. 2)/A. Tetris.cpp new file mode 100644 index 0000000..e10c5e0 --- /dev/null +++ b/Educational Codeforces Round 41 (Rated for Div. 2)/A. Tetris.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://codeforces.com/contest/961/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi fds(n); + + while (m--) { + int a; + cin >> a; + fds[a - 1]++; + } + + cout << *min_element(all(fds)) << '\n'; +} diff --git a/Educational Codeforces Round 41 (Rated for Div. 2)/B. Lecture Sleep.cpp b/Educational Codeforces Round 41 (Rated for Div. 2)/B. Lecture Sleep.cpp new file mode 100644 index 0000000..4df0622 --- /dev/null +++ b/Educational Codeforces Round 41 (Rated for Div. 2)/B. Lecture Sleep.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/contest/961/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + vl aw(n); + cin >> aw; + + ll ans = 0; + vl pref(n + 1); + vl valpref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + valpref[i + 1] += valpref[i]; + + if (aw[i]) { + ans += fds[i]; + valpref[i + 1] += fds[i]; + } + } + + rep(i, n - k + 1) { + rmax(ans, valpref[i] + pref[i + k] - pref[i] + valpref[n] - valpref[i + k]); + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 41 (Rated for Div. 2)/C. Chessboard.cpp b/Educational Codeforces Round 41 (Rated for Div. 2)/C. Chessboard.cpp new file mode 100644 index 0000000..da0b228 --- /dev/null +++ b/Educational Codeforces Round 41 (Rated for Div. 2)/C. Chessboard.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/contest/961/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> fds(4, V(n)); + + repv(i, fds) { + cin >> i; + } + + vi perm = {0, 1, 2, 3}; + + int ans = INT32_MAX >> 1; + + do { + V act(2 * n); + rep(i, n) { + act[i] = fds[perm[0]][i] + fds[perm[1]][i]; + act[i + n] = fds[perm[2]][i] + fds[perm[3]][i]; + } + + int now = 0; + rep(i, 2 * n) { + rep(j, 2 * n) { + if ((i + j) & 1) { + now += act[i][j] == '0'; + } else { + now += act[i][j] == '1'; + } + } + } + + rmin(ans, now); + + now = 0; + rep(i, 2 * n) { + rep(j, 2 * n) { + if ((i + j) & 1) { + now += act[i][j] == '1'; + } else { + now += act[i][j] == '0'; + } + } + } + + rmin(ans, now); + } while (next_permutation(all(perm))); + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 41 (Rated for Div. 2)/D. Pair Of Lines.cpp b/Educational Codeforces Round 41 (Rated for Div. 2)/D. Pair Of Lines.cpp new file mode 100644 index 0000000..d4c8500 --- /dev/null +++ b/Educational Codeforces Round 41 (Rated for Div. 2)/D. Pair Of Lines.cpp @@ -0,0 +1,206 @@ +/* Problem URL: https://codeforces.com/contest/961/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; +} + +// Geometria + +typedef long long ld; +const ld DINF = 1e18; +const ld pi = acos(-1.0); +const ld eps = 1e-9; + +#define sq(x) ((x)*(x)) + +bool eq(ld a, ld b) { + return abs(a - b) <= eps; +} + +struct pt { // ponto + ld x, y; + pt(ld x_ = 0, ld y_ = 0) : x(x_), y(y_) {} + bool operator < (const pt p) const { + if (!eq(x, p.x)) return x < p.x; + if (!eq(y, p.y)) return y < p.y; + return 0; + } + bool operator == (const pt p) const { + return eq(x, p.x) and eq(y, p.y); + } + pt operator + (const pt p) const { return pt(x+p.x, y+p.y); } + pt operator - (const pt p) const { return pt(x-p.x, y-p.y); } + pt operator * (const ld c) const { return pt(x*c , y*c ); } + pt operator / (const ld c) const { return pt(x/c , y/c ); } + ld operator * (const pt p) const { return x*p.x + y*p.y; } + ld operator ^ (const pt p) const { return x*p.y - y*p.x; } + friend istream& operator >> (istream& in, pt& p) { + return in >> p.x >> p.y; + } +}; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + if (n <= 3) { + cout << "YES\n"; + return 0; + } + + V a(n); + cin >> a; + + V group(n); + V invalid(n); + + auto iscol = [&](pt a, pt b, pt c) { + return (b.y - a.y) * (c.x - b.x) == (c.y - b.y) * (b.x - a.x); + }; + + auto check = [&]() { + int first = 0; + while (group[first]) { + first++; + } + + int second = first + 1; + while (group[second]) { + second++; + } + + rep(i, n) { + if (group[i]) { + continue; + } + + if (!iscol(a[first], a[second], a[i])) { + return false; + } + } + + return true; + }; + + int c = 0; + + rep(i, n) { + if (iscol(a[0], a[1], a[i])) { + group[i] = true; + } else { + c++; + } + } + + if (c <= 2 || check()) { + cout << "YES\n"; + return 0; + } + + fill(all(group), false); + c = 0; + rep(i, n) { + if (iscol(a[0], a[2], a[i])) { + group[i] = true; + } else { + c++; + } + } + + if (c <= 2 || check()) { + cout << "YES\n"; + return 0; + } + + fill(all(group), false); + c = 0; + rep(i, n) { + if (iscol(a[1], a[2], a[i])) { + group[i] = true; + } else { + c++; + } + } + + if (c <= 2 || check()) { + cout << "YES\n"; + return 0; + } + + cout << "NO\n"; +} diff --git a/Educational Codeforces Round 41 (Rated for Div. 2)/E. Tufurama.cpp b/Educational Codeforces Round 41 (Rated for Div. 2)/E. Tufurama.cpp new file mode 100644 index 0000000..825a670 --- /dev/null +++ b/Educational Codeforces Round 41 (Rated for Div. 2)/E. Tufurama.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/contest/961/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int size = n; + while (__builtin_popcount(size) != 1) { + size++; + fds.push_back(0); + } + + vvl mergeseg(size * 2); + + function build = [&](int i, int l, int r) { + if (l == r) { + mergeseg[i] = {fds[i - size]}; + return; + } + + int mid = (l + r) >> 1; + + build(i * 2, l, mid); + build(i * 2 + 1, mid + 1, r); + + merge(all(mergeseg[i * 2]), all(mergeseg[i * 2 + 1]), back_inserter(mergeseg[i])); + }; + + build(1, 0, size - 1); + + function query = [&](int i, int l, int r, int tl, int tr, ll v) -> ll { + if (l > tr || r < tl) { + return 0LL; + } + + if (l >= tl && r <= tr) { + auto lower = lower_bound(all(mergeseg[i]), v); + return mergeseg[i].end() - lower; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr, v) + query(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + ll ans = 0; + + rep(i, n) { + rmin(fds[i], n); + if (fds[i] <= i + 1) { + continue; + } + + ans += query(1, 0, size - 1, i + 1, fds[i] - 1, i + 1); + } + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 42 (Rated for Div. 2)/A. Equator.cpp b/Educational Codeforces Round 42 (Rated for Div. 2)/A. Equator.cpp new file mode 100644 index 0000000..a7ebd8c --- /dev/null +++ b/Educational Codeforces Round 42 (Rated for Div. 2)/A. Equator.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/962/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + vi a(n); + cin >> a; + + ll total = accumulate(all(a), 0LL); + + ll now = 0; + int i = 0; + while (now < ((total + 1) >> 1)) { + now += a[i]; + i++; + } + + cout << i << '\n'; +} diff --git a/Educational Codeforces Round 42 (Rated for Div. 2)/B. Students in Railway Carriage.cpp b/Educational Codeforces Round 42 (Rated for Div. 2)/B. Students in Railway Carriage.cpp new file mode 100644 index 0000000..8df68ad --- /dev/null +++ b/Educational Codeforces Round 42 (Rated for Div. 2)/B. Students in Railway Carriage.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/962/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, a, b; + cin >> n >> a >> b; + + string fds; + cin >> fds; + + vi c = {a, b}; + + int s = c[0] > c[1] ? 0 : 1; + + int prev = -1; + int ans = 0; + + rep(i, n) { + if (fds[i] == '*') { + int now = s; + for (int j = prev + 1; j < i; j++) { + if (c[now]) { + ans++; + c[now]--; + } + now ^= 1; + } + + s = c[0] > c[1] ? 0 : 1; + prev = i; + } + } + + int now = s; + for (int j = prev + 1; j < n; j++) { + if (c[now]) { + ans++; + c[now]--; + } + now ^= 1; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 42 (Rated for Div. 2)/C. Make a Square.cpp b/Educational Codeforces Round 42 (Rated for Div. 2)/C. Make a Square.cpp new file mode 100644 index 0000000..d77774b --- /dev/null +++ b/Educational Codeforces Round 42 (Rated for Div. 2)/C. Make a Square.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/962/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string n; + cin >> n; + + int ans = INT32_MAX >> 1; + + nrep(i, 1, 1e5) { + string now = to_string((ll)i * i); + + int j = 0; + int k = 0; + + int tmp = 0; + + while (j < n.size() && k < now.size()) { + if (n[j] == now[k]) { + k++; + } else { + tmp++; + } + j++; + } + + if (k < now.size()) { + continue; + } + + rmin(ans, tmp + (int)n.size() - j); + } + + if (ans == INT32_MAX >> 1) { + cout << "-1\n"; + return 0; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 42 (Rated for Div. 2)/D. Merge Equals.cpp b/Educational Codeforces Round 42 (Rated for Div. 2)/D. Merge Equals.cpp new file mode 100644 index 0000000..091b4da --- /dev/null +++ b/Educational Codeforces Round 42 (Rated for Div. 2)/D. Merge Equals.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/contest/962/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl a(n); + cin >> a; + + set act; + map pos; + + rep(i, n) { + if (!act.count(a[i])) { + act.insert(a[i]); + pos[a[i]] = i; + continue; + } + + act.erase(a[i]); + pos.erase(a[i]); + a[i] *= 2; + i--; + } + + V> ans; + repv(i, pos) { + ans.emplace_back(i.second, i.first); + } + + sortv(ans); + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.second << ' '; + } + cout << '\n'; +} diff --git a/Educational Codeforces Round 42 (Rated for Div. 2)/E. Byteland, Berland and Disputed Cities.cpp b/Educational Codeforces Round 42 (Rated for Div. 2)/E. Byteland, Berland and Disputed Cities.cpp new file mode 100644 index 0000000..cf36c03 --- /dev/null +++ b/Educational Codeforces Round 42 (Rated for Div. 2)/E. Byteland, Berland and Disputed Cities.cpp @@ -0,0 +1,227 @@ +/* Problem URL: https://codeforces.com/contest/962/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl r; + vl b; + vl p; + + rep(i, n) { + ll x; + char o; + cin >> x >> o; + + if (o == 'R') { + r.push_back(x); + continue; + } + + if (o == 'B') { + b.push_back(x); + continue; + } + + p.push_back(x); + } + + sortv(r); + sortv(b); + sortv(p); + + ll ans = 0; + nrep(i, 1, p.size()) { + ans += p[i] - p[i - 1]; + } + + V> tmp; + + repv(i, r) { + tmp.emplace_back(i, 0); + } + repv(i, p) { + tmp.emplace_back(i, 1); + } + sortv(tmp); + + nrep(i, 1, tmp.size()) { + if (tmp[i].second == 0 || tmp[i - 1].second == 0) { + ans += tmp[i].first - tmp[i - 1].first; + } + } + + tmp.clear(); + + repv(i, b) { + tmp.emplace_back(i, 0); + } + repv(i, p) { + tmp.emplace_back(i, 1); + } + sortv(tmp); + + nrep(i, 1, tmp.size()) { + if (tmp[i].second == 0 || tmp[i - 1].second == 0) { + ans += tmp[i].first - tmp[i - 1].first; + } + } + + repv(i, r) { + tmp.emplace_back(i, 2); + } + + sortv(tmp); + + int prev = -1; + bool ry = false; + bool by = false; + rep(i, tmp.size()) { + if (tmp[i].second == 1) { + if (prev == -1 || (!ry && !by)) { + prev = i; + ry = false; + by = false; + continue; + } + + if (ry && by) { + int prevr = -1; + int prevb = -1; + ll maxr = 0; + ll maxb = 0; + + nrep(j, prev + 1, i) { + if (tmp[j].second == 2) { + if (prevr == -1) { + rmax(maxr, tmp[j].first - tmp[prev].first); + } else { + rmax(maxr, tmp[j].first - tmp[prevr].first); + } + prevr = j; + continue; + } + + if (prevb == -1) { + rmax(maxb, tmp[j].first - tmp[prev].first); + } else { + rmax(maxb, tmp[j].first - tmp[prevb].first); + } + prevb = j; + } + + rmax(maxr, tmp[i].first - tmp[prevr].first); + rmax(maxb, tmp[i].first - tmp[prevb].first); + + // cout << maxr << ' ' << maxb << ' ' << tmp[i].first - tmp[prev].first << '\n'; + ans -= max(maxr + maxb, tmp[i].first - tmp[prev].first); + + prev = i; + ry = false; + by = false; + continue; + } + + ll maximal = 0; + nrep(j, prev + 1, i + 1) { + if (i > 0) { + rmax(maximal, tmp[j].first - tmp[j - 1].first); + } + } + + ans -= maximal; + prev = i; + ry = false; + by = false; + continue; + } + + if (tmp[i].second == 2) { + ry = true; + continue; + } + + by = true; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 43 (Rated for Div. 2)/A. Minimum Binary Number.cpp b/Educational Codeforces Round 43 (Rated for Div. 2)/A. Minimum Binary Number.cpp new file mode 100644 index 0000000..ab016c9 --- /dev/null +++ b/Educational Codeforces Round 43 (Rated for Div. 2)/A. Minimum Binary Number.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/contest/976/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + string a; + cin >> a; + + bool one = false; + string ans; + rep(i, n) { + if (a[i] == '1') { + one = true; + continue; + } + ans.push_back('0'); + } + + if (one) { + cout << '1'; + } + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 43 (Rated for Div. 2)/B. Lara Croft and the New Game.cpp b/Educational Codeforces Round 43 (Rated for Div. 2)/B. Lara Croft and the New Game.cpp new file mode 100644 index 0000000..702872d --- /dev/null +++ b/Educational Codeforces Round 43 (Rated for Div. 2)/B. Lara Croft and the New Game.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/contest/976/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, m, k; + cin >> n >> m >> k; + + if (k < n) { + cout << k + 1 << ' ' << 1 << '\n'; + return 0; + } + + k -= n; + + ll loop = (m - 1) * 2; + ll lin = n - k / (m - 1); + ll col = k % loop; + + // cout << lin << ' ' << col << '\n'; + + if (col < m - 1) { + cout << lin << ' ' << col + 2 << '\n'; + return 0; + } + + cout << lin << ' ' << m - col % (m - 1) << '\n'; +} diff --git a/Educational Codeforces Round 43 (Rated for Div. 2)/C. Nested Segments.cpp b/Educational Codeforces Round 43 (Rated for Div. 2)/C. Nested Segments.cpp new file mode 100644 index 0000000..067fa4b --- /dev/null +++ b/Educational Codeforces Round 43 (Rated for Div. 2)/C. Nested Segments.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/contest/976/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> fds(n); + rep(i, n) { + auto &[l, r, j] = fds[i]; + cin >> l >> r; + j = i; + } + + sort(all(fds), [&](tuple &a, tuple &b){ + auto [li, ri, _] = a; + auto [lj, rj, __] = b; + if (li == lj) { + return ri > rj; + } + return li < lj; + }); + + vvi sparse(25, vi(n)); + rep(i, n) { + sparse[0][i] = get<1>(fds[i]); + } + + nrep(j, 1, 25) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto query = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return min(sparse[log][l], sparse[log][r - (1 << log) + 1]); + }; + + int l = -1; + int r = -1; + + rep(i, n - 1) { + auto [li, ri, ii] = fds[i]; + if (query(i + 1, n - 1) <= ri) { + nrep(j, i + 1, n) { + auto [lj, rj, ij] = fds[j]; + if (lj >= li && rj <= ri) { + l = ij + 1; + r = ii + 1; + break; + } + } + break; + } + } + + cout << l << ' ' << r << '\n'; +} diff --git a/Educational Codeforces Round 43 (Rated for Div. 2)/D. Degree Set.cpp b/Educational Codeforces Round 43 (Rated for Div. 2)/D. Degree Set.cpp new file mode 100644 index 0000000..199e0f6 --- /dev/null +++ b/Educational Codeforces Round 43 (Rated for Div. 2)/D. Degree Set.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/976/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int size; + cin >> size; + + vi fds(size); + cin >> fds; + + int n = fds.back() + 1; + + vi ans(n, fds.back()); + rep(i, fds.size()) { + ans[i] = fds[fds.size() - i - 1]; + } + nrep(i, fds.size(), n) { + ans[i] = fds[0]; + } + + V> edges; + int valid = n - 1; + + rep(i, n) { + if (ans[i] == 0) { + break; + } + + int now = valid; + while (ans[i] != 0) { + ans[i]--; + ans[now]--; + edges.emplace_back(i + 1, now + 1); + if (ans[now] == 0) { + valid = now - 1; + } + now--; + } + } + + cout << edges.size() << '\n'; + repv(i, edges) { + cout << i.first << ' ' << i.second << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/A. Disjoint Sets Union.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/A. Disjoint Sets Union.cpp new file mode 100644 index 0000000..c7c8021 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/A. Disjoint Sets Union.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/1/practice/contest/289390/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + dsu[find_p(b)] = find_p(a); + }; + + while (m--) { + string op; + cin >> op; + int a, b; + cin >> a >> b; + a--, b--; + + if (op[0] == 'u') { + join(a, b); + continue; + } + + cout << (find_p(a) == find_p(b) ? "YES\n" : "NO\n"); + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/B. Disjoint Sets Union 2.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/B. Disjoint Sets Union 2.cpp new file mode 100644 index 0000000..dff4d73 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/B. Disjoint Sets Union 2.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/1/practice/contest/289390/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + vi sizes(n, 1); + vi maximal(n); + vi minimal(n); + rep(i, n) { + dsu[i] = i; + maximal[i] = i + 1; + minimal[i] = i + 1; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return; + } + + rmax(maximal[a], maximal[b]); + rmin(minimal[a], minimal[b]); + sizes[a] += sizes[b]; + dsu[b] = a; + }; + + while (m--) { + string op; + cin >> op; + + int a; + cin >> a; + a--; + + if (op[0] == 'u') { + int b; + cin >> b; + b--; + join(a, b); + continue; + } + + a = find_p(a); + + cout << minimal[a] << ' ' << maximal[a] << ' ' << sizes[a] << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/D. Cutting a graph.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/D. Cutting a graph.cpp new file mode 100644 index 0000000..86362cd --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/D. Cutting a graph.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/1/practice/contest/289390/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + + while (m--) { + int a, b; + cin >> a >> b; + } + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + dsu[find_p(b)] = find_p(a); + }; + + V> query(k); + V ans; + + while (k--) { + string a; + int b, c; + cin >> a >> b >> c; + + if (a[0] == 'a') { + query[k] = {0, b - 1, c - 1}; + continue; + } + + query[k] = {1, b - 1, c - 1}; + } + + for (auto [op, a, b] : query) { + if (op == 0) { + ans.emplace_back((find_p(a) == find_p(b) ? "YES\n" : "NO\n")); + continue; + } + + join(a, b); + } + + for (int i = ans.size() - 1; i >= 0; i--) { + cout << ans[i]; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/E. Monkeys.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/E. Monkeys.cpp new file mode 100644 index 0000000..13ae38c --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/E. Monkeys.cpp @@ -0,0 +1,180 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/1/practice/contest/289390/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V> graph(n); + + rep(i, n) { + int a, b; + cin >> a >> b; + + graph[i] = {a - 1, b - 1}; + } + + V> edges(m); + + while (m--) { + int i, p; + cin >> i >> p; + i--, p--; + + edges[m] = {i, p, graph[i][p]}; + graph[i][p] = -1; + } + + vi dsu(n); + V> nodes(n); + rep(i, n) { + dsu[i] = i; + nodes[i].insert(i); + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return; + } + + if (a > b) { + swap(a, b); + } + + if (nodes[a].size() < nodes[b].size()) { + swap(nodes[a], nodes[b]); + } + + repv(i, nodes[b]) { + nodes[a].insert(i); + } + nodes[b].clear(); + + dsu[b] = a; + }; + + rep(i, n) { + if(graph[i][0] >= 0) { + join(i, graph[i][0]); + } + + if (graph[i][1] >= 0) { + join(i, graph[i][1]); + } + } + + vi ans(n, -1); + + rep(k, edges.size()) { + auto [i, p, j] = edges[k]; + + i = find_p(i); + j = find_p(j); + + if (i != j) { + if (i > j) { + swap(i, j); + } + + if (i == 0) { + repv(l, nodes[j]) { + ans[l] = edges.size() - k - 1; + } + } + + join(i, j); + } + } + + repv(i, ans) { + cout << i << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/A. People are leaving.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/A. People are leaving.cpp new file mode 100644 index 0000000..b215336 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/A. People are leaving.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n + 1); + rep(i, n + 1) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a < b) { + swap(a, b); + } + + dsu[b] = a; + }; + + while (m--) { + char op; + int x; + cin >> op >> x; + x--; + + if (op == '-') { + join(x, x + 1); + continue; + } + + x = find_p(x); + if (x == n) { + cout << "-1\n"; + continue; + } + + cout << x + 1 << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/B. Parking.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/B. Parking.cpp new file mode 100644 index 0000000..662837b --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/B. Parking.cpp @@ -0,0 +1,115 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + dsu[find_p(a)] = find_p(b); + }; + + vi ans(n); + + rep(i, n) { + int x; + cin >> x; + x--; + + x = find_p(x); + ans[i] = x + 1; + join(x, (x + 1) * ((x + 1) < n)); + } + + cout << ans; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/C. Restructuring Company.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/C. Restructuring Company.cpp new file mode 100644 index 0000000..60c41c5 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/C. Restructuring Company.cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vi dsui(n); + vi dsup(n); + rep(i, n) { + dsui[i] = i; + dsup[i] = i; + } + + function find_p = [&](vi &dsu, int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu, dsu[i]); + }; + + auto join = [&](vi &dsu, int a, int b) { + a = find_p(dsu, a); + b = find_p(dsu, b); + if (a < b) { + swap(a, b); + } + + dsu[b] = a; + }; + + while (q--) { + int op; + cin >> op; + + if (op == 1) { + int a, b; + cin >> a >> b; + a--, b--; + + join(dsui, a, b); + continue; + } + + if (op == 2) { + int x, y; + cin >> x >> y; + x--, y--; + + while (x = find_p(dsup, x), x < y) { + join(dsup, x, x + 1); + join(dsui, x, x + 1); + } + continue; + } + + int a, b; + cin >> a >> b; + a--, b--; + + if (find_p(dsui, a) == find_p(dsui, b) || find_p(dsup, a) == find_p(dsup, b)) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/D. Bosses.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/D. Bosses.cpp new file mode 100644 index 0000000..21bff9f --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/D. Bosses.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + vi ans(n); + + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + + if (dsu[dsu[i]] != dsu[i]) { + int prev = dsu[i]; + int tmp = find_p(dsu[i]); + ans[i] += ans[prev] + 1; + dsu[i] = tmp; + } + + return dsu[i]; + }; + + auto join = [&](int a, int b) { + dsu[find_p(a)] = find_p(b); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int a, b; + cin >> a >> b; + a--, b--; + join(a, b); + continue; + } + + int a; + cin >> a; + a--; + + int p = find_p(a); + cout << (p == a ? 0 : ans[a] + 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/E. Spanning Tree.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/E. Spanning Tree.cpp new file mode 100644 index 0000000..6465d36 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/E. Spanning Tree.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V> edges(m); + for (auto &[c, u, v] : edges) { + cin >> u >> v >> c; + u--, v--; + } + + sortv(edges); + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + dsu[b] = a; + return a != b; + }; + + ll ans = 0; + for (auto [c, u, v] : edges) { + if (join(u, v)) { + ans += c; + } + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/F. Dense spanning tree.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/F. Dense spanning tree.cpp new file mode 100644 index 0000000..1a1a96d --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/F. Dense spanning tree.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/problem/F */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + V> edges(m); + for (auto &[c, u, v] : edges) { + cin >> u >> v >> c; + u--, v--; + } + + vi dsu(n); + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + dsu[b] = a; + + return a != b; + }; + + sortv(edges); + ll oo = INT64_MAX >> 1; + + auto getans = [&]() { + ll ans = oo; + + rep(i, m) { + rep(j, n) { + dsu[j] = j; + } + + ll minimal = oo; + ll maximal = -oo; + + int num = n - 1; + + int j = i; + while (j < m && num > 0) { + auto [c, u, v] = edges[j]; + if (join(u, v)) { + rmin(minimal, c); + rmax(maximal, c); + num--; + } + j++; + } + + if (num > 0) { + return ans; + } + + rmin(ans, abs(maximal - minimal)); + } + + return ans; + }; + + ll ans = getans(); + if (ans == oo) { + cout << "NO\n"; + return 0; + } + + cout << "YES\n" << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/G. No refuel.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/G. No refuel.cpp new file mode 100644 index 0000000..5182f30 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/G. No refuel.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/problem/G */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + V> edges(k); + + for (auto &[c, u, v] : edges) { + cin >> u >> v >> c; + u--, v--; + } + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + dsu[b] = a; + + return a != b; + }; + + sortv(edges); + int ans = 0; + for (auto [c, u, v] : edges) { + if (join(u, v)) { + ans = c; + } + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/H. Oil business.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/H. Oil business.cpp new file mode 100644 index 0000000..3688eaf --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/H. Oil business.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/problem/H */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + ll s; + cin >> n >> m >> s; + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + dsu[b] = a; + + return a != b; + }; + + V> edges(m); + rep(i, m) { + auto &[c, j, u, v] = edges[i]; + cin >> u >> v >> c; + u--, v--; + j = i; + } + + sort(all(edges), greater<>()); + + V> potential; + for (auto [c, i, u, v] : edges) { + if (!join(u, v)) { + potential.emplace_back(c, i); + } + } + + rep(i, n) { + dsu[i] = i; + } + reverse(all(potential)); + + vi ans; + ll total = 0; + + for (auto [c, i] : potential) { + if (total + c > s) { + break; + } + + ans.push_back(i + 1); + total += c; + } + + cout << ans.size() << '\n'; + rep(i, ans.size()) { + cout << ans[i] << " \n"[i == ans.size() - 1]; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/I. Bipartite Graph.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/I. Bipartite Graph.cpp new file mode 100644 index 0000000..d318ee1 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/I. Bipartite Graph.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/problem/I */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + vi depth(n); + + function(int)> find_p = [&](int i) -> pair{ + if (dsu[i] == i) { + return {i, 0}; + } + + auto ans = find_p(dsu[i]); + dsu[i] = ans.first; + depth[i] ^= ans.second; + + return {dsu[i], depth[i]}; + }; + + auto join = [&](int a, int b) { + auto ap = find_p(a); + auto bp = find_p(b); + + dsu[bp.first] = ap.first; + depth[bp.first] = (1 ^ bp.second ^ ap.second); + }; + + int shift = 0; + function func[] = { + [&](int a, int b) { + join(a, b); + }, + [&](int a, int b) { + find_p(a); + find_p(b); + + int ca = depth[a]; + int cb = depth[b]; + + if (ca == cb) { + cout << "YES\n"; + shift++; + return; + } + + cout << "NO\n"; + } + }; + + while (m--) { + int op, a, b; + cin >> op >> a >> b; + a = (a + shift) % n; + b = (b + shift) % n; + func[op](a, b); + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp index e2c2dab..5d4d555 100644 --- a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp @@ -80,5 +80,102 @@ int main() ios::sync_with_stdio(false); cin.tie(nullptr); + int n, m; + cin >> n >> m; + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazyconst(n * 2); + vl lazyprog(n * 2); + + auto propagateconst = [&](int i) { + if (lazyconst[i] == 0) { + return; + } + + if (i >= n) { + seg[i] += lazyconst[i]; + lazyconst[i] = 0; + return; + } + + lazyconst[i * 2] += lazyconst[i]; + lazyconst[i * 2 + 1] += lazyconst[i]; + lazyconst[i] = 0; + }; + + auto propagateprog = [&](int i) { + if (lazyprog[i] == 0) { + return; + } + + if (i >= n) { + seg[i] += lazyprog[i] * (i - n + 1); + lazyprog[i] = 0; + return; + } + + lazyprog[i * 2] += lazyprog[i]; + lazyprog[i * 2 + 1] += lazyprog[i]; + lazyprog[i] = 0; + }; + + auto propagate = [&](int i) { + propagateconst(i); + propagateprog(i); + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll c, ll p) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazyconst[i] += c; + lazyprog[i] += p; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, c, p); + update(i * 2 + 1, mid + 1, r, tl, tr, c, p); + }; + + function query = [&](int i, int l, int r, int t) { + propagate(i); + + if (i >= n) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + if (mid >= t) { + return query(i * 2, l, mid, t); + } + + return query(i * 2 + 1, mid + 1, r, t); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + ll l, r, a, d; + cin >> l >> r >> a >> d; + update(1, 0, n - 1, l - 1, r - 1, a - d * l, d); + continue; + } + + int i; + cin >> i; + cout << query(1, 0, n - 1, i - 1) << '\n'; + } } diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/C. Painter.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/C. Painter.cpp new file mode 100644 index 0000000..5bece8e --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/C. Painter.cpp @@ -0,0 +1,189 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/4/practice/contest/280801/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int middle = 500000; + + int size = 1 << (32 - __builtin_clz(1e6 + middle + 1)); + + struct node { + int sum; + int seg; + bool suf; + bool pref; + bool total; + + node() = default; + }; + + V seg(size * 2); + V upd(size * 2); + vi lazy(size * 2); + + auto merge = [&](node &a, node &b) -> node { + node ans; + ans.sum = a.sum + b.sum; + + if (a.total && b.total) { + ans.total = true; + ans.pref = true; + ans.suf = true; + ans.seg = 1; + + return ans; + } + + ans.seg = a.seg + b.seg - (a.suf && b.pref); + ans.pref = a.pref; + ans.suf = b.suf; + ans.total = false; + + return ans; + }; + + auto propagate = [&](int i, int l, int r) { + if (!upd[i]) { + return; + } + + upd[i] = false; + + seg[i].sum = lazy[i] * (r - l + 1); + seg[i].pref = lazy[i]; + seg[i].suf = lazy[i]; + seg[i].total = lazy[i]; + seg[i].seg = lazy[i]; + + if (i < size) { + lazy[i * 2] = lazy[i]; + lazy[i * 2 + 1] = lazy[i]; + upd[i * 2] = true; + upd[i * 2 + 1] = true; + } + + return; + }; + + function update = [&](int i, int l, int r, int tl, int tr, int v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = v; + upd[i] = true; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = merge(seg[i * 2], seg[i * 2 + 1]); + }; + + auto query = [&]() { + propagate(1, 0, size - 1); + + return seg[1]; + }; + + rep(i, n) { + char op; + int x, l; + cin >> op >> x >> l; + x += middle; + + op = op == 'B'; + + update(1, 0, size - 1, x, x + l - 1, op); + + auto ans = query(); + cout << ans.seg << ' ' << ans.sum << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/D. Problem About Weighted Sum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/D. Problem About Weighted Sum.cpp new file mode 100644 index 0000000..8e55b0d --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/D. Problem About Weighted Sum.cpp @@ -0,0 +1,182 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/4/practice/contest/280801/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi a(n); + cin >> a; + + while (__builtin_popcount(n) != 1) { + n++; + a.push_back(0); + } + + V> seg(n * 2); + vl lazy(n * 2); + + auto merge = [&](pair a, pair b) -> pair { + return {a.first + b.first, a.second + b.second}; + }; + + nrep(i, n, n * 2) { + seg[i] = {a[i - n], a[i - n] * (i - n + 1)}; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = merge(seg[i * 2], seg[i * 2 + 1]); + } + + auto propagate = [&](int i, int l, int r) { + if (lazy[i] == 0) { + return; + } + + ll size = (r - l + 1); + + seg[i].first += lazy[i] * size; + seg[i].second += lazy[i] * (size * (r + l + 2) / 2); + + if (i < n) { + lazy[i * 2] += lazy[i]; + lazy[i * 2 + 1] += lazy[i]; + } + + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, int v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = merge(seg[i * 2], seg[i * 2 + 1]); + }; + + 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, l, r); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return merge(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r, d; + cin >> l >> r >> d; + update(1, 0, n - 1, l - 1, r - 1, d); + continue; + } + + int l, r; + cin >> l >> r; + + auto ans = query(1, 0, n - 1, l - 1, r - 1); + + cout << ans.second - ans.first * (l - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/E. Wall.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/E. Wall.cpp new file mode 100644 index 0000000..200b939 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/E. Wall.cpp @@ -0,0 +1,181 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/4/practice/contest/280801/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + int prev = n; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + V> lazy(n * 2, {0, 1e5}); + V upd(n * 2); + + auto propagate = [&](int i) { + if (!upd[i]) { + return; + } + + upd[i] = false; + + rmax(seg[i], lazy[i].first); + rmin(seg[i], lazy[i].second); + + if (i < n) { + rmax(lazy[i * 2].first, lazy[i].first); + rmin(lazy[i * 2].second, lazy[i].second); + if (lazy[i * 2].first > lazy[i * 2].second) { + if (lazy[i * 2].first == lazy[i].first) { + lazy[i * 2].second = lazy[i * 2].first; + } else { + lazy[i * 2].first = lazy[i * 2].second; + } + } + upd[i * 2] = true; + rmax(lazy[i * 2 + 1].first, lazy[i].first); + rmin(lazy[i * 2 + 1].second, lazy[i].second); + if (lazy[i * 2 + 1].first > lazy[i * 2 + 1].second) { + if (lazy[i * 2 + 1].first == lazy[i].first) { + lazy[i * 2 + 1].second = lazy[i * 2 + 1].first; + } else { + lazy[i * 2 + 1].first = lazy[i * 2 + 1].first; + } + } + upd[i * 2 + 1] = true; + } + + lazy[i] = {0, 1e5}; + }; + + function update = [&](int i, int l, int r, int tl, int tr, int t, ll v) { + propagate(i); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + if (t) { + lazy[i].second = v; + } else { + lazy[i].first = v; + } + upd[i] = true; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, t, v); + update(i * 2 + 1, mid + 1, r, tl, tr, t, v); + }; + + function query = [&](int i, int l, int r, int t) { + propagate(i); + + if (i >= n) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + if (mid >= t) { + return query(i * 2, l, mid, t); + } + return query(i * 2 + 1, mid + 1, r, t); + }; + + while (k--) { + int op, l, r, h; + cin >> op >> l >> r >> h; + + update(1, 0, n - 1, l, r, op - 1, h); + } + + rep(i, prev) { + cout << query(1, 0, n - 1, i) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/F. Mountain.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/F. Mountain.cpp new file mode 100644 index 0000000..ec88cc7 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/F. Mountain.cpp @@ -0,0 +1,426 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/4/practice/contest/280801/problem/F */ + +#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; +} + +class segtree_arithmethic { + struct node { + int val; + int left; + int right; + }; + + struct lazynode { + int c; + int x; + int xi; + bool cupd; + bool xupd; + }; + + V seg; + V lazy; + int n; + + int newnode() { + lazy.emplace_back(0LL, 0LL, 0LL, false, false); + seg.emplace_back(0LL, -1, -1); + + return seg.size() - 1; + } + + void createchildren(int i) { + if (seg[i].left == -1) { + seg[i].left = newnode(); + } + + if (seg[i].right == -1) { + seg[i].right = newnode(); + } + } + + void propagatear(int i, int l, int r) { + if (!lazy[i].xupd) { + return; + } + + seg[i].val = lazy[i].xi + lazy[i].x * (r - l); + + if (l != r) { + createchildren(i); + + int left = seg[i].left; + int right = seg[i].right; + + int mid = (l + r) >> 1; + + lazy[left].x = lazy[i].x; + lazy[left].c = 0; + lazy[left].xi = lazy[i].xi; + lazy[left].cupd = false; + lazy[left].xupd = true; + + lazy[right].x = lazy[i].x; + lazy[right].c = 0; + lazy[right].xi = lazy[i].xi + lazy[i].x * (mid - l + 1); + lazy[right].cupd = false; + lazy[right].xupd = true; + } + + lazy[i].xupd = false; + }; + + void propagatec(int i, int l, int r) { + if (!lazy[i].cupd) { + return; + } + + seg[i].val += lazy[i].c; + + if (l != r) { + createchildren(i); + + int left = seg[i].left; + int right = seg[i].right; + + lazy[left].c += lazy[i].c; + lazy[left].cupd = true; + + lazy[right].c += lazy[i].c; + lazy[right].cupd = true; + } + + lazy[i].cupd = false; + lazy[i].c = 0; + } + + void propagate(int i, int l, int r) { + propagatear(i, l, r); + propagatec(i, l, r); + } + + int x; + int tl, tr; + + void updatearithmethic(int i, int l, int r, int xi) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i].x = x; + lazy[i].xi = xi; + lazy[i].xupd = true; + lazy[i].c = 0; + lazy[i].cupd = false; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + createchildren(i); + + updatearithmethic(seg[i].left, l, mid, xi); + updatearithmethic(seg[i].right, mid + 1, r, xi + x * (mid - l + 1)); + + seg[i].val = max(seg[seg[i].left].val, seg[seg[i].right].val); + } + + int c; + + void updateconst(int i, int l, int r) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i].c += c; + lazy[i].cupd = true; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + createchildren(i); + + updateconst(seg[i].left, l, mid); + updateconst(seg[i].right, mid + 1, r); + + seg[i].val = max(seg[seg[i].left].val, seg[seg[i].right].val); + } + + int h; + + ll query(int i, int l, int r) { + propagate(i, l, r); + + if (l == r) { + return seg[i].val > h ? -1 : l; + } + + int mid = (l + r) >> 1; + + createchildren(i); + propagate(seg[i].left, l, mid); + propagate(seg[i].right, mid + 1, r); + + if (seg[seg[i].left].val > h) { + return query(seg[i].left, l, mid); + } + int res = query(seg[i].right, mid + 1, r); + return res == -1 ? mid : res; + } + + public: + segtree_arithmethic(int n): n(n) { + seg.emplace_back(0LL, -1, -1); + lazy.emplace_back(); + } + + void updatearithmethic(int l, int r, ll x) { + this->x = x; + tl = l; + tr = r; + updatearithmethic(0, 0, n - 1, x * (-l + 1)); + } + + void updateconst(int l, int r, ll c) { + this->c = c; + tl = l; + tr = r; + updateconst(0, 0, n - 1); + } + + ll query(int h) { + this->h = h; + return query(0, 0, n - 1); + } +}; + +class segtree_sum { + struct node { + int val; + int left; + int right; + }; + + struct lazynode { + int val; + bool upd; + }; + + V seg; + V lazy; + int n; + + int newnode() { + seg.emplace_back(0LL, -1, -1); + lazy.emplace_back(0LL, false); + + return seg.size() - 1; + } + + void createchildren(int i) { + if (seg[i].left == -1) { + seg[i].left = newnode(); + } + + if (seg[i].right == -1) { + seg[i].right = newnode(); + } + } + + void propagate(int i, int l, int r) { + if (!lazy[i].upd) { + return; + } + + seg[i].val = lazy[i].val * (r - l + 1); + + if (l != r) { + createchildren(i); + lazy[seg[i].left] = lazy[i]; + lazy[seg[i].right] = lazy[i]; + } + + lazy[i].upd = false; + } + + int tl, tr; + int v; + + void upd(int i, int l, int r) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = {v, true}; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + createchildren(i); + + upd(seg[i].left, l, mid); + upd(seg[i].right, mid + 1, r); + + seg[i].val = seg[seg[i].left].val + seg[seg[i].right].val; + } + + ll query(int i, int l, int r) { + if (l > tr || r < tl || i == -1) { + return 0LL; + } + + propagate(i, l, r); + + if (l >= tl && r <= tr) { + return seg[i].val; + } + + int mid = (l + r) >> 1; + + return query(seg[i].left, l, mid) + query(seg[i].right, mid + 1, r); + } + + public: + segtree_sum(int n): n(n) { + seg.emplace_back(0LL, -1, -1); + lazy.emplace_back(0LL, false); + } + + void update(int l, int r, int d) { + tl = l; + tr = r; + v = d; + upd(0, 0, n - 1); + } + + ll query(int l, int r) { + tl = l; + tr = r; + return query(0, 0, n - 1); + } +}; + +signed main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + ll prev = n; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n)); + } + + segtree_arithmethic arithmethic(n); + segtree_sum sum(n); + + char op; + while (cin >> op, op != 'E') { + if (op == 'I') { + int a, b, d; + cin >> a >> b >> d; + + arithmethic.updateconst(b + 1, n, -sum.query(a, b)); + arithmethic.updatearithmethic(a, b, d); + arithmethic.updateconst(a, b, sum.query(0, a - 1)); + sum.update(a, b, d); + arithmethic.updateconst(b + 1, n, sum.query(a, b)); + + continue; + } + + int h; + cin >> h; + + cout << min(arithmethic.query(h), prev) << '\n'; + } +} diff --git a/The 2025 ICPC South America - Brazil First Phase/H. How many teams?.cpp b/The 2025 ICPC South America - Brazil First Phase/H. How many teams?.cpp new file mode 100644 index 0000000..0c3103f --- /dev/null +++ b/The 2025 ICPC South America - Brazil First Phase/H. How many teams?.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/gym/106073/problem/H */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, log; + cin >> n >> log; + + auto comb = [&](ll n, ll k) { + if (n < k) { + return 0LL; + } + + ll ans = 1; + ll div = 1; + rep(i, k) { + ans *= n - i; + div *= (i + 1); + } + + return ans / div; + }; + + vl sos(1 << log); + + auto tobin = [&](string &a) { + int ans = 0; + repv(i, a) { + ans <<= 1; + ans += i - '0'; + } + + return ans; + }; + + vi nums(n); + + rep(i, n) { + string fds; + cin >> fds; + nums[i] = tobin(fds); + sos[nums[i]]++; + } + + rep(i, log) { + rep(j, 1 << log) { + if ((j >> i) & 1) { + sos[j] += sos[j ^ (1 << i)]; + } + } + } + + vl ans(1 << log); + rep(j, 1 << log) { + ans[j] = comb(sos[j], 3); + } + + for (int i = log - 1; i >= 0; i--) { + for (int j = 1 << log; j >= 0; j--) { + if ((j >> i) & 1) { + ans[j] -= ans[j ^ (1 << i)]; + } + } + } + + int q; + cin >> q; + while (q--) { + string fds; + cin >> fds; + int now = tobin(fds); + + cout << ans[now] << '\n'; + } +}