#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)
const int N = 1'000'005;
int a[N];
struct node {
int s, prf, suf, ans;
node() {
s = prf = suf = ans = 0;
}
node(int x) {
s = x;
prf = suf = ans = max(x, 0);
}
friend node operator+(const node &l, const node &r) {
node ret;
ret.s = l.s + r.s;
ret.prf = max(l.prf, l.s + r.prf);
ret.suf = max(r.suf, l.suf + r.s);
ret.ans = max({l.ans + r.s, l.s + r.ans, l.prf + r.suf});
return ret;
}
} seg[1 << 21];
void build(int i, int l, int r) {
if (l == r) {
seg[i] = a[l];
return;
}
build(i << 1, l, md);
build(i << 1 | 1, md + 1, r);
seg[i] = seg[i << 1] + seg[i << 1 | 1];
}
node get(int i, int l, int r, int s, int e) {
if (s <= l && r <= e) {
return seg[i];
}
if (r < s || e < l) {
return node();
}
return get(i << 1, l, md, s, e) + get(i << 1 | 1, md + 1, r, s, e);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
a[i] = (c == 'T'? 1 : -1);
}
build(1, 0, n - 1);
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
cout << get(1, 0, n - 1, l, r).ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
33368 KB |
Output is correct |
2 |
Correct |
9 ms |
33368 KB |
Output is correct |
3 |
Correct |
7 ms |
33492 KB |
Output is correct |
4 |
Correct |
7 ms |
33440 KB |
Output is correct |
5 |
Correct |
8 ms |
33688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
33368 KB |
Output is correct |
2 |
Correct |
9 ms |
33368 KB |
Output is correct |
3 |
Correct |
7 ms |
33492 KB |
Output is correct |
4 |
Correct |
7 ms |
33440 KB |
Output is correct |
5 |
Correct |
8 ms |
33688 KB |
Output is correct |
6 |
Correct |
48 ms |
36688 KB |
Output is correct |
7 |
Correct |
44 ms |
36660 KB |
Output is correct |
8 |
Correct |
47 ms |
36432 KB |
Output is correct |
9 |
Correct |
49 ms |
36844 KB |
Output is correct |
10 |
Correct |
45 ms |
36436 KB |
Output is correct |
11 |
Correct |
47 ms |
36688 KB |
Output is correct |
12 |
Correct |
45 ms |
36636 KB |
Output is correct |
13 |
Correct |
57 ms |
36612 KB |
Output is correct |
14 |
Correct |
53 ms |
36708 KB |
Output is correct |
15 |
Correct |
45 ms |
36692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
33368 KB |
Output is correct |
2 |
Correct |
9 ms |
33368 KB |
Output is correct |
3 |
Correct |
7 ms |
33492 KB |
Output is correct |
4 |
Correct |
7 ms |
33440 KB |
Output is correct |
5 |
Correct |
8 ms |
33688 KB |
Output is correct |
6 |
Correct |
48 ms |
36688 KB |
Output is correct |
7 |
Correct |
44 ms |
36660 KB |
Output is correct |
8 |
Correct |
47 ms |
36432 KB |
Output is correct |
9 |
Correct |
49 ms |
36844 KB |
Output is correct |
10 |
Correct |
45 ms |
36436 KB |
Output is correct |
11 |
Correct |
47 ms |
36688 KB |
Output is correct |
12 |
Correct |
45 ms |
36636 KB |
Output is correct |
13 |
Correct |
57 ms |
36612 KB |
Output is correct |
14 |
Correct |
53 ms |
36708 KB |
Output is correct |
15 |
Correct |
45 ms |
36692 KB |
Output is correct |
16 |
Correct |
369 ms |
45112 KB |
Output is correct |
17 |
Correct |
318 ms |
44628 KB |
Output is correct |
18 |
Correct |
336 ms |
44820 KB |
Output is correct |
19 |
Correct |
300 ms |
44440 KB |
Output is correct |
20 |
Correct |
375 ms |
44324 KB |
Output is correct |
21 |
Correct |
366 ms |
45960 KB |
Output is correct |
22 |
Correct |
403 ms |
45720 KB |
Output is correct |
23 |
Correct |
397 ms |
46164 KB |
Output is correct |
24 |
Correct |
367 ms |
45724 KB |
Output is correct |
25 |
Correct |
392 ms |
45300 KB |
Output is correct |