#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define sz size()
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define bc back()
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e18+7;
const ll mod=998244353;
const ll N=2e5+7;
const ld eps=1e-9;
ll pref[N],suf[N];
vector <pair<ll,ll>> t(N*4);
void build(ll v,ll tl,ll tr){
if(tl==tr){
t[v]={pref[tl],suf[tl]};
return;
}
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]={min(t[v*2].fr,t[v*2+1].fr),min(t[v*2].sc,t[v*2+1].sc)};
}
pair<ll,ll> go(ll v,ll tl,ll tr,ll l,ll r){
if(tl>r || tr<l)return {inf,inf};
if(l<=tl && tr<=r){
return t[v];
}
ll tm=(tl+tr)/2;
pair<ll,ll> a=go(v*2,tl,tm,l,r),b=go(v*2+1,tm+1,tr,l,r);
return {min(a.fr,b.fr),min(a.sc,b.sc)};
}
void solve(){
ll i,j,q;
ll n,m,k;
ll b,sum=0,c=0;
cin>>n;
str s;
cin>>s;
s=" "+s;
for(i=1;i<=n;i++){
if(s[i]=='C')pref[i]=pref[i-1]+1;
else pref[i]=pref[i-1]-1;
}
for(i=n;i>0;i--){
if(s[i]=='C') suf[i]=suf[i+1]+1;
else suf[i]=suf[i+1]-1;
}
build(1,1,n);
cin>>q;
ll l,r;
for(i=0;i<q;i++){
cin>>l>>r;
pair<ll,ll> p=go(1,1,n,l,r);
b=min(p.fr-pref[l-1],p.sc-suf[r+1]);
if(b<0){
cout<<abs(b)<<endl;
}else cout<<0<<endl;
}
}
signed main(){
start();
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ll t=1;
//cin>>t;
while(t--) solve();
}
/*
100
1 50 69 33 544
2 18 71 14 25 91 45
*/
Compilation message
election.cpp: In function 'void solve()':
election.cpp:56:10: warning: unused variable 'j' [-Wunused-variable]
56 | ll i,j,q;
| ^
election.cpp:57:10: warning: unused variable 'm' [-Wunused-variable]
57 | ll n,m,k;
| ^
election.cpp:57:12: warning: unused variable 'k' [-Wunused-variable]
57 | ll n,m,k;
| ^
election.cpp:58:7: warning: unused variable 'sum' [-Wunused-variable]
58 | ll b,sum=0,c=0;
| ^~~
election.cpp:58:13: warning: unused variable 'c' [-Wunused-variable]
58 | ll b,sum=0,c=0;
| ^
election.cpp: In function 'void fre(std::string)':
election.cpp:24:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:24:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |