This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
struct node
{
int max_pref;
int max_suff;
int sum;
int qry;
};
node combine(node x, node y)
{
node rez;
rez.sum = x.sum + y.sum;
rez.max_pref = max(x.max_pref, x.sum + y.max_pref);
rez.max_suff = max(y.max_suff, y.sum + x.max_suff);
rez.qry = max(x.max_pref + y.max_suff, max(x.qry + y.sum, y.qry + x.sum));
return rez;
}
node aint[2100001];
int n;
int v[500001];
void build(int nod, int st, int dr)
{
if(st==dr)
{
int x;
if(v[st]==1) x=1;
else x=0;
aint[nod]={max(0,v[st]),max(0,v[st]),v[st],x};
return;
}
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
aint[nod]=combine(aint[nod*2], aint[nod*2+1]);
}
node query(int nod, int st, int dr, int le, int ri)
{
if(le>ri)
return {0,0,0,0};
if(le==st && dr==ri)
return aint[nod];
int mij=(st+dr)/2;
return combine(query(nod*2,st,mij,le,min(mij,ri)),
query(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
char ch;
for(int i=1;i<=n;i++)
{
cin>>ch;
if(ch=='T')
v[i]=1;
else
v[i]=-1;
}
build(1,1,n);
int q,l,r;
node x;
cin>>q;
for(int i=0;i<q;i++)
{
cin>>l>>r;
x=query(1,1,n,l,r);
cout<<x.qry<<"\n";
}
return 0;
}
/**
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |