Submission #571986

#TimeUsernameProblemLanguageResultExecution timeMemory
571986ShinElection (BOI18_election)C++14
100 / 100
565 ms46464 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define all(x) x.begin(), x.end() using namespace std; template <class X, class Y> bool minimize(X &a, Y b) { if (a > b) return a = b, true; return false; } template <class X, class Y> bool maximize(X &a, Y b) { if (a < b) return a = b, true; return false; } struct segment_tree { struct Node { int sum, ans; Node(int _sum = 0, int _ans = 0) { sum = _sum; ans = _ans; } Node operator + (const Node &oth) const { return Node(sum + oth.sum, min(oth.ans, ans + oth.sum)); } }; vector<Node> t; int n; segment_tree(int _n) : n(_n) { t.resize((n << 2) + 7); } void modify(int node, int l, int r, int p, int v) { if (l > p || r < p) { return; } if (l == r) { t[node] = Node(v, v); } else { int mid = (l + r) >> 1; modify(node << 1, l, mid, p, v); modify(node << 1|1, mid + 1, r, p, v); t[node] = t[node << 1] + t[node << 1|1]; } } Node get(int node, int l, int r, int u, int v) { if (l > v || r < u) { return Node(0, 0); } if (u <= l && r <= v) { return t[node]; } int mid = (l + r) >> 1; Node L = get(node << 1, l, mid, u, v); Node R = get(node << 1|1, mid + 1, r, u, v); return L + R; } void modify(int p, int v) { modify(1, 1, n, p, v); } int get(int l, int r) { return -get(1, 1, n, l, r).ans; } }; const int N = 5e5 + 7; int n; int a[N]; int res[N]; string s; vector<pair<int, int>> pos[N]; signed main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> s; for (int i = 1; i <= n; i ++) { a[i] = (s[i - 1] == 'C' ? 1 : -1); } int q; cin >> q; for (int i = 1; i <= q; i ++) { int l, r; cin >> l >> r; pos[l].emplace_back(r, i); } vector<int> s; segment_tree st(n); for (int l = n; l > 0; l --) { if (a[l] == 1) { if (!s.empty()) { st.modify(-s.back(), -1); s.pop_back(); } st.modify(l, 1); } else { s.push_back(-l); } for (pair<int, int> x: pos[l]) { int r, i; tie(r, i) = x; int add = lower_bound(all(s), - r) - s.begin(); add = (int) s.size() - add; res[i] = max(0, st.get(l, r)) + add; } } for (int i = 1; i <= q; i ++) { cout << res[i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...