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> //Andrei Alexandru a.k.a Sho10
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,q,a[500005];
string s;
struct nodee{
ll pref;
ll suf;
ll sum;
ll res;
};
nodee tree[10000005];
nodee con(nodee x,nodee y){
nodee ans;
ans.pref=max(x.pref,x.sum+y.pref);
ans.suf=max(y.suf,y.sum+x.suf);
ans.sum=x.sum+y.sum;
ans.res=max(x.pref+y.suf,max(x.sum+y.res,y.sum+x.res));
return ans;
}
void build(ll node,ll l,ll r){
if(l==r){
tree[node].pref=max(0ll,a[l]);
tree[node].suf=max(0ll,a[l]);
tree[node].sum=a[l];
tree[node].res=max(0ll,a[l]);
return;
}
ll mid=(l+r)/2;
build(2*node,l,mid);
build(2*node+1,mid+1,r);
tree[node]=con(tree[node*2],tree[node*2+1]);
}
nodee query(ll node,ll l,ll r,ll st,ll dr){
if(l>dr){
return {};
}
if(r<st){
return {};
}
if(st<=l&&r<=dr){
return tree[node];
}
ll mid=(l+r)/2;
return con(query(2*node,l,mid,st,dr),query(2*node+1,mid+1,r,st,dr));
}
int32_t main(){
CODE_START;
cin>>n;
cin>>s;
for(ll i=0;i<s.size();i++)
{
ll s1=0;
if(s[i]=='C'){
s1=-1;
}else s1=+1;
a[i+1]=s1;
}
build(1,1,n);
cin>>q;
while(q--){
ll l,r;
cin>>l>>r;
cout<<query(1,1,n,l,r).res<<endl;
}
}
Compilation message (stderr)
election.cpp: In function 'int32_t main()':
election.cpp:68:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(ll i=0;i<s.size();i++)
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |