#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1e9+7;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
struct node{
ll sum;
ll mpre;
ll msuf;
};
node seg[2000005];
string S;
void build(ll idx,ll s, ll e){
if(e == s){
ll v = 1;
if(S[e-1] == 'C'){
v = -1;
}
seg[idx].sum = v;
seg[idx].mpre = seg[idx].msuf = max(0LL,v);
return;
}
ll mid = (e+s)/2;
build(idx*2,s,mid);
build(idx*2+1,mid+1,e);
seg[idx].sum = seg[idx*2].sum + seg[idx*2+1].sum;
seg[idx].mpre = max(seg[idx*2].mpre,seg[idx*2].sum+seg[idx*2+1].mpre);
seg[idx].msuf = max(seg[idx*2+1].msuf,seg[idx*2+1].sum+seg[idx*2].msuf);
}
node query(ll idx,ll s,ll e,ll qs, ll qe){
node anss;
if(qs <= s && e <= qe){
return seg[idx];
}
if(qs > e || qe < s){
anss.sum = anss.mpre = anss.msuf = 0;
return anss;
}
ll mid = (e+s)/2;
node a = query(idx*2,s,mid,qs,qe);
node b = query(idx*2+1,mid+1,e,qs,qe);
anss.sum = a.sum + b.sum;
anss.mpre = max(a.mpre,a.sum+b.mpre);
anss.msuf = max(b.msuf,b.sum+a.msuf);
return anss;
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,q;
cin>>n;
string s;
cin>>s;
cin>>q;
vll arr[n];
ll team[n+1];
ll t = 0;
S = s;
build(1,1,n);
for(ll i = n-1;i >= 0;i--){
if(s[i] == 'T'){
if(query(1,1,n,i+2,n).mpre <= 0){
team[i+1] = t;
arr[t].pb(i+1);
t++;
}
else{
ll l = i+2,r = n;
ll ans = n;
while(l <= r){
ll mid = (l+r)/2;
if(query(1,1,n,i+2,mid).mpre > 0){
r = mid - 1;
ans = min(ans,mid);
}
else{
l = mid + 1;
}
}
team[i+1] = team[ans];
arr[team[i+1]].pb(i+1);
}
}
}
f(i,0,n){
sort(all(arr[i]));
}
while(q--){
ll L,R;
cin>>L>>R;
ll ans = 0;
ll POS = L,POS2 = L-1;
if(query(1,1,n,L,R).mpre > 0){
ll l = L,r = R;
ll res = R;
while(l <= r){
ll mid = (l+r)/2;
if(query(1,1,n,L,mid).mpre > 0){
r = mid - 1;
res = min(res,mid);
}
else{
l = mid + 1;
}
}
ll T = team[res];
ll posl = lower_bound(all(arr[T]),res) - arr[T].begin();
ll P = upper_bound(all(arr[T]),R) - arr[T].begin();
ll posr = min(1LL * arr[T].size() - 1,P);
ans += posr - posl + 1;
POS = arr[T][posr] + 1;
POS2 = arr[T][posl] - 1;
}
ans += query(1,1,n,POS,R).msuf;
ans += max(0LL,query(1,1,n,L,POS2).msuf - max(0LL,-query(1,1,n,POS,R).sum));
cout<<ans<<"\n";
}
}
/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/
Compilation message
election.cpp: In function 'int main()':
election.cpp:128:52: error: no matching function for call to 'min(long long unsigned int, ll&)'
128 | ll posr = min(1LL * arr[T].size() - 1,P);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from election.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
230 | min(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: template argument deduction/substitution failed:
election.cpp:128:52: note: deduced conflicting types for parameter 'const _Tp' ('long long unsigned int' and 'll' {aka 'long long int'})
128 | ll posr = min(1LL * arr[T].size() - 1,P);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from election.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
278 | min(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: template argument deduction/substitution failed:
election.cpp:128:52: note: deduced conflicting types for parameter 'const _Tp' ('long long unsigned int' and 'll' {aka 'long long int'})
128 | ll posr = min(1LL * arr[T].size() - 1,P);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from election.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(std::initializer_list<_Tp>)'
3468 | min(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: template argument deduction/substitution failed:
election.cpp:128:52: note: mismatched types 'std::initializer_list<_Tp>' and 'long long unsigned int'
128 | ll posr = min(1LL * arr[T].size() - 1,P);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from election.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(std::initializer_list<_Tp>, _Compare)'
3474 | min(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: template argument deduction/substitution failed:
election.cpp:128:52: note: mismatched types 'std::initializer_list<_Tp>' and 'long long unsigned int'
128 | ll posr = min(1LL * arr[T].size() - 1,P);
| ^