Submission #1209013

#TimeUsernameProblemLanguageResultExecution timeMemory
1209013vaneaElection (BOI18_election)C++20
0 / 100
2 ms324 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int mxN = 2e6+10;
string s, s1;
array<int, 3> st1[mxN], st2[mxN];

array<int, 3> comb(array<int, 3> a, array<int, 3> b) {
    array<int, 3> ans = {0, 0, 0};
    ans[0] = a[0] + max(0, b[0] - a[2]);
    ans[1] = a[1] + b[1];
    ans[2] = max(b[2], b[2] + a[2] - b[0]);
    return ans;
}

void build(int node, int l, int r) {
    if(l == r) {
        st1[node] = {s[l] == 'T', s[l] == 'C' ? 1 : -1, s[l] == 'C'};
        st2[node] = {s1[l] == 'T', s1[l] == 'C' ? 1 : -1, s1[l] == 'C'};
        return ;
    }
    int mid = (l+r)/2;
    build(node*2, l, mid);
    build(node*2+1, mid+1, r);
    st1[node] = comb(st1[node*2], st1[node*2+1]);
    st2[node] = comb(st2[node*2], st2[node*2+1]);
}

array<int, 3> qry(int node, int l, int r, int l1, int r1, int flag) {
    if(l1 <= l && r <= r1) {
        if(flag == 1) return st1[node];
        else return st2[node];
    }
    if(l > r1 || r < l1) return {0, 0};
    int mid = (l+r)/2;
    return comb(qry(node*2, l, mid, l1, r1, flag),
                qry(node*2+1, mid+1, r, l1, r1, flag));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    cin >> s;
    s1 = s;
    reverse(s1.begin(), s1.end());
    int q;
    cin >> q;
    build(1, 0, n-1);
    while(q--) {
        int l, r;
        cin >> l >> r;
        --l; --r;
        array<int, 3> now = qry(1, 0, n-1, l, r, 1);
        array<int, 3> now1 = qry(1, 0, n-1, n-r-1, n-l-1, 2);
        cout << max(now[0], now1[0]) << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...