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>
#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<ordered_set,ordered_set>> t(N*4);
void build(ll v,ll tl,ll tr){
if(tl==tr){
if(pref[tl]-pref[tl-1]<0){t[v].fr.ins(pref[tl]);t[v].sc.ins(suf[tl]);}
return;
}
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
for(auto i : t[v*2].fr) t[v].fr.ins(i);
for(auto i : t[v*2+1].fr) t[v].fr.ins(i);
for(auto i : t[v*2].sc) t[v].sc.ins(i);
for(auto i : t[v*2+1].sc) t[v].sc.ins(i);
}
ll go(ll v,ll tl,ll tr,ll l,ll r){
if(tl>r || tr<l)return 0ll;
if(l<=tl && tr<=r){
/*
cout<<tl<<' '<<tr<<endl;
for(auto i : t[v].fr) cout<<i<<' ';
cout<<endl;
for(auto i : t[v].sc) cout<<i<<' ';
cout<<endl;
cout<<max(t[v].fr.order_of_key(pref[l-1]),t[v].sc.order_of_key(suf[r+1]))<<endl<<endl;
*/
return max(t[v].fr.order_of_key(pref[l-1]),t[v].sc.order_of_key(suf[r+1]));
}
ll tm=(tl+tr)/2;
ll a=go(v*2,tl,tm,l,r),b=go(v*2+1,tm+1,tr,l,r);
return a+b;
}
void solve(){
ll i,q;
ll n;
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;
cout<<go(1,1,n,l,r)<<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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |