diff --git a/Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp b/Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp new file mode 100644 index 0000000..35ba141 --- /dev/null +++ b/Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2185/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + int sz = 1 << n; + + vl a(sz); + cin >> a; + + vl an(sz * 2); + + function calc = [&](int i) { + if (i >= sz) { + an[i] = a[i - sz]; + return an[i]; + } + + ll a1 = calc(i * 2); + ll a2 = calc(i * 2 + 1); + + an[i] = a1 ^ a2; + return an[i]; + }; + + calc(1); + + while (q--) { + int b, c; + cin >> b >> c; + + ll ans = 0; + ll cur = a[b - 1]; + bool add = false; + int st = 0; + b += sz; + b--; + + while (b > 1) { + int now = b & 1; + b >>= 1; + + ll cur = an[(b << 1) + (now ^ 1)]; + if (cur > c || (cur == c && now)) { + ans += 1 << st; + } + + st++; + c ^= cur; + } + + cout << ans << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 957 (Div. 3)/F. Valuable Cards.cpp b/Codeforces Round 957 (Div. 3)/F. Valuable Cards.cpp new file mode 100644 index 0000000..3fae126 --- /dev/null +++ b/Codeforces Round 957 (Div. 3)/F. Valuable Cards.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1992/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, x; + cin >> n >> x; + + vi a(n); + cin >> a; + + set ev; + ev.insert(x); + + int ans = 1; + rep(i, n) { + set tmp = ev; + repv(j, ev) { + if (j % a[i] == 0) { + tmp.insert(j / a[i]); + } + } + + if (tmp.count(1)) { + ans++; + ev.clear(); + ev.insert(x); + if (x % a[i] == 0) { + ev.insert(x / a[i]); + } + continue; + } + + swap(ev, tmp); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 976 (Div. 2) and Divide By Zero 9.0/C. Bitwise Balancing.cpp b/Codeforces Round 976 (Div. 2) and Divide By Zero 9.0/C. Bitwise Balancing.cpp new file mode 100644 index 0000000..c8c3c6d --- /dev/null +++ b/Codeforces Round 976 (Div. 2) and Divide By Zero 9.0/C. Bitwise Balancing.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2020/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll b, c, d; + cin >> b >> c >> d; + + ll ans = 0; + rep(i, 61) { + int bi = (b >> i) & 1; + int ci = (c >> i) & 1; + ll di = (d >> i) & 1; + + if (bi && ci) { + ans |= (di^1) << i; + continue; + } + + if (bi && !ci) { + if (!di) { + cout << "-1\n"; + return; + } + continue; + } + + if (!bi && ci) { + if (di) { + cout << "-1\n"; + return; + } + continue; + } + + ans |= di << i; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Hello 2023/C. Least Prefix Sum.cpp b/Hello 2023/C. Least Prefix Sum.cpp new file mode 100644 index 0000000..ba0d6d4 --- /dev/null +++ b/Hello 2023/C. Least Prefix Sum.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1779/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl a(n); + cin >> a; + + m--; + + int ans = 0; + vl pref(n + 1); + + nrep(i, 1, n + 1) { + pref[i] = pref[i - 1] + a[i - 1]; + } + pref[0] = OO; + + priority_queue> pq; + + ll cur = pref[m + 1]; + for (int i = m; i >= 0; i--) { + pq.emplace(a[i], i); + + while (cur > pref[i]) { + auto [c, j] = pq.top(); + pq.pop(); + a[j] = -a[j]; + cur -= c * 2; + ans++; + } + } + + while (!pq.empty()) { + pq.pop(); + } + + pref[0] = 0; + nrep(i, 1, n + 1) { + pref[i] = pref[i - 1] + a[i - 1]; + } + + priority_queue> mpq; + + cur = pref[m + 1]; + nrep(i, m + 1, n) { + cur += a[i]; + mpq.push(a[i]); + + while (cur < pref[m + 1]) { + cur -= mpq.top() * 2; + mpq.pop(); + ans++; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +}