Submission #611784

#TimeUsernameProblemLanguageResultExecution timeMemory
611784tengiz05Election (BOI18_election)C++17
100 / 100
1250 ms53036 KiB
#include <bits/stdc++.h> using i64 = long long; using namespace std; constexpr int inf = 1E9; struct Info { int x; Info(int x = -inf) : x(x) {} }; Info operator+(const Info &a, const Info &b) { return Info(max(a.x, b.x)); } struct Tag { int x; Tag(int x = 0) : x(x) {} }; void apply(Tag &a, const Tag &b) { a.x += b.x; } void apply(Info &a, const Tag &b) { a.x += b.x; } template<class Info, class Tag, class Merge = std::plus<Info>> struct LazySegmentTree { const int n; const Merge merge; std::vector<Info> info; std::vector<Tag> tag; LazySegmentTree(int n) : n(n), merge(Merge()), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {} LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) { std::function<void(int, int, int)> build = [&](int p, int l, int r) { if (r - l == 1) { info[p] = init[l]; return; } int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m, r); pull(p); }; build(1, 0, n); } void pull(int p) { info[p] = merge(info[2 * p], info[2 * p + 1]); } void apply(int p, const Tag &v) { ::apply(info[p], v); ::apply(tag[p], v); } void push(int p) { apply(2 * p, tag[p]); apply(2 * p + 1, tag[p]); tag[p] = Tag(); } void modify(int p, int l, int r, int x, Info v) { if (r - l == 1) { info[p] = v; return; } int m = (l + r) / 2; push(p); if (x < m) { modify(2 * p, l, m, x, v); } else { modify(2 * p + 1, m, r, x, v); } pull(p); } void modify(int x, Info v) { modify(1, 0, n, x, v); } Info rangeQuery(int p, int l, int r, int x, int y) { if (l >= y || r <= x) { return Info(); } if (l >= x && r <= y) { return info[p]; } int m = (l + r) / 2; push(p); return merge(rangeQuery(2 * p, l, m, x, y), rangeQuery(2 * p + 1, m, r, x, y)); } Info rangeQuery(int l, int r) { return rangeQuery(1, 0, n, l, r); } void rangeApply(int p, int l, int r, int x, int y, const Tag &v) { if (l >= y || r <= x) { return; } if (l >= x && r <= y) { apply(p, v); return; } int m = (l + r) / 2; push(p); rangeApply(2 * p, l, m, x, y, v); rangeApply(2 * p + 1, m, r, x, y, v); pull(p); } void rangeApply(int l, int r, const Tag &v) { return rangeApply(1, 0, n, l, r, v); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string S; cin >> S; int q; cin >> q; vector<vector<pair<int, int>>> Q(n); vector<int> ans(q); for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; r--; Q[l].emplace_back(r, i); } stack<int> s; LazySegmentTree<Info, Tag> res(n), f(n); for (int i = 0; i < n; i++) { res.modify(i, Info(0)); f.modify(i, Info(0)); } for (int i = n - 1; i >= 0; i--) { if (S[i] == 'T') { res.rangeApply(i, n, 1); s.push(i); } else { if (!s.empty()) { res.rangeApply(s.top(), n, -1); f.rangeApply(s.top(), n, -1); s.pop(); } f.rangeApply(i, n, 1); } for (auto [r, id] : Q[i]) { ans[id] = res.rangeQuery(r, r + 1).x + (f.rangeQuery(i, r + 1).x - f.rangeQuery(r, r + 1).x); } } for (int i = 0; i < q; i++) { cout << ans[i] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...