#include<iostream> #include<algorithm> #define lc p << 1 #define rc p << 1 | 1 #define N 200001 usingnamespace std; int n, m, w[N]; int val[N << 2]; voidbuild(int p, int s, int t) { if (s == t) { val[p] = w[s]; return; } int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); val[p] = max(val[lc], val[rc]); } voidupdate(int x, int c, int p, int s, int t) { if (s == x && x == t) { val[p] = c; return; } int m = s + t >> 1; if (x <= m) update(x, c, lc, s, m); else update(x, c, rc, m + 1, t); val[p] = max(val[lc], val[rc]); } intquery(int l, int r, int p, int s, int t) { if (l <= s && t <= r) return val[p]; int m = s + t >> 1; int res = 0; if (l <= m) res = max(res, query(l, r, lc, s, m)); if (r > m) res = max(res, query(l, r, rc, m + 1, t)); return res; } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); while (cin >> n >> m) { for (int i = 1; i <= n; ++i) cin >> w[i]; build(1, 1, n); string op; int i, j; while (m--) { cin >> op >> i >> j; if (op[0] == 'Q') cout << query(i, j, 1, 1, n) << endl; else update(i, j, 1, 1, n); } } return0; }
#include<iostream> #include<algorithm> #define lowbit(x) (x & -x) #define N 200001 usingnamespace std; int n, m, w[N], tr[N]; voidupdate(int i) { for (; i <= n; i += lowbit(i)) { tr[i] = w[i]; int l = lowbit(i); for (int j = 1; j < l; j <<= 1) tr[i] = max(tr[i], tr[i - j]); } } intquery(int l, int r) { int ans = 0; while (r >= l) { ans = max(ans, w[r]); for (--r; r - lowbit(r) >= l; r -= lowbit(r)) ans = max(ans, tr[r]); } return ans; } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); while (cin >> n >> m) { for (int i = 1; i <= n; ++i) tr[i] = 0; for (int i = 1, x; i <= n; ++i) { cin >> w[i]; update(i); } string op; int i, j; while (m--) { cin >> op >> i >> j; if (op[0] == 'Q') cout << query(i, j) << endl; else w[i] = j, update(i); } } return0; }
#include<iostream> #define lc p << 1 #define rc p << 1 | 1 #define N 100001 #define ll long long usingnamespace std; int n, q, w[N]; ll val[N << 2], hold[N << 2]; voidbuild(int p, int s, int t) { val[p] = w[s], hold[p] = 0; if (s == t) return; int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); val[p] = val[lc] + val[rc]; } voidpushdown(int p, int s, int t) { if (hold[p]) { int m = s + t >> 1; val[lc] += hold[p] * (m - s + 1); val[rc] += hold[p] * (t - m); hold[lc] += hold[p]; hold[rc] += hold[p]; hold[p] = 0; } } voidupdate(int l, int r, int c, int p, int s, int t) { if (l <= s && t <= r) { val[p] += (t - s + 1) * c; hold[p] += c; return; } int m = t + s >> 1; pushdown(p, s, t); if (l <= m) update(l, r, c, lc, s, m); if (r > m) update(l, r, c, rc, m + 1, t); val[p] = val[lc] + val[rc]; } ll query(int l, int r, int p, int s, int t) { if (l <= s && t <= r) return val[p]; int m = s + t >> 1; pushdown(p, s, t); ll res = 0; if (l <= m) res += query(l, r, lc, s, m); if (r > m) res += query(l, r, rc, m + 1, t); return res; } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> q; for (int i = 1; i <= n; ++i) cin >> w[i]; build(1, 1, n); string op; int l, r, c; while (q--) { cin >> op >> l >> r;
#include<cstring> #include<iostream> #include<vector> #include<algorithm> #define lc p << 1 #define rc p << 1 | 1 #define N 10000 usingnamespace std; int t, n; pair<int, int> pos[N]; vector<int> s; bool val[N << 3]; // covered. intget(int x) { returnlower_bound(s.begin(), s.end(), x) - s.begin(); } voidbuild(int p, int s, int t) { val[p] = 0; if (s == t) return; int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); } boolupdate(int l, int r, int p, int s, int t) { if (val[p]) return0; if (t == s) return val[p] = 1; bool res = 0; int m = s + t >> 1; if (l <= m) res |= update(l, r, lc, s, m); if (r > m) res |= update(l, r, rc, m + 1, t); if (val[lc] && val[rc]) // pushup. val[p] = 1; return res; } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> t; while (t--) { s.clear(); memset(val, 0, sizeof val); cin >> n; for (int i = 0, l, r; i < n; ++i) { cin >> l >> r; ++r; pos[i] = {l, r}; s.push_back(l); s.push_back(r); } sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); int len = s.size() - 2; build(1, 0, len); int ans = 0; for (int i = n - 1, l, r; i >= 0; --i) { l = get(pos[i].first), r = get(pos[i].second) - 1; if (update(l, r, 1, 0, len)) ++ans; } cout << ans << endl; } return0; }
#include<iostream> #define lc p << 1 #define rc p << 1 | 1 #define N 100001 usingnamespace std; int t, n, q, val[N << 2], hold[N << 2]; voidbuild(int p, int s, int t) { val[p] = 1, hold[p] = 0; if (s == t) return; int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); val[p] = val[lc] + val[rc]; } voidpushdown(int p, int s, int t) { if (hold[p] && s != t) { int m = s + t >> 1; val[lc] = hold[p] * (m - s + 1); val[rc] = hold[p] * (t - m); hold[lc] = hold[p]; hold[rc] = hold[p]; hold[p] = 0; } } voidupdate(int l, int r, int c, int p, int s, int t) { if (l <= s && t <= r) { val[p] = (t - s + 1) * c; hold[p] = c; return; } pushdown(p, s, t); int m = s + t >> 1; if (l <= m) update(l, r, c, lc, s, m); if (r > m) update(l, r, c, rc, m + 1, t); val[p] = val[lc] + val[rc]; } intquery(int l, int r, int p, int s, int t) { if (l <= s && t <= r) return val[p]; pushdown(p, s, t); int m = s + t >> 1, res = 0; if (l <= m) res += query(l, r, lc, s, m); if (r > m) res += query(l, r, rc, m + 1, t); return res; } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> t; for (int _ = 1; _ <= t; ++_) { cin >> n >> q; build(1, 1, n); int x, y, z; while (q--) { cin >> x >> y >> z; update(x, y, z, 1, 1, n); } cout << "Case " << _ << ": The total value of the hook is " << query(1, n, 1, 1, n) << "." << endl; } return0; }
#include<iostream> #include<vector> #include<map> #define lc p << 1 #define rc p << 1 | 1 #define N 8000 usingnamespace std; int n, val[(N + 1) << 2]; map<int, vector<int>> m; voidbuild(int p, int s, int t) { val[p] = 0; if (s == t) return; int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); } voidpushdown(int p, int s, int t) { if (val[p]) { val[lc] = val[rc] = val[p]; val[p] = 0; } } voidupdate(int l, int r, int c, int p, int s, int t) { if (l <= s && t <= r) { val[p] = c; return; } pushdown(p, s, t); int m = s + t >> 1; if (l <= m) update(l, r, c, lc, s, m); if (r > m) update(l, r, c, rc, m + 1, t); } voidquery(int l, int r, int p, int s, int t) { if (s == t) { int c = val[p]; if (c) { if (m[c].empty()) m[c] = {t}; elseif (m[c].back() + 1 == s) m[c].back() = t; else m[c].push_back(t); } return; } pushdown(p, s, t); int m = s + t >> 1; if (l <= m) query(l, r, lc, s, m); if (r > m) query(l, r, rc, m + 1, t); } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); while (cin >> n) { build(1, 1, N); int l, r, c; while (n--) { cin >> l >> r >> c; update(l + 1, r, c + 1, 1, 1, N); } m.clear(); query(1, N, 1, 1, N); for (auto &x : m) if (x.first != -1) cout << x.first - 1 << " " << x.second.size() << endl; cout << endl; } return0; }
#include<iostream> #include<cmath> #define lc p << 1 #define rc p << 1 | 1 #define N 100001 #define ll long long usingnamespace std; int t, n, m; ll val[N << 2]; voidbuild(int p, int s, int t) { if (s == t) { cin >> val[p]; return; } int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); val[p] = val[lc] + val[rc]; } voidupdate(int l, int r, int p, int s, int t) { if (l <= s && t <= r) { if (val[p] <= t - s + 1) return; } if (s == t) { val[p] = sqrt(val[p]); return; } int m = s + t >> 1; if (l <= m) update(l, r, lc, s, m); if (r > m) update(l, r, rc, m + 1, t); val[p] = val[lc] + val[rc]; } ll query(int l, int r, int p, int s, int t) { if (l <= s && t <= r) return val[p]; int m = s + t >> 1; ll res = 0; if (l <= m) res += query(l, r, lc, s, m); if (r > m) res += query(l, r, rc, m + 1, t); return res; } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); while (cin >> n) { cout << "Case #" << ++t << ":" << endl; build(1, 1, n); cin >> m; for (int i = 0, t, x, y; i < m; ++i) { cin >> t >> x >> y; if (x > y) swap(x, y); if (t) cout << query(x, y, 1, 1, n) << endl; else update(x, y, 1, 1, n); } } return0; }
#include<iostream> #include<stack> #define lc p << 1 #define rc p << 1 | 1 #define N 50001 usingnamespace std; int n, m, len[N << 2], pre[N << 2], suf[N << 2]; stack<int> stk; voidpushup(int p, int s, int t) { pre[p] = pre[lc]; suf[p] = suf[rc]; len[p] = max(max(len[lc], len[rc]), suf[lc] + pre[rc]); int m = s + t >> 1; if (len[lc] == m - s + 1) pre[p] += pre[rc]; if (len[rc] == t - m) suf[p] += suf[lc]; } voidbuild(int p, int s, int t) { len[p] = pre[p] = suf[p] = 1; if (s == t) return; int m = s + t >> 1; build(lc, s, m), build(rc, m + 1, t); pushup(p, s, t); } voidupdate(int x, int c, int p, int s, int t) { if (s == x && x == t) { len[p] = pre[p] = suf[p] = c; return; } int m = s + t >> 1; if (x <= m) update(x, c, lc, s, m); else update(x, c, rc, m + 1, t); pushup(p, s, t); } intquery(int x, int p, int s, int t) { if (s == t) return len[p]; int m = s + t >> 1; if (x > m - suf[lc] // x \in [m - suf[lc] + 1, ...]. && x <= m + pre[rc]) // x \in [..., (m + 1) + pre[rc] - 1]. return suf[lc] + pre[rc]; return x <= m ? query(x, lc, s, m) : query(x, rc, m + 1, t); } intmain() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); while (cin >> n >> m) { build(1, 1, n); string op; int x; while (m--) { cin >> op; if (op[0] == 'D') { cin >> x; stk.push(x); update(x, 0, 1, 1, n); } elseif (op[0] == 'R') { if (!stk.empty()) { x = stk.top(); stk.pop(); update(x, 1, 1, 1, n); } } else { cin >> x; cout << query(x, 1, 1, n) << endl; } } } return0; }