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;
const int BASE = 1<<19;
const int INF = 1e9+7;
int d1[2*BASE], d2[2*BASE], suf[BASE], pref[BASE], t[BASE];
int mini1(int x,int y){
x+=BASE-1, y+=BASE+1; int r=INF;
while(x/2 != y/2){
if(x%2==0) r=min(r,d1[x+1]);
if(y%2==1) r=min(r,d1[y-1]);
x/=2; y/=2;
}
return r;
}
int mini2(int x,int y){
x+=BASE-1, y+=BASE+1; int r=INF;
while(x/2 != y/2){
if(x%2==0) r=min(r,d2[x+1]);
if(y%2==1) r=min(r,d2[y-1]);
x/=2; y/=2;
}
return r;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,a,b,q,r1,r2;
char c;
cin>>n;
for(int i=1;i<=n;i++){
cin>>c;
if(c=='T') t[i]=-1;
else t[i]=1;
pref[i]=pref[i-1]+t[i];
}
for(int i=n;i>=1;i--) suf[i]=suf[i+1]+t[i];
for(int i=1;i<=n;i++) d1[i+BASE]=pref[i];
for(int i=1;i<=n;i++) d2[i+BASE]=suf[i];
for(int i=BASE-1;i>=1;i--){
d1[i]=min(d1[2*i], d1[2*i+1]);
d2[i]=min(d2[2*i], d2[2*i+1]);
}
cin>>q;
while(q--){
cin>>a>>b;
r1=mini1(a,b)-pref[a-1];
r2=mini2(a,b)-suf[b+1];
if(min(r1,r2)<0) cout<<-min(r1,r2)<<'\n';
else cout<<0<<'\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... |