//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define ll long long
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 5e5 + 3, mod = 998244353;
const ll INF = 1e10;
struct node{
int L, R, ans, sum;
}t[N * 4];
int n, q;
node merge(const node &x, const node &y){
node tmp;
tmp.L = max(x.L, x.sum + y.L);
tmp.R = max(x.R + y.sum, y.R);
tmp.sum = x.sum + y.sum;
tmp.ans = max(x.L + y.R, max(x.sum + y.ans, x.ans + y.sum));
return tmp;
}
void build(int v, int tl, int tr){
if(tl == tr){
char x;
cin >> x;
if(x == 'T')
t[v] = {1, 1, 1, 1};
else
t[v] = {0, 0, 0, -1};
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm), build(v * 2 + 1, tm + 1, tr);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
node get(int v, int tl, int tr, int l, int r){
if(tl > r || l > tr)
return {};
if(tl >= l && tr <= r)
return t[v];
int tm = (tl + tr) / 2;
return merge(get(v * 2, tl, tm, l, r), get(v * 2 + 1, tm + 1, tr, l, r));
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
build(1, 1, n);
cin >> q;
for(int l, r;q;q--){
cin >> l >> r;
cout << get(1, 1, n, l, r).ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
75 ms |
5612 KB |
Output is correct |
7 |
Correct |
66 ms |
5680 KB |
Output is correct |
8 |
Correct |
68 ms |
5740 KB |
Output is correct |
9 |
Correct |
61 ms |
5612 KB |
Output is correct |
10 |
Correct |
71 ms |
5612 KB |
Output is correct |
11 |
Correct |
71 ms |
5740 KB |
Output is correct |
12 |
Correct |
72 ms |
5740 KB |
Output is correct |
13 |
Correct |
76 ms |
5740 KB |
Output is correct |
14 |
Correct |
73 ms |
5784 KB |
Output is correct |
15 |
Correct |
73 ms |
5740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
75 ms |
5612 KB |
Output is correct |
7 |
Correct |
66 ms |
5680 KB |
Output is correct |
8 |
Correct |
68 ms |
5740 KB |
Output is correct |
9 |
Correct |
61 ms |
5612 KB |
Output is correct |
10 |
Correct |
71 ms |
5612 KB |
Output is correct |
11 |
Correct |
71 ms |
5740 KB |
Output is correct |
12 |
Correct |
72 ms |
5740 KB |
Output is correct |
13 |
Correct |
76 ms |
5740 KB |
Output is correct |
14 |
Correct |
73 ms |
5784 KB |
Output is correct |
15 |
Correct |
73 ms |
5740 KB |
Output is correct |
16 |
Correct |
709 ms |
26476 KB |
Output is correct |
17 |
Correct |
612 ms |
26160 KB |
Output is correct |
18 |
Correct |
660 ms |
26372 KB |
Output is correct |
19 |
Correct |
548 ms |
25776 KB |
Output is correct |
20 |
Correct |
696 ms |
25708 KB |
Output is correct |
21 |
Correct |
701 ms |
27364 KB |
Output is correct |
22 |
Correct |
699 ms |
27372 KB |
Output is correct |
23 |
Correct |
720 ms |
27436 KB |
Output is correct |
24 |
Correct |
696 ms |
27116 KB |
Output is correct |
25 |
Correct |
705 ms |
26656 KB |
Output is correct |