제출 #1342016

#제출 시각아이디문제언어결과실행 시간메모리
1342016nguyenkhangninh99Election (BOI18_election)C++20
100 / 100
511 ms40064 KiB

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 5e5 + 5, inf = 1e9 + 7;

struct node{
    int pre, suf, sum, mx;
} st[4 * maxn];
node merge(node a, node b){
    return {max(a.pre, b.pre + a.sum), max(b.suf, a.suf + b.sum), a.sum + b.sum, max({a.mx, b.mx, a.suf + b.pre})};
}
void update(int r, int pos, int val){
    int l = 1, id = 1;
    while(l < r){
        int mid = (l + r) / 2;
        if(pos <= mid) r = mid, id = id * 2;
        else l = mid + 1, id = id * 2 + 1; 
    }
    st[id] = {val, val, val, val};
    while(id > 1){
        id /= 2;
        st[id] = merge(st[id * 2], st[id * 2 + 1]);
    }
}
node get(int id, int l, int r, int u, int v){
    if(l > v || r <  u) return {-inf, -inf, 0, 0};
    if(l >= u && r <= v) return st[id];

    int mid = (l + r) / 2;
    return merge(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n; cin >> n;
    vector<int> pre(n + 1);
    for(int i = 1; i <= n; i++){
        char x; cin >> x;
        int val = (x == 'C' ? 1 : -1);
        update(n, i, val);
        pre[i] = pre[i - 1] + val;
    }

    int q; cin >> q;
    while(q--){
        int l, r; cin >> l >> r;
        cout << get(1, 1, n, l, r).mx - (pre[r] - pre[l - 1]) << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...