Submission #348794

#TimeUsernameProblemLanguageResultExecution timeMemory
348794EnkognitElection (BOI18_election)C++14
100 / 100
659 ms41108 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define fi first
#define se second
#define pld pair<ld,ld>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define y1 Enkognit
#define all(v) v.begin(),v.end()

using namespace std;
using namespace __gnu_pbds;

const ll MOD=1000000007;

ll n, m, k, T;
string s;

ll binpow(ll h,ll r)
{
    ll l=1;
    while (r)
    {
        if (r&1) l=(l*h)%MOD;
        h=(h*h)%MOD;
        r/=2;
    }
    return l;
}

struct node
{
    int sum;
    int ans;
    int l, r;
    node():l(0),r(0),sum(0),ans(0){};
    node(int h):l(h),r(h),sum(h),ans(h){};
};

node d[2000001];

node operator + (node x,node y)
{
    if (x.sum==-1e9) return y;
    if (y.sum==-1e9) return x;
    node n;
    n.sum=x.sum+y.sum;
    n.l=max(x.l, x.sum+y.l);
    n.r=max(y.r, y.sum+x.r);
    n.ans=max(max(x.ans + y.sum, y.ans + x.sum), x.l + y.r);
    return n;
}

void build(int h,int l,int r)
{
    if (l==r)
    {
        if (s[l]=='T')
        {
            d[h].l++;
            d[h].r++;
            d[h].sum++;
            d[h].ans++;
        }else
        {
            d[h].sum--;
        }
        return;
    }
    int w=(l+r)/2;
    build(h*2,l,w);
    build(h*2+1,w+1,r);
    d[h]=d[h*2]+d[h*2+1];
}

node get(int h,int l,int r,int x,int y)
{
    if (x>y) return node(-1e9);
    if (l==x && y==r) return d[h];
    int w=(l+r)/2;
    return get(h*2,l,w,x,min(y,w))+get(h*2+1,w+1,r,max(x,w+1),y);
}

void solve()
{
    cin >> n;
    cin >> s;
    s=' '+s;
    build(1,1,n);
    ll q;
    cin >> q;
    while (q--)
    {
        ll l, r;
        cin >> l >> r;
        cout << get(1,1,n,l,r).ans << "\n";
    }
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll t=1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/

Compilation message (stderr)

election.cpp: In constructor 'node::node()':
election.cpp:42:12: warning: 'node::r' will be initialized after [-Wreorder]
   42 |     int l, r;
      |            ^
election.cpp:40:9: warning:   'int node::sum' [-Wreorder]
   40 |     int sum;
      |         ^~~
election.cpp:43:5: warning:   when initialized here [-Wreorder]
   43 |     node():l(0),r(0),sum(0),ans(0){};
      |     ^~~~
election.cpp: In constructor 'node::node(int)':
election.cpp:42:12: warning: 'node::r' will be initialized after [-Wreorder]
   42 |     int l, r;
      |            ^
election.cpp:40:9: warning:   'int node::sum' [-Wreorder]
   40 |     int sum;
      |         ^~~
election.cpp:44:5: warning:   when initialized here [-Wreorder]
   44 |     node(int h):l(h),r(h),sum(h),ans(h){};
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...