Submission #568760

#TimeUsernameProblemLanguageResultExecution timeMemory
568760ngpin04Election (BOI18_election)C++17
100 / 100
586 ms57196 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define TASK "" #define bit(x) (1LL << (x)) #define getbit(x, i) (((x) >> (i)) & 1) #define ALL(x) (x).begin(), (x).end() using namespace std; template <typename T1, typename T2> bool mini(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maxi(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count()); int rand(int l, int r) { return l + rd() % (r - l + 1); } const int N = 5e5 + 5; const int oo = 1e9; const long long ooo = 1e18; const int mod = 1e9 + 7; // 998244353; const long double pi = acos(-1); struct segtree { struct node { int sum, minr; node(int _sum = 0, int _minr = 0) { sum = _sum; minr = _minr; } node operator + (const node &p) const { node res; res.sum = sum + p.sum; res.minr = min(p.minr, minr + p.sum); return res; } }; int n; vector <node> st; segtree(int _n = 0) { n = _n; st.assign(4 * n + 5, node()); } void update(int id, int l, int r, int pos, int val) { if (l > pos || r < pos) return; if (l == r) { st[id] = node(val, val); return; } int mid = (l + r) >> 1; update(id << 1, l, mid, pos, val); update(id << 1 | 1, mid + 1, r, pos, val); st[id] = st[id << 1] + st[id << 1 | 1]; } void update(int pos, int val) { update(1, 1, n, pos, val); } node getval(int id, int l, int r, int u, int v) { if (l > v || r < u) return node(); if (l >= u && r <= v) return st[id]; int mid = (l + r) >> 1; node lnode = getval(id << 1, l, mid, u, v); node rnode = getval(id << 1 | 1, mid + 1, r, u, v); return lnode + rnode; } int getval(int l, int r) { return getval(1, 1, n, l, r).minr; } }; vector <int> qr[N]; int ans[N]; int bit[N]; int l[N]; int r[N]; int a[N]; int n, q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ONLINE_JUDGE // freopen(TASK".inp","r",stdin); // freopen(TASK".out","w",stdout); #endif cin >> n; { string s; cin >> s; for (int i = 1; i <= n; i++) a[i] = (s[i - 1] == 'C') ? 1 : -1; } cin >> q; for (int i = 1; i <= q; i++) { cin >> l[i] >> r[i]; qr[l[i]].push_back(i); } segtree st(n); vector <int> pos; for (int i = n; i >= 1; i--) { if (a[i] < 0) pos.push_back(-i); else { if (pos.size()) { st.update(-pos.back(), -1); pos.pop_back(); } st.update(i, 1); } for (int ind : qr[i]) { int cntdel = (int) pos.size() - (lower_bound(ALL(pos), -r[ind]) - pos.begin()); ans[ind] = max(0, -st.getval(l[ind], r[ind])) + cntdel; } } for (int i = 1; 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...