#include <bits/stdc++.h>
#define FOR(i, beg, ed, etr) for(ll i = beg;i <= ed;i += etr)
#define FORN(i, beg, ed, etr) for(ll i = beg;i >= ed;i -= etr)
#define all(x) x.begin(), x.end()
#define F first
#define pb push_back
#define S second
#define con continue
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef double db;
typedef long double ld;
const ll inf = 1e9;
const ll INF = 1e18;
const ll N = 5e4 + 5;
const ll MOD = 1e9 + 7;
int a[N],pre[N];
void solve(){
int n;cin >> n;
FOR(i, 1, n, 1){
char c;cin >> c;
if(c == 'T')a[i] = -1;
else a[i] = 1;
pre[i] = pre[i-1] + a[i];
}
int q;cin >>q ;
while(q--){
int l,r;cin >> l >> r;
int ans = 0;
int cnt = 0,sum = (pre[r] - pre[l-1]);
vector <int> v = {};
FOR(i, l, r, 1){
cnt += a[i];
v.pb(a[i]);
while(cnt < 0){
v.pop_back();
ans++;
cnt++;
}
}
cnt = 0;
int mx = 0;
FORN(i, v.size() - 1, 0, 1){
cnt += v[i];
if(cnt < 0)mx = max(mx,abs(cnt));
}
cout << (ans + mx) << '\n';
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("optmilk.in","r",stdin);
// freopen("optmilk.out","w",stdout);
ll abd = 1;
// cin >> abd;
FOR(i, 1, abd, 1){
solve();
}
}
Compilation message (stderr)
election.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
53 | main(){
| ^~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |