This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin>>n;
string s;
cin>>s;
int arr[n];
for(int i=0;i<n;i++){
arr[i]=(s[i]=='C'?1:-1);
}
int pref[n];
int suff[n+1];
for(int i=0;i<n;i++){
pref[i]=arr[i];
if(i)
pref[i]+=pref[i-1];
}
for(int j=n-1;j>=0;j--){
suff[j]=arr[j];
if(j+1<n) suff[j]+=suff[j+1];
}
int q;
cin>>q;
while(q--){
int ans=0;
int l,r;
cin>>l>>r;
l--,r--;
int c=0;
int d=0;
for(int i=l;i<=r;i++){
c=min(c,pref[i]-(l?pref[l-1]:0LL));
}
for(int i=l;i<=r;i++){
d=min(d,suff[i]-(r+1<n?suff[r+1]:0LL));
}
// cout<<c<<" "<<d<<endl;
int x=max(-c,-d);
ans=max(ans,x);
cout<<x<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |