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;
struct node{
ll pref,suf,ans,sum;
node operator +(node a){
node b;
b.sum=a.sum+sum;
b.pref=max(pref,a.pref+sum);
b.suf=max(a.suf,suf+a.sum);
b.ans=max({0ll,sum+a.ans,a.sum+ans,pref+a.suf});
return b;
}
};
ll a[N];
vector <node> t(N*4);
void build(ll v,ll tl,ll tr){
if(tl==tr){
t[v].sum=a[tl];
t[v].ans=t[v].pref=t[v].suf=max(a[tl],0ll);
return;
}
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=t[v*2]+t[v*2+1];
}
node go(ll v,ll tl,ll tr,ll l,ll r){
if(tl>r || tr<l)return {0ll,0ll,0ll,0ll};
if(l<=tl && tr<=r){
return t[v];
}
ll tm=(tl+tr)/2;
node 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') a[i]=-1;
else a[i]=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).ans<<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... |