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;
typedef long long ll;
const ll N=5e5+7;
ll n, Q, a[N], b[N], l, r;
char s;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>s;
a[i]=(s=='C')?1:-1;
}
cin>>Q;
for(int i=1;i<=Q;i++){
cin>>l>>r;
ll sum=0,countb=0;
for(int pos=l;pos<=r;pos++){
b[pos]=0;
sum+=a[pos];
if(sum<0){
b[pos]--;
sum=0;
countb++;
}
}
sum=0;
for(int pos=r;pos>=l;pos--){
sum+=a[pos];
if(sum<0){
b[pos]++;
sum=0;
}
}
sum=0;
for(int pos=l;pos<=r;pos++){
sum+=b[pos];
if(sum<0) sum=0;
}
cout<<countb+sum<<'\n';
}
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... |