#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vector<int>>
#define INF LLONG_MAX
#define MOD 1000000007
#define MAXN 500000
using namespace std;
struct node{
int L,R,S,A;
};
node t[4*MAXN+1];
node merge(node a,node b){
node res;
res.L=max(a.L,a.S+b.L);
res.R=max(b.R,b.S+a.R);
res.S=a.S+b.S;
res.A=max({a.A+b.S,a.S+b.A,a.L+b.R});
return res;
}
void build(int a[],int v,int tl,int tr){
if (tl==tr){
if (a[tl]==1)t[v]={1,1,1,1}; else t[v]={0,0,-1,0};
}else{
int tm=(tl+tr)/2;
build(a,v*2,tl,tm);
build(a,v*2+1,tm+1,tr);
t[v]=merge(t[v*2],t[v*2+1]);
}
}
node query(int v,int tl,int tr,int l,int r){
if (l>r)return {0,0,0,0};
if (tl==l && tr==r)return t[v];
int tm=(tl+tr)/2;
return merge(query(v*2,tl,tm,l,min(r,tm)),query(v*2+1,tm+1,tr,max(l,tm+1),r));
}
int32_t main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
int a[n+1];
for (int i=1;i<=n;i++){
char c;
cin>>c;
if (c=='T')a[i]=1; else a[i]=-1;
}
build(a,1,1,n);
int q;
cin>>q;
while (q--){
int l,r;
cin>>l>>r;
cout<<query(1,1,n,l,r).A<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
57 ms |
10236 KB |
Output is correct |
7 |
Correct |
49 ms |
10188 KB |
Output is correct |
8 |
Correct |
49 ms |
10188 KB |
Output is correct |
9 |
Correct |
43 ms |
10188 KB |
Output is correct |
10 |
Correct |
50 ms |
10096 KB |
Output is correct |
11 |
Correct |
52 ms |
10312 KB |
Output is correct |
12 |
Correct |
50 ms |
10280 KB |
Output is correct |
13 |
Correct |
50 ms |
10340 KB |
Output is correct |
14 |
Correct |
51 ms |
10228 KB |
Output is correct |
15 |
Correct |
53 ms |
10156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
57 ms |
10236 KB |
Output is correct |
7 |
Correct |
49 ms |
10188 KB |
Output is correct |
8 |
Correct |
49 ms |
10188 KB |
Output is correct |
9 |
Correct |
43 ms |
10188 KB |
Output is correct |
10 |
Correct |
50 ms |
10096 KB |
Output is correct |
11 |
Correct |
52 ms |
10312 KB |
Output is correct |
12 |
Correct |
50 ms |
10280 KB |
Output is correct |
13 |
Correct |
50 ms |
10340 KB |
Output is correct |
14 |
Correct |
51 ms |
10228 KB |
Output is correct |
15 |
Correct |
53 ms |
10156 KB |
Output is correct |
16 |
Correct |
508 ms |
46668 KB |
Output is correct |
17 |
Correct |
440 ms |
46272 KB |
Output is correct |
18 |
Correct |
443 ms |
46524 KB |
Output is correct |
19 |
Correct |
382 ms |
45956 KB |
Output is correct |
20 |
Correct |
511 ms |
45896 KB |
Output is correct |
21 |
Correct |
568 ms |
47548 KB |
Output is correct |
22 |
Correct |
517 ms |
47380 KB |
Output is correct |
23 |
Correct |
584 ms |
47728 KB |
Output is correct |
24 |
Correct |
510 ms |
47304 KB |
Output is correct |
25 |
Correct |
496 ms |
46804 KB |
Output is correct |