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"
#include <bits/stdc++.h>
using namespace std;
const int N = 500*1000+5;
int a[N];
int b[N];
int main(){
ios_base::sync_with_stdio(false);
int n;
cin>>n;
string s;
cin>>s;
s = "0"+s;
for(int i = 1;i<=n;i++){
a[i] = a[i-1];
if(s[i] == 'C') a[i]++;
else a[i]--;
}
for(int i = n;i>=1;i--){
b[i] = b[i+1];
if(s[i] == 'C') b[i]++;
else b[i]--;
}
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
int mn = 1000*1000*1000+5;
int id = -1;
int ans = 0;
for(int i = l;i<=r;i++){
if(a[i]<mn){
mn = a[i];
id = i;
}
}
mn-=a[l-1];
ans = max(ans,-mn);
int qan = 0;
for(int i = id;i>=1;i--){
// if(qan >= -mn) break;
if(s[i] == 'T' && qan<(-mn)){
qan++;
}
b[i]+=qan;
}
int mnn = 1000*1000*1000+5;
for(int i = l;i<=r;i++){
if(b[i]<mnn){
mnn = b[i];
}
}
mnn-=b[r+1];
if(mnn<=0) ans += (-mnn);
qan = 0;
for(int i = id;i>=1;i--){
// if(qan>=-mn) break;
if(s[i] == 'T' && qan<(-mn)){
qan++;
}
b[i]-=qan;
}
cout<<ans<<endl;
}
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... |