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;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n;
string str;
struct state{
int ans;
int tot;
int pref;
int suf;
};
state neutral;
state tree[4 * 500100];
state merge(state a, state b){
if(a.tot==-(1e9))return b;if(b.tot==-(1e9))return a;
state c;
c.tot=a.tot+b.tot;
c.pref=std::max(a.tot+b.pref,a.pref);
c.suf=std::max(b.tot+a.suf,b.suf);
c.ans=std::max(std::max(a.ans+b.tot,a.tot+b.ans),a.pref+b.suf);
return c;
}
void update(int node, int l, int r, state put, int pos){
if(l == r && l == pos){
tree[node] = put;
}
else{
int mid = (l + r) / 2;
if(pos <= mid) update(node*2, l, mid, put, pos);
else update(node*2+1, mid+1, r, put, pos);
tree[node] =merge(tree[node*2], tree[node*2+1]);
}
}
state query(int node, int l, int r, int L, int R){
if(r < L || l > R) return neutral;
if(L <= l && r <= R) return tree[node];
int mid = (l + r) / 2;
return merge(query(node*2, l, mid, L, R), query(node*2+1, mid+1, r, L, R));
}
int main(){
cin>>n>>str;
state neg, pos;
neg.ans = neg.pref = neg.suf = neg.tot = 1;
pos.ans = pos.pref = pos.suf = 0;
pos.tot = -1;
neutral.tot = -1e9;
for(int i = 0; i < n; i++){
if(str[i] == 'C') update(1, 0, n-1, pos, i);
else update(1, 0, n-1, neg, i);
}
int q;
cin>>q;
while(q--){
int l, r;
cin>>l>>r;
l--; r--;
cout<<query(1, 0, n-1, l, r).ans<<endl;
}
return 0;
}
Compilation message (stderr)
election.cpp: In function 'state merge(state, state)':
election.cpp:21:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
21 | if(a.tot==-(1e9))return b;if(b.tot==-(1e9))return a;
| ^~
election.cpp:21:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
21 | if(a.tot==-(1e9))return b;if(b.tot==-(1e9))return a;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |