#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n , a[N];
string s;
struct Node{
int L , R , cnt , ans;
Node operator+(Node y){
Node ret;
ret.cnt = cnt + y.cnt;
ret.L = max(L ,cnt + y.L);
ret.R = max(y.R , y.cnt + R);
ret.ans = max({ans + y.cnt , y.ans + cnt , L + y.R});
return ret;
}
}T[4*N];
void B(int l = 0 , int r = n - 1 , int pos = 0){
if(l == r){
if(a[l] == 1) T[pos] = {1 , 1, 1, 1};
else T[pos] = {0 , 0 , -1 , 0};
return;
}
int mid = (l + r) >> 1;
B(l , mid , pos * 2 + 1);
B(mid + 1 ,r , pos * 2 + 2);
T[pos] = T[pos *2 + 1] + T[pos *2 + 2];
}
Node get(int L , int R , int l =0 , int r = n - 1 , int pos = 0){
if(r <L || R < l) return {0 , 0 , 0 , 0};
if(L <= l && r <= R) return T[pos];
int mid = (l + r) >> 1;
return get(L , R , l , mid , pos * 2 + 1) + get(L , R , mid + 1 , r , pos * 2 + 2);
}
int main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> s;
memset(a , -1 , sizeof(a));
for(int i = 0;i < n; ++i){
if(s[i] == 'T')a[i] = 1;
}
B();
int q;
cin >> q;
while(q--){
int a , b;
cin >> a >> b;
--a; --b;
cout << get(a , b).ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2252 KB |
Output is correct |
2 |
Correct |
5 ms |
2252 KB |
Output is correct |
3 |
Correct |
5 ms |
2252 KB |
Output is correct |
4 |
Correct |
6 ms |
2252 KB |
Output is correct |
5 |
Correct |
5 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2252 KB |
Output is correct |
2 |
Correct |
5 ms |
2252 KB |
Output is correct |
3 |
Correct |
5 ms |
2252 KB |
Output is correct |
4 |
Correct |
6 ms |
2252 KB |
Output is correct |
5 |
Correct |
5 ms |
2252 KB |
Output is correct |
6 |
Correct |
177 ms |
6912 KB |
Output is correct |
7 |
Correct |
172 ms |
6820 KB |
Output is correct |
8 |
Correct |
170 ms |
6720 KB |
Output is correct |
9 |
Correct |
194 ms |
6636 KB |
Output is correct |
10 |
Correct |
170 ms |
6596 KB |
Output is correct |
11 |
Correct |
171 ms |
6884 KB |
Output is correct |
12 |
Correct |
201 ms |
6872 KB |
Output is correct |
13 |
Correct |
173 ms |
6876 KB |
Output is correct |
14 |
Correct |
184 ms |
6836 KB |
Output is correct |
15 |
Correct |
173 ms |
6840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2252 KB |
Output is correct |
2 |
Correct |
5 ms |
2252 KB |
Output is correct |
3 |
Correct |
5 ms |
2252 KB |
Output is correct |
4 |
Correct |
6 ms |
2252 KB |
Output is correct |
5 |
Correct |
5 ms |
2252 KB |
Output is correct |
6 |
Correct |
177 ms |
6912 KB |
Output is correct |
7 |
Correct |
172 ms |
6820 KB |
Output is correct |
8 |
Correct |
170 ms |
6720 KB |
Output is correct |
9 |
Correct |
194 ms |
6636 KB |
Output is correct |
10 |
Correct |
170 ms |
6596 KB |
Output is correct |
11 |
Correct |
171 ms |
6884 KB |
Output is correct |
12 |
Correct |
201 ms |
6872 KB |
Output is correct |
13 |
Correct |
173 ms |
6876 KB |
Output is correct |
14 |
Correct |
184 ms |
6836 KB |
Output is correct |
15 |
Correct |
173 ms |
6840 KB |
Output is correct |
16 |
Correct |
1404 ms |
21268 KB |
Output is correct |
17 |
Correct |
1342 ms |
21308 KB |
Output is correct |
18 |
Correct |
1388 ms |
21448 KB |
Output is correct |
19 |
Correct |
1299 ms |
21024 KB |
Output is correct |
20 |
Correct |
1444 ms |
20260 KB |
Output is correct |
21 |
Correct |
1410 ms |
22124 KB |
Output is correct |
22 |
Correct |
1418 ms |
22076 KB |
Output is correct |
23 |
Correct |
1376 ms |
22204 KB |
Output is correct |
24 |
Correct |
1407 ms |
22016 KB |
Output is correct |
25 |
Correct |
1385 ms |
21540 KB |
Output is correct |