#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define getar(ar,n) for(ll i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define all(v) v.begin(), v.end()
#define br cout<<"\n"
#define pb push_back
#define nl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ret return
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define fi first
#define se second
const int mxN = 5e5 + 5000;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
string str;
struct Node
{
ll pref=0,suff=0,mx=0,val=0;
};
Node seg[mxN*4];
Node mrg(Node lft,Node rgt){
Node curr;
curr.pref=max(lft.val+rgt.pref,lft.pref);
curr.suff=max(rgt.val+lft.suff,rgt.suff);
curr.mx=max({lft.pref+rgt.suff,lft.mx+rgt.val,lft.val+rgt.mx});
curr.val=lft.val+rgt.val;
return curr;
}
void build(ll node,ll start,ll end){
if(start==end){
if(str[start]=='T') seg[node]={1,1,1,1};
else seg[node]={0,0,0,-1};
return;
}
ll mid=start+(end-start)/2;
build(node*2+1,start,mid);
build(node*2+2,mid+1,end);
seg[node]=mrg(seg[node*2+1],seg[node*2+2]);
}
Node qry(ll node,ll start,ll end,ll l,ll r){
if(start>r||end<l) return {0,0,0,0};
if(start>=l&&end<=r) return seg[node];
ll mid=start+(end-start)/2;
return mrg(qry(node*2+1,start,mid,l,r),qry(node*2+2,mid+1,end,l,r));
}
void lets_go() {
ll n,l,r,q,c=0;cin>>n;
cin>>str;
build(0,0,n-1);
cin>>q;
while(q--){
cin>>l>>r;--l,--r;
show(qry(0,0,n-1,l,r).mx);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
lets_go();
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... |