#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(max(x.a + y.t, y.a + x.t), x.p + y.s);
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
420 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
420 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 |
67 ms |
5588 KB |
Output is correct |
7 |
Correct |
64 ms |
5580 KB |
Output is correct |
8 |
Correct |
64 ms |
5516 KB |
Output is correct |
9 |
Correct |
71 ms |
5552 KB |
Output is correct |
10 |
Correct |
70 ms |
5596 KB |
Output is correct |
11 |
Correct |
77 ms |
5588 KB |
Output is correct |
12 |
Correct |
67 ms |
5632 KB |
Output is correct |
13 |
Correct |
73 ms |
5708 KB |
Output is correct |
14 |
Correct |
65 ms |
5652 KB |
Output is correct |
15 |
Correct |
76 ms |
5632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
420 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 |
67 ms |
5588 KB |
Output is correct |
7 |
Correct |
64 ms |
5580 KB |
Output is correct |
8 |
Correct |
64 ms |
5516 KB |
Output is correct |
9 |
Correct |
71 ms |
5552 KB |
Output is correct |
10 |
Correct |
70 ms |
5596 KB |
Output is correct |
11 |
Correct |
77 ms |
5588 KB |
Output is correct |
12 |
Correct |
67 ms |
5632 KB |
Output is correct |
13 |
Correct |
73 ms |
5708 KB |
Output is correct |
14 |
Correct |
65 ms |
5652 KB |
Output is correct |
15 |
Correct |
76 ms |
5632 KB |
Output is correct |
16 |
Correct |
596 ms |
26376 KB |
Output is correct |
17 |
Correct |
495 ms |
25876 KB |
Output is correct |
18 |
Correct |
512 ms |
26132 KB |
Output is correct |
19 |
Correct |
470 ms |
25708 KB |
Output is correct |
20 |
Correct |
587 ms |
25424 KB |
Output is correct |
21 |
Correct |
543 ms |
27120 KB |
Output is correct |
22 |
Correct |
548 ms |
27040 KB |
Output is correct |
23 |
Correct |
591 ms |
27396 KB |
Output is correct |
24 |
Correct |
613 ms |
26996 KB |
Output is correct |
25 |
Correct |
538 ms |
26584 KB |
Output is correct |