#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define fi first
#define se second
#define pld pair<ld,ld>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define y1 Enkognit
#define all(v) v.begin(),v.end()
using namespace std;
using namespace __gnu_pbds;
const ll MOD=1000000007;
ll n, m, k, T;
string s;
ll binpow(ll h,ll r)
{
ll l=1;
while (r)
{
if (r&1) l=(l*h)%MOD;
h=(h*h)%MOD;
r/=2;
}
return l;
}
struct node
{
int sum;
int ans;
int l, r;
node():l(0),r(0),sum(0),ans(0){};
node(int h):l(h),r(h),sum(h),ans(h){};
};
node d[2000001];
node operator + (node x,node y)
{
if (x.sum==-1e9) return y;
if (y.sum==-1e9) return x;
node n;
n.sum=x.sum+y.sum;
n.l=max(x.l, x.sum+y.l);
n.r=max(y.r, y.sum+x.r);
n.ans=max(max(x.ans + y.sum, y.ans + x.sum), x.l + y.r);
return n;
}
void build(int h,int l,int r)
{
if (l==r)
{
if (s[l]=='T')
{
d[h].l++;
d[h].r++;
d[h].sum++;
d[h].ans++;
}else
{
d[h].sum--;
}
return;
}
int w=(l+r)/2;
build(h*2,l,w);
build(h*2+1,w+1,r);
d[h]=d[h*2]+d[h*2+1];
}
node get(int h,int l,int r,int x,int y)
{
if (x>y) return node(-1e9);
if (l==x && y==r) return d[h];
int w=(l+r)/2;
return get(h*2,l,w,x,min(y,w))+get(h*2+1,w+1,r,max(x,w+1),y);
}
void solve()
{
cin >> n;
cin >> s;
s=' '+s;
build(1,1,n);
ll q;
cin >> q;
while (q--)
{
ll l, r;
cin >> l >> r;
cout << get(1,1,n,l,r).ans << "\n";
}
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
ll t=1;
//cin >> t;
while (t--)
{
solve();
}
return 0;
}
/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/
Compilation message
election.cpp: In constructor 'node::node()':
election.cpp:42:12: warning: 'node::r' will be initialized after [-Wreorder]
42 | int l, r;
| ^
election.cpp:40:9: warning: 'int node::sum' [-Wreorder]
40 | int sum;
| ^~~
election.cpp:43:5: warning: when initialized here [-Wreorder]
43 | node():l(0),r(0),sum(0),ans(0){};
| ^~~~
election.cpp: In constructor 'node::node(int)':
election.cpp:42:12: warning: 'node::r' will be initialized after [-Wreorder]
42 | int l, r;
| ^
election.cpp:40:9: warning: 'int node::sum' [-Wreorder]
40 | int sum;
| ^~~
election.cpp:44:5: warning: when initialized here [-Wreorder]
44 | node(int h):l(h),r(h),sum(h),ans(h){};
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31724 KB |
Output is correct |
2 |
Correct |
20 ms |
31724 KB |
Output is correct |
3 |
Correct |
20 ms |
31616 KB |
Output is correct |
4 |
Correct |
20 ms |
31724 KB |
Output is correct |
5 |
Correct |
20 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31724 KB |
Output is correct |
2 |
Correct |
20 ms |
31724 KB |
Output is correct |
3 |
Correct |
20 ms |
31616 KB |
Output is correct |
4 |
Correct |
20 ms |
31724 KB |
Output is correct |
5 |
Correct |
20 ms |
31724 KB |
Output is correct |
6 |
Correct |
82 ms |
33000 KB |
Output is correct |
7 |
Correct |
75 ms |
33000 KB |
Output is correct |
8 |
Correct |
82 ms |
33000 KB |
Output is correct |
9 |
Correct |
71 ms |
33000 KB |
Output is correct |
10 |
Correct |
79 ms |
32872 KB |
Output is correct |
11 |
Correct |
81 ms |
33128 KB |
Output is correct |
12 |
Correct |
80 ms |
33156 KB |
Output is correct |
13 |
Correct |
80 ms |
33128 KB |
Output is correct |
14 |
Correct |
79 ms |
33128 KB |
Output is correct |
15 |
Correct |
80 ms |
33068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31724 KB |
Output is correct |
2 |
Correct |
20 ms |
31724 KB |
Output is correct |
3 |
Correct |
20 ms |
31616 KB |
Output is correct |
4 |
Correct |
20 ms |
31724 KB |
Output is correct |
5 |
Correct |
20 ms |
31724 KB |
Output is correct |
6 |
Correct |
82 ms |
33000 KB |
Output is correct |
7 |
Correct |
75 ms |
33000 KB |
Output is correct |
8 |
Correct |
82 ms |
33000 KB |
Output is correct |
9 |
Correct |
71 ms |
33000 KB |
Output is correct |
10 |
Correct |
79 ms |
32872 KB |
Output is correct |
11 |
Correct |
81 ms |
33128 KB |
Output is correct |
12 |
Correct |
80 ms |
33156 KB |
Output is correct |
13 |
Correct |
80 ms |
33128 KB |
Output is correct |
14 |
Correct |
79 ms |
33128 KB |
Output is correct |
15 |
Correct |
80 ms |
33068 KB |
Output is correct |
16 |
Correct |
651 ms |
39956 KB |
Output is correct |
17 |
Correct |
551 ms |
40212 KB |
Output is correct |
18 |
Correct |
602 ms |
40376 KB |
Output is correct |
19 |
Correct |
488 ms |
39828 KB |
Output is correct |
20 |
Correct |
657 ms |
39344 KB |
Output is correct |
21 |
Correct |
658 ms |
40980 KB |
Output is correct |
22 |
Correct |
659 ms |
40852 KB |
Output is correct |
23 |
Correct |
653 ms |
41108 KB |
Output is correct |
24 |
Correct |
644 ms |
40724 KB |
Output is correct |
25 |
Correct |
659 ms |
40524 KB |
Output is correct |