#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 left, state right){
if(left.tot == -1e9) return right;
if(right.tot == -1e9) return left;
state now;
now.tot = left.tot + right.tot;
now.pref = max(left.tot + right.pref, left.pref);
now.suf = max(left.suf + right.tot, right.suf);
now.ans = max(max(left.ans + right.tot, left.tot + right.ans), left.pref + right.pref);
return now;
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |