Submission #571465

#TimeUsernameProblemLanguageResultExecution timeMemory
571465BackNoobElection (BOI18_election)C++14
100 / 100
1095 ms64404 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define endl '\n' #define MASK(i) (1LL << (i)) #define ull unsigned long long #define ld long double #define pb push_back #define all(x) (x).begin() , (x).end() #define BIT(x , i) ((x >> (i)) & 1) #define TASK "task" #define sz(s) (int) (s).size() using namespace std; const int mxN = 5e5 + 227; const int inf = 1e9 + 277; const int mod = 24211007; const ll infll = 1e18 + 7; const int base = 200; const int LOG = 29; template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } int pre[mxN]; struct IT{ int n; vector<int> tmax, tmin; IT(){} IT(int _n) { n = _n; tmax.resize(n * 4 + 7, -inf); tmin.resize(n * 4 + 7, inf); } void update(int v, int tl, int tr, int pos, int val) { if(tl == tr) { tmax[v] = val; tmin[v] = val; } else { int tm = (tl + tr) / 2; if(pos <= tm) update(v * 2, tl, tm, pos, val); else update(v * 2 + 1, tm + 1, tr, pos, val); tmax[v] = max(tmax[v * 2], tmax[v * 2 + 1]); tmin[v] = min(tmin[v * 2], tmin[v * 2 + 1]); } } int getmax(int v, int tl, int tr, int l, int r) { if(l > r) return -inf; if(tl == l && tr == r) return tmax[v]; int tm = (tl + tr) / 2; int m1 = getmax(v * 2, tl, tm, l, min(r, tm)); int m2 = getmax(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); return max(m1, m2); } int getmin(int v, int tl, int tr, int l, int r) { if(l > r) return inf; if(tl == l && tr == r) return tmin[v]; int tm = (tl + tr) / 2; int m1 = getmin(v * 2, tl, tm, l, min(r, tm)); int m2 = getmin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); return min(m1, m2); } void update(int pos, int val) { update(1, 1, n, pos, val); } int getmax(int l, int r) { return getmax(1, 1, n, l, r); } int getmin(int l, int r) { return getmin(1, 1, n, l, r); } } seg; struct node{ int lef, rig, sum, mx; }; struct IT1{ int n; vector<node> t; IT1(){} IT1(int _n) { n = _n; t.resize(n * 4 + 7, {0, 0, 0, -inf}); } node modify(node a, node b) { if(a.mx == -inf) return b; if(b.mx == -inf) return a; node res; res.mx = max(a.mx, b.mx); res.sum = a.sum + b.sum; res.lef = max(a.lef, a.sum + b.lef); res.rig = max(b.rig, b.sum + a.rig); maximize(res.mx, res.lef); maximize(res.mx, res.rig); maximize(res.mx, a.rig + b.lef); return res; } void update(int v, int tl, int tr, int pos, int val) { if(tl == tr) { if(val == -1) t[v] = {0, 0, -1, 0}; else t[v] = {1, 1, 1, 1}; } else { int tm = (tl + tr) / 2; if(pos <= tm) update(v * 2, tl, tm, pos, val); else update(v * 2 + 1, tm + 1, tr, pos, val); t[v] = modify(t[v * 2], t[v * 2 + 1]); } } node getans(int v, int tl, int tr, int l, int r) { if(l > r) return {0, 0, 0, -inf}; if(tl == l && tr == r) return t[v]; int tm = (tl + tr) / 2; node m1 = getans(v * 2, tl, tm, l, min(r, tm)); node m2 = getans(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); node res = modify(m1, m2); // cout << m1.lef << ' ' << m1.rig << ' ' << m1.sum << ' ' << m1.mx << endl; // cout << m2.lef << ' ' << m2.rig << ' ' << m2.sum << ' ' << m2.mx << endl; // cout << res.lef << ' ' << res.rig << ' ' << res.sum << ' ' << res.mx << endl; return res; } int getans(int l, int r) { return getans(1, 1, n, l, r).mx; } void update(int pos, int val) { update(1, 1, n, pos, val); } } seg1; int n; int q; int a[mxN]; int maxleng[mxN]; string s; void solve() { cin >> n; cin >> s; for(int i = 1; i <= n; i++) { if(s[i - 1] == 'C') a[i] = 1; else a[i] = -1; } for(int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i]; seg = IT(n); seg1 = IT1(n); for(int i = 1; i <= n; i++) seg.update(i, pre[i]); for(int i = 1; i <= n; i++) seg1.update(i, a[i]); for(int i = 1; i <= n; i++) { if(a[i] == -1) maxleng[i] = 0; else maxleng[i] = maxleng[i - 1] + a[i]; } cin >> q; while(q--) { int l, r; cin >> l >> r; int mn = seg.getmin(l, r) - pre[l - 1]; int res = 0; int last = pre[r] - pre[l - 1]; // cout << last << ' '; if(mn < 0) { res += -mn; last += -mn; } // cout << last << ' ' << seg1.getans(l, r) << endl; res += max(0, seg1.getans(l, r) - last); cout << res << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen(TASK".in" , "r" , stdin); //freopen(TASK".out" , "w" , stdout); int tc = 1; // cin >> tc; while(tc--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...