#include <bits/stdc++.h>
using namespace std;
int n, q;
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].p = val; seg[root].s = val;
seg[root].t += val;
seg[root].a = ((val == -1) ? 0 : 1);
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]);
}
node query(int root, int tl, int tr, int l, int r){
if (tl > r || tr < l) return {0, 0, 0, 0};
if (tl >= l && tr <= r) return seg[root];
int tm = (tl + tr) / 2;
return combine(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 >> q;
while (q--){
int l, r; cin >> l >> r;
cout << query(1, 1, n, l, r).a << '\n';
}
}
/*
3
TCT
3
1 1
2 2
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
69 ms |
5588 KB |
Output is correct |
7 |
Correct |
63 ms |
5472 KB |
Output is correct |
8 |
Correct |
57 ms |
5524 KB |
Output is correct |
9 |
Correct |
59 ms |
5452 KB |
Output is correct |
10 |
Correct |
69 ms |
5508 KB |
Output is correct |
11 |
Correct |
81 ms |
5612 KB |
Output is correct |
12 |
Correct |
68 ms |
5620 KB |
Output is correct |
13 |
Correct |
81 ms |
5620 KB |
Output is correct |
14 |
Correct |
64 ms |
5632 KB |
Output is correct |
15 |
Correct |
74 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
69 ms |
5588 KB |
Output is correct |
7 |
Correct |
63 ms |
5472 KB |
Output is correct |
8 |
Correct |
57 ms |
5524 KB |
Output is correct |
9 |
Correct |
59 ms |
5452 KB |
Output is correct |
10 |
Correct |
69 ms |
5508 KB |
Output is correct |
11 |
Correct |
81 ms |
5612 KB |
Output is correct |
12 |
Correct |
68 ms |
5620 KB |
Output is correct |
13 |
Correct |
81 ms |
5620 KB |
Output is correct |
14 |
Correct |
64 ms |
5632 KB |
Output is correct |
15 |
Correct |
74 ms |
5624 KB |
Output is correct |
16 |
Correct |
602 ms |
26452 KB |
Output is correct |
17 |
Correct |
502 ms |
26080 KB |
Output is correct |
18 |
Correct |
573 ms |
26316 KB |
Output is correct |
19 |
Correct |
460 ms |
25608 KB |
Output is correct |
20 |
Correct |
622 ms |
25356 KB |
Output is correct |
21 |
Correct |
561 ms |
27212 KB |
Output is correct |
22 |
Correct |
622 ms |
27068 KB |
Output is correct |
23 |
Correct |
644 ms |
27488 KB |
Output is correct |
24 |
Correct |
599 ms |
27080 KB |
Output is correct |
25 |
Correct |
603 ms |
26504 KB |
Output is correct |