#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
int n;
cin>>n;
string a;
cin>>a;
vector<int> nums(n);
for(int i = 0; i < n; i++){
if(a[i] == 'T') nums[i] = -1;
else nums[i] = 1;
}
int q;
cin>>q;
while(q--){
int l, r;
cin>>l>>r;
l--; r--;
int ans1 = 0, ans2 = 0;
int keep = 0;
for(int i = l; i <= r; i++){
keep += nums[i];
if(keep < 0){
keep = 0;
ans1++;
}
}
if(keep < 0){
keep = 0;
ans1++;
}
keep = 0;
for(int i = r; i >= l; i--){
keep += nums[i];
if(keep < 0){
keep = 0;
ans2++;
}
}
if(keep < 0){
keep = 0;
ans2++;
}
cout<<max(ans1, ans2)<<'\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin>>t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |