#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int maxn = 5e5 + 69;
struct Node {
int p, s, ans, sm;
} st[4 * maxn];
int n, q;
string s;
Node combine(Node a, Node b) {
Node ret;
ret.p = max(a.p, a.sm + b.p);
ret.s = max(b.s, b.sm + a.s);
ret.sm = a.sm + b.sm;
ret.ans = max({a.sm + b.ans, b.sm + a.ans, a.p + b.s});
return ret;
}
void build(int v, int tl, int tr) {
if (tl == tr) {
if (s[tl - 1] == 'T')
st[v].p = st[v].s = st[v].sm = st[v].ans = 1;
else {
st[v].p = st[v].s = st[v].ans = 0;
st[v].sm = -1;
}
}
else {
int mid = (tl + tr) / 2;
build(v * 2, tl, mid);
build(v * 2 + 1, mid + 1, tr);
st[v] = combine(st[v * 2], st[v * 2 + 1]);
}
}
Node qry(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return {0, 0, 0, 0};
else if (tl >= l && tr <= r)
return st[v];
else {
int mid = (tl + tr) / 2;
return combine(qry(v * 2, tl, mid, l, r), qry(v * 2 + 1, mid + 1, tr, l, r));
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> s >> q;
build(1, 1, n);
for (int i = 1;i <= q;i++) {
int l, r;
cin >> l >> r;
cout << qry(1, 1, n, l, r).ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
616 KB |
Output is correct |
6 |
Correct |
47 ms |
10212 KB |
Output is correct |
7 |
Correct |
46 ms |
10068 KB |
Output is correct |
8 |
Correct |
45 ms |
10056 KB |
Output is correct |
9 |
Correct |
52 ms |
10104 KB |
Output is correct |
10 |
Correct |
46 ms |
10064 KB |
Output is correct |
11 |
Correct |
47 ms |
10072 KB |
Output is correct |
12 |
Correct |
48 ms |
10064 KB |
Output is correct |
13 |
Correct |
48 ms |
10300 KB |
Output is correct |
14 |
Correct |
46 ms |
10024 KB |
Output is correct |
15 |
Correct |
47 ms |
10072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
616 KB |
Output is correct |
6 |
Correct |
47 ms |
10212 KB |
Output is correct |
7 |
Correct |
46 ms |
10068 KB |
Output is correct |
8 |
Correct |
45 ms |
10056 KB |
Output is correct |
9 |
Correct |
52 ms |
10104 KB |
Output is correct |
10 |
Correct |
46 ms |
10064 KB |
Output is correct |
11 |
Correct |
47 ms |
10072 KB |
Output is correct |
12 |
Correct |
48 ms |
10064 KB |
Output is correct |
13 |
Correct |
48 ms |
10300 KB |
Output is correct |
14 |
Correct |
46 ms |
10024 KB |
Output is correct |
15 |
Correct |
47 ms |
10072 KB |
Output is correct |
16 |
Correct |
477 ms |
43536 KB |
Output is correct |
17 |
Correct |
401 ms |
43284 KB |
Output is correct |
18 |
Correct |
455 ms |
43772 KB |
Output is correct |
19 |
Correct |
411 ms |
43048 KB |
Output is correct |
20 |
Correct |
467 ms |
42712 KB |
Output is correct |
21 |
Correct |
498 ms |
44544 KB |
Output is correct |
22 |
Correct |
487 ms |
44636 KB |
Output is correct |
23 |
Correct |
453 ms |
44624 KB |
Output is correct |
24 |
Correct |
466 ms |
44276 KB |
Output is correct |
25 |
Correct |
458 ms |
43888 KB |
Output is correct |