#include <bits/stdc++.h>
using namespace std;
int n, q; string s;
struct node{
int p;
int s;
int t;
int a;
};
node seg[2310000];
node combine(node x, node y){
node re;
re.p = max(x.p, x.t + y.p);
re.s = max(y.s, y.t + x.s);
re.t = x.t + y.t;
re.a = max(x.p + y.s, max(x.a + y.t, y.a + x.t));
return re;
}
void update(int root, int tl, int tr, int pos, int val){
if (tl == tr){
seg[root].p = max(seg[root].p, val);
seg[root].s = max(seg[root].s, val);
seg[root].t += val;
seg[root].a = -val;
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) update(2 * root, tl, tm, pos, val);
else update(2 * root + 1, tm + 1, tr, pos, val);
seg[root] = combine(seg[2 * root], seg[2 * root + 1]);
}
int query(int root, int tl, int tr, int l, int r){
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return seg[root].a;
int tm = (tl + tr) / 2;
return (query(2 * root, tl, tm, l, r) + query(2 * root + 1, tm + 1, tr, l, r));
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
/*
for (int i = 1;i <= n;i ++){
char x; cin >> x;
update(1, 1, n, i, ((x == 'C') ? -1 : 1));
}
*/
cin >> s;
for (int i = 1;i <= n;i++){
update(1, 1, n, i, ((s[i - 1] == 'C') ? -1 : 1));
}
cin >> q;
while (q--){
int l, r; cin >> l >> r;
cout << query(1, 1, n, l, r) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |