#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAXN = 5e5 + 10;
char c[MAXN];
struct Segment {
struct Node {
int pf, sf, sum, ans;
Node()
{
pf = sf = sum = ans = 0;
}
} node[MAXN << 2];
Node merge(Node l, Node r)
{
Node res;
res.pf = max(l.pf, l.sum + r.pf);
res.sf = max(r.sf, r.sum + l.sf);
res.sum = l.sum + r.sum;
res.ans = max(max(l.ans + r.sum, r.ans + l.sum), l.pf + r.sf);
return res;
}
void initialize(int id, int l, int r)
{
if (l + 1 == r)
{
if (c[l] == 'T')
node[id].pf = node[id].sf = node[id].sum = node[id].ans = 1;
else
{
node[id].pf = node[id].sf = node[id].ans = 0;
node[id].sum = -1;
}
return;
}
int mid = (l + r) >> 1;
initialize(id << 1 | 0, l, mid);
initialize(id << 1 | 1, mid, r);
node[id] = merge(node[id << 1 | 0], node[id << 1 | 1]);
}
Node get(int id, int L, int R, int l, int r)
{
if (L == l && R == r)
{
return node[id];
}
int mid = (L + R) >> 1;
Node res;
if (l < mid)
res = merge(res, get(id << 1 | 0, L, mid, l, min(r, mid)));
if (r > mid)
res = merge(res, get(id << 1 | 1, mid, R, max(l, mid), r));
return res;
}
} Seg;
int main()
{
int n, q;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> c[i];
Seg.initialize(1, 1, n + 1);
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r;
auto ans = Seg.get(1, 1, n + 1, l, r + 1);
cout << ans.ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31568 KB |
Output is correct |
2 |
Correct |
10 ms |
31568 KB |
Output is correct |
3 |
Correct |
9 ms |
31740 KB |
Output is correct |
4 |
Correct |
9 ms |
31568 KB |
Output is correct |
5 |
Correct |
10 ms |
31568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31568 KB |
Output is correct |
2 |
Correct |
10 ms |
31568 KB |
Output is correct |
3 |
Correct |
9 ms |
31740 KB |
Output is correct |
4 |
Correct |
9 ms |
31568 KB |
Output is correct |
5 |
Correct |
10 ms |
31568 KB |
Output is correct |
6 |
Correct |
177 ms |
32012 KB |
Output is correct |
7 |
Correct |
168 ms |
32088 KB |
Output is correct |
8 |
Correct |
156 ms |
31824 KB |
Output is correct |
9 |
Correct |
159 ms |
31836 KB |
Output is correct |
10 |
Correct |
165 ms |
32072 KB |
Output is correct |
11 |
Correct |
161 ms |
32072 KB |
Output is correct |
12 |
Correct |
156 ms |
32072 KB |
Output is correct |
13 |
Correct |
163 ms |
32120 KB |
Output is correct |
14 |
Correct |
157 ms |
31972 KB |
Output is correct |
15 |
Correct |
180 ms |
31880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31568 KB |
Output is correct |
2 |
Correct |
10 ms |
31568 KB |
Output is correct |
3 |
Correct |
9 ms |
31740 KB |
Output is correct |
4 |
Correct |
9 ms |
31568 KB |
Output is correct |
5 |
Correct |
10 ms |
31568 KB |
Output is correct |
6 |
Correct |
177 ms |
32012 KB |
Output is correct |
7 |
Correct |
168 ms |
32088 KB |
Output is correct |
8 |
Correct |
156 ms |
31824 KB |
Output is correct |
9 |
Correct |
159 ms |
31836 KB |
Output is correct |
10 |
Correct |
165 ms |
32072 KB |
Output is correct |
11 |
Correct |
161 ms |
32072 KB |
Output is correct |
12 |
Correct |
156 ms |
32072 KB |
Output is correct |
13 |
Correct |
163 ms |
32120 KB |
Output is correct |
14 |
Correct |
157 ms |
31972 KB |
Output is correct |
15 |
Correct |
180 ms |
31880 KB |
Output is correct |
16 |
Correct |
1261 ms |
34124 KB |
Output is correct |
17 |
Correct |
1156 ms |
34044 KB |
Output is correct |
18 |
Correct |
1141 ms |
34028 KB |
Output is correct |
19 |
Correct |
1126 ms |
33552 KB |
Output is correct |
20 |
Correct |
1252 ms |
33096 KB |
Output is correct |
21 |
Correct |
1250 ms |
34872 KB |
Output is correct |
22 |
Correct |
1253 ms |
34908 KB |
Output is correct |
23 |
Correct |
1269 ms |
35040 KB |
Output is correct |
24 |
Correct |
1273 ms |
34780 KB |
Output is correct |
25 |
Correct |
1199 ms |
34264 KB |
Output is correct |