#include <bits/stdc++.h>
using namespace std;
struct A {
int L,R,sum,ans;
} s[1<<20];
int n;
char c[500001];
A merge(A a, A b) {
A res;
res.L=max(a.L,a.sum+b.L);
res.R=max(b.R,b.sum+a.R);
res.sum=a.sum+b.sum;
res.ans=max({a.L+b.R,a.ans+b.sum,b.ans+a.sum});
return res;
}
void build(int l, int r, int idx) {
if (l==r) {
if (c[l]=='T') s[idx]={1,1,1,1};
else s[idx]={0,0,-1,0};
} else {
int mid=(l+r)/2;
build(l,mid,2*idx);
build(mid+1,r,2*idx+1);
s[idx]=merge(s[2*idx],s[2*idx+1]);
}
}
A query(int l, int r, int idx, int x, int y) {
if (x>r || y<l) return {0,0,0,0};
if (x<=l && r<=y) return s[idx];
int mid=(l+r)/2;
return merge(query(l,mid,2*idx,x,y),query(mid+1,r,2*idx+1,x,y));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=n; ++i) cin>>c[i];
build(1,n,1);
int q; cin>>q;
while (q--) {
int a,b; cin>>a>>b;
cout<<query(1,n,1,a,b).ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
6 |
Correct |
50 ms |
5652 KB |
Output is correct |
7 |
Correct |
47 ms |
5648 KB |
Output is correct |
8 |
Correct |
53 ms |
5576 KB |
Output is correct |
9 |
Correct |
40 ms |
5584 KB |
Output is correct |
10 |
Correct |
45 ms |
5580 KB |
Output is correct |
11 |
Correct |
46 ms |
5756 KB |
Output is correct |
12 |
Correct |
46 ms |
5708 KB |
Output is correct |
13 |
Correct |
47 ms |
5684 KB |
Output is correct |
14 |
Correct |
62 ms |
5708 KB |
Output is correct |
15 |
Correct |
45 ms |
5604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
424 KB |
Output is correct |
6 |
Correct |
50 ms |
5652 KB |
Output is correct |
7 |
Correct |
47 ms |
5648 KB |
Output is correct |
8 |
Correct |
53 ms |
5576 KB |
Output is correct |
9 |
Correct |
40 ms |
5584 KB |
Output is correct |
10 |
Correct |
45 ms |
5580 KB |
Output is correct |
11 |
Correct |
46 ms |
5756 KB |
Output is correct |
12 |
Correct |
46 ms |
5708 KB |
Output is correct |
13 |
Correct |
47 ms |
5684 KB |
Output is correct |
14 |
Correct |
62 ms |
5708 KB |
Output is correct |
15 |
Correct |
45 ms |
5604 KB |
Output is correct |
16 |
Correct |
444 ms |
26828 KB |
Output is correct |
17 |
Correct |
431 ms |
26432 KB |
Output is correct |
18 |
Correct |
443 ms |
26664 KB |
Output is correct |
19 |
Correct |
351 ms |
26076 KB |
Output is correct |
20 |
Correct |
449 ms |
25932 KB |
Output is correct |
21 |
Correct |
479 ms |
27680 KB |
Output is correct |
22 |
Correct |
466 ms |
27508 KB |
Output is correct |
23 |
Correct |
478 ms |
27856 KB |
Output is correct |
24 |
Correct |
513 ms |
27456 KB |
Output is correct |
25 |
Correct |
515 ms |
27048 KB |
Output is correct |