제출 #1136825

#제출 시각아이디문제언어결과실행 시간메모리
1136825TrinhKhanhDungElection (BOI18_election)C++20
100 / 100
305 ms35172 KiB
#include <bits/stdc++.h>

using namespace std;

struct Node{
    int L, R, S, A;

    Node operator + (const Node &other) const{
        Node ans;
        ans.L = max(L, S + other.L);
        ans.R = max(other.R, other.S + R);
        ans.S = S + other.S;
        ans.A = max(max(A + other.S, S + other.A), L + other.R);
        return ans;
    }
};

struct seg{
    vector<Node> it;
    int n;

    seg(int _n = 0){
        n = _n;
        it.resize(n * 4 + 3);
    }

    void build(string &s, int i, int l, int r){
        if(l == r){
            if(s[l - 1] == 'C')
                it[i] = {0, 0, -1, 0};
            else
                it[i] = {1, 1, 1, 1};
            return;
        }
        int m = (l + r) >> 1;
        build(s, i * 2, l, m);
        build(s, i * 2 + 1, m + 1, r);
        it[i] = it[i * 2] + it[i * 2 + 1];
    }

    Node get(int i, int l, int r, int u, int v){
        if(r < u || v < l) return {0, 0, 0, 0};
        if(u <= l && r <= v) return it[i];
        int m = (l + r) >> 1;
        return get(i * 2, l, m, u, v) + get(i * 2 + 1, m + 1, r, u, v);
    }

    int get(int u, int v){
        Node t = get(1, 1, n, u, v);
        return t.A;
    }
};

void solve(){
    int N; cin >> N;
    string s; cin >> s;

    seg it(N);
    it.build(s, 1, 1, N);

    int Q; cin >> Q;
    while(Q--){
        int l, r; cin >> l >> r;
        cout << it.get(l, r) << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("SHIP.inp", "r", stdin);
//    freopen("SHIP.out", "w", stdout);

    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...