Submission #1311250

#TimeUsernameProblemLanguageResultExecution timeMemory
1311250nikaa123Election (BOI18_election)C++20
0 / 100
2 ms568 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pp pop_back
#define endl '\n'
#define ff first
#define ss second
#define stop exit(0)
#define sz(x) (int)x.size()
#define pause system("pause")
#define all(x) (x).begin(), (x).end()
#define deb(x) cout << #x << "-" << x << endl
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
typedef string str;
typedef long long ll;
typedef vector<int> vii;
typedef pair<int, int> pii;
 
const long long INF = LLONG_MAX;
const int inf = INT_MAX;
const int mod = 998244353;
const int MOD = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
const double PI = 2 * acos(0.0);
 
const int N = 2e5+5;

int n;
map <int, int> id;
set<int> s;
int a[N];
pii t[N*4],t1[N*4];
int pref[N],suf[N],preft[N];

pii merge1(pii a, pii b) {
    return max(a,b);
}

pii merge(pii a,pii b) {
    if (a.ff == b.ff) {
        if (b.ss < a.ss) {
            return b;
        }
        return a;
    }
    return max(a,b);
}

void update(int node, int l, int r, int pos, int val) {
    if (pos < l || pos > r) return;
    if (l == r) {
        t[node] = {val,pos};
        return;
    }
    int mid = (l+r)/2;
    update(node*2,l,mid,pos,val);
    update(node*2+1,mid+1,r,pos,val);
    t[node] = merge(t[node*2],t[node*2+1]);
}

pii getans(int node, int l, int r, int L, int R) {
    if (r < L || l > R) return {-inf,0};
    if (l >= L && r <= R) return t[node];
    int mid = (l+r)/2;
    return merge(getans(node*2,l,mid,L,R), getans(node*2+1,mid+1,r,L,R));
}

void update1(int node, int l, int r, int pos, int val) {
    if (pos < l || pos > r) return;
    if (l == r) {
        t1[node] = {val,pos};
        return;
    }
    int mid = (l+r)/2;
    update1(node*2,l,mid,pos,val);
    update1(node*2+1,mid+1,r,pos,val);
    t1[node] = merge1(t1[node*2],t1[node*2+1]);
}

pii getans1(int node, int l, int r, int L, int R) {
    if (r < L || l > R) return {-inf,0};
    if (l >= L && r <= R) return t1[node];
    int mid = (l+r)/2;
    return merge1(getans1(node*2,l,mid,L,R), getans1(node*2+1,mid+1,r,L,R));
}


inline void test_case() {

    cin >> n;
    string c;cin >> c;
    for (int i = 0; i < n; i++) {
        if (c[i] == 'T') a[i+1] = 1; else a[i+1] = -1;
    }
    pref[0] = suf[n+1] = 0;
    for (int i = 1; i <= n; i++) {
        preft[i] = preft[i-1] + (a[i] == 1);
    }
    for (int i = 1; i <= n; i++) {
        pref[i] = pref[i-1] + a[i];
        update(1,1,n,i,pref[i]);
    }
    for (int i = n; i >= 1; i--) {
        suf[i] = suf[i+1] + a[i]; 
        update1(1,1,n,i,suf[i]);
    }

    // deb(getans(1,1,n,1,n).ss);

    int q;
    cin >> q;
    while (q--) {
        int l; int r;
        cin >> l >> r;
        auto [val1,pos1] = getans(1,1,n,l,r); // pref
        auto [val2,pos2] = getans1(1,1,n,l,r); // suf

        val1 -= pref[l-1];
        val2 -= suf[r+1];

        val1 = max(val1,0LL);
        val2 = max(val2,0LL);


        // deb(pos1);
        // deb(pos2);
        // deb(val1);

        int ans = 0;
        // deb(val2);

        int com = max(0LL,preft[pos1]-preft[pos2-1]);
        com = min(com,min(val1,val2));

        ans = val1 + val2;

        if (pos1 >= pos2) {
            ans -= com;
        }

        cout << ans << endl;
                
    }
    


}
 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int T = 1;
    // cin >> T;
    while (T--) test_case();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...