#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 0, n + 1)
#define SZ(x) (int)(x).size()
#define pb push_back
#define pf push_front
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
#define ckmin(a, b) a = min(a, b)
#define ckmax(a, b) a = max(a, b)
// #define int long long
const int INF = 1e9;
const int maxn = 3e5 + 5;
int n, q;
string s;
struct node{
int mxl, mxr, sum, ans;
node operator + (node b){
node ret;
ret.mxl = max(mxl, b.mxl + sum);
ret.mxr = max(mxr + b.sum, b.mxr);
ret.sum = sum + b.sum;
ret.ans = max(max(sum + b.ans, ans + b.sum), mxl + b.mxr);
return ret;
}
} st[maxn * 4];
void build(int v = 1, int l = 0, int r = n - 1){
if(l == r){
if(s[l] == 'T') st[v] = {1, 1, 1, 1};
else st[v] = {0, 0, -1, 0};
return;
}
int m = (l + r) / 2;
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
st[v] = st[v * 2] + st[v * 2 + 1];
}
node qry(int l, int r, int v = 1, int L = 0, int R = n - 1){
if(l > R || r < L) return {0, 0, 0, 0};
if(l <= L && r >= R) return st[v];
int m = (L + R) / 2;
return qry(l, r, v * 2, L, m) + qry(l, r, v * 2 + 1, m + 1, R);
}
int main(void){
fastio;
cin>>n>>s>>q;
build();
for(int i = 0; i < q; i++){
int l, r;
cin>>l>>r;
l--, r--;
cout<<qry(l, r).ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
37 ms |
6108 KB |
Output is correct |
7 |
Correct |
38 ms |
6224 KB |
Output is correct |
8 |
Correct |
35 ms |
5976 KB |
Output is correct |
9 |
Correct |
32 ms |
5828 KB |
Output is correct |
10 |
Correct |
38 ms |
5968 KB |
Output is correct |
11 |
Correct |
37 ms |
5968 KB |
Output is correct |
12 |
Correct |
37 ms |
6180 KB |
Output is correct |
13 |
Correct |
45 ms |
6124 KB |
Output is correct |
14 |
Correct |
38 ms |
6176 KB |
Output is correct |
15 |
Correct |
36 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
37 ms |
6108 KB |
Output is correct |
7 |
Correct |
38 ms |
6224 KB |
Output is correct |
8 |
Correct |
35 ms |
5976 KB |
Output is correct |
9 |
Correct |
32 ms |
5828 KB |
Output is correct |
10 |
Correct |
38 ms |
5968 KB |
Output is correct |
11 |
Correct |
37 ms |
5968 KB |
Output is correct |
12 |
Correct |
37 ms |
6180 KB |
Output is correct |
13 |
Correct |
45 ms |
6124 KB |
Output is correct |
14 |
Correct |
38 ms |
6176 KB |
Output is correct |
15 |
Correct |
36 ms |
5980 KB |
Output is correct |
16 |
Correct |
388 ms |
27072 KB |
Output is correct |
17 |
Correct |
292 ms |
26856 KB |
Output is correct |
18 |
Correct |
328 ms |
27112 KB |
Output is correct |
19 |
Correct |
277 ms |
26480 KB |
Output is correct |
20 |
Correct |
339 ms |
26292 KB |
Output is correct |
21 |
Correct |
374 ms |
28048 KB |
Output is correct |
22 |
Correct |
352 ms |
27872 KB |
Output is correct |
23 |
Correct |
354 ms |
28128 KB |
Output is correct |
24 |
Correct |
385 ms |
27712 KB |
Output is correct |
25 |
Correct |
337 ms |
27368 KB |
Output is correct |