#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
struct node
{
int max_pref;
int max_suff;
int sum;
int qry;
};
node combine(node x, node y)
{
node rez;
rez.sum = x.sum + y.sum;
rez.max_pref = max(x.max_pref, x.sum + y.max_pref);
rez.max_suff = max(y.max_suff, y.sum + x.max_suff);
rez.qry = max(x.max_pref + y.max_suff, max(x.qry + y.sum, y.qry + x.sum));
return rez;
}
node aint[2100001];
int n;
int v[500001];
void build(int nod, int st, int dr)
{
if(st==dr)
{
int x;
if(v[st]==1) x=1;
else x=0;
aint[nod]={max(0,v[st]),max(0,v[st]),v[st],x};
return;
}
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
aint[nod]=combine(aint[nod*2], aint[nod*2+1]);
}
node query(int nod, int st, int dr, int le, int ri)
{
if(le>ri)
return {0,0,0,0};
if(le==st && dr==ri)
return aint[nod];
int mij=(st+dr)/2;
return combine(query(nod*2,st,mij,le,min(mij,ri)),
query(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
char ch;
for(int i=1;i<=n;i++)
{
cin>>ch;
if(ch=='T')
v[i]=1;
else
v[i]=-1;
}
build(1,1,n);
int q,l,r;
node x;
cin>>q;
for(int i=0;i<q;i++)
{
cin>>l>>r;
x=query(1,1,n,l,r);
cout<<x.qry<<"\n";
}
return 0;
}
/**
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
47 ms |
5796 KB |
Output is correct |
7 |
Correct |
45 ms |
5856 KB |
Output is correct |
8 |
Correct |
45 ms |
5824 KB |
Output is correct |
9 |
Correct |
42 ms |
5780 KB |
Output is correct |
10 |
Correct |
63 ms |
5792 KB |
Output is correct |
11 |
Correct |
50 ms |
5944 KB |
Output is correct |
12 |
Correct |
48 ms |
5972 KB |
Output is correct |
13 |
Correct |
46 ms |
5972 KB |
Output is correct |
14 |
Correct |
47 ms |
5896 KB |
Output is correct |
15 |
Correct |
44 ms |
5808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
47 ms |
5796 KB |
Output is correct |
7 |
Correct |
45 ms |
5856 KB |
Output is correct |
8 |
Correct |
45 ms |
5824 KB |
Output is correct |
9 |
Correct |
42 ms |
5780 KB |
Output is correct |
10 |
Correct |
63 ms |
5792 KB |
Output is correct |
11 |
Correct |
50 ms |
5944 KB |
Output is correct |
12 |
Correct |
48 ms |
5972 KB |
Output is correct |
13 |
Correct |
46 ms |
5972 KB |
Output is correct |
14 |
Correct |
47 ms |
5896 KB |
Output is correct |
15 |
Correct |
44 ms |
5808 KB |
Output is correct |
16 |
Correct |
450 ms |
28276 KB |
Output is correct |
17 |
Correct |
366 ms |
27840 KB |
Output is correct |
18 |
Correct |
406 ms |
28160 KB |
Output is correct |
19 |
Correct |
408 ms |
27724 KB |
Output is correct |
20 |
Correct |
432 ms |
27348 KB |
Output is correct |
21 |
Correct |
429 ms |
29212 KB |
Output is correct |
22 |
Correct |
442 ms |
29156 KB |
Output is correct |
23 |
Correct |
486 ms |
29444 KB |
Output is correct |
24 |
Correct |
417 ms |
28988 KB |
Output is correct |
25 |
Correct |
453 ms |
28484 KB |
Output is correct |