#include <bits/stdc++.h>
using namespace std;
int N, Q; string S;
struct node{
int sum, pref, suff, res;
} ST[4 * 500005];
node merge(node A, node B) {
node C;
C.sum = A.sum + B.sum;
C.pref = max(A.pref, A.sum + B.pref);
C.suff = max(B.suff, B.sum + A.suff);
C.res = max({A.pref + B.suff, A.res + B.sum, A.sum + B.res});
return C;
}
void build(int v, int l, int r) {
if(l == r) {
if(S[l] == 'C') ST[v] = {-1, 0, 0, 0};
if(S[l] == 'T') ST[v] = {1, 1, 1, 1};
return;
}
build(v * 2, l, (l + r) / 2);
build(v * 2 + 1, (l + r) / 2 + 1, r);
ST[v] = merge(ST[v * 2], ST[v * 2 + 1]);
}
node query(int v, int l, int r, int lo, int hi) {
if(l > hi || r < lo) return {0, 0, 0, 0};
if(l >= lo && r <= hi) return ST[v];
return merge(query(v * 2, l, (l + r) / 2, lo, hi),
query(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi));
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> S >> Q;
build(1, 0, N - 1);
while(Q--) {
int l, r; cin >> l >> r; l--; r--;
cout << query(1, 0, N - 1, l, r).res << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
39 ms |
5732 KB |
Output is correct |
7 |
Correct |
38 ms |
5704 KB |
Output is correct |
8 |
Correct |
38 ms |
5636 KB |
Output is correct |
9 |
Correct |
33 ms |
5672 KB |
Output is correct |
10 |
Correct |
39 ms |
5624 KB |
Output is correct |
11 |
Correct |
40 ms |
5792 KB |
Output is correct |
12 |
Correct |
54 ms |
5924 KB |
Output is correct |
13 |
Correct |
39 ms |
5900 KB |
Output is correct |
14 |
Correct |
39 ms |
5820 KB |
Output is correct |
15 |
Correct |
39 ms |
5728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
39 ms |
5732 KB |
Output is correct |
7 |
Correct |
38 ms |
5704 KB |
Output is correct |
8 |
Correct |
38 ms |
5636 KB |
Output is correct |
9 |
Correct |
33 ms |
5672 KB |
Output is correct |
10 |
Correct |
39 ms |
5624 KB |
Output is correct |
11 |
Correct |
40 ms |
5792 KB |
Output is correct |
12 |
Correct |
54 ms |
5924 KB |
Output is correct |
13 |
Correct |
39 ms |
5900 KB |
Output is correct |
14 |
Correct |
39 ms |
5820 KB |
Output is correct |
15 |
Correct |
39 ms |
5728 KB |
Output is correct |
16 |
Correct |
361 ms |
26996 KB |
Output is correct |
17 |
Correct |
309 ms |
26492 KB |
Output is correct |
18 |
Correct |
364 ms |
26856 KB |
Output is correct |
19 |
Correct |
283 ms |
26344 KB |
Output is correct |
20 |
Correct |
371 ms |
26024 KB |
Output is correct |
21 |
Correct |
368 ms |
27816 KB |
Output is correct |
22 |
Correct |
377 ms |
27672 KB |
Output is correct |
23 |
Correct |
369 ms |
27908 KB |
Output is correct |
24 |
Correct |
368 ms |
27616 KB |
Output is correct |
25 |
Correct |
371 ms |
27220 KB |
Output is correct |