#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll INF=1e9,LINF=1e18,MOD=1e9+7; const ll MAXN=1e3+1,MAXA=1e5+1;
#define fi first
#define se second
#define pii pair<int,int>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define PRINT(x) cerr<<#x<<'-'<<x<<endl<<flush;
#define pb push_back
#define PRINTvec(x) { cerr<<#x<<"-"; for(int i=0;i<sz(x);i++) cerr<<x[i]<<" "; cerr<<endl; }
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
struct Node{
int inc,dec,sum,ans,l,r;
void PRINTnode(){
cerr<<"["<<l<<","<<r<<"]"<<endl;
cerr<<"inc="<<inc<<endl;
cerr<<"dec="<<dec<<endl;
cerr<<"sum="<<sum<<endl;
cerr<<"ans="<<ans<<endl;
}
};
void Solve(){
int N,Q; cin>>N;
string S; cin>>S;
vector<Node> seg(4*(N+1));
function<Node(Node,Node)> Merge=[&](Node n1,Node n2){
Node ret;
ret.inc=max(n1.inc,n1.sum+n2.inc);
ret.dec=max(n2.dec,n2.sum+n1.dec);
ret.sum=n1.sum+n2.sum;
ret.ans=max({n1.inc+n2.ans,n1.ans+n2.dec,n1.inc+n2.dec});
ret.l=n1.l;
ret.r=n2.r;
return ret;
};
function<void(int,int,int)> Init=[&](int node,int l,int r){
if(l==r){
if(S[l-1]=='T') seg[node]={1,1,1,1,l,r};
else seg[node]={0,0,-1,0,l,r};
return;
}
Init(2*node,l,mid); Init(2*node+1,mid+1,r);
seg[node]=Merge(seg[2*node],seg[2*node+1]);
};
function<Node(int,int,int,int,int)> Query=[&](int node,int l,int r,int L,int R){
if(L<=l && r<=R){
return seg[node];
}
Node ret={0,0,0,0};
if(L<=mid) ret=Merge(ret,Query(2*node,l,mid,L,R));
if(R>mid) ret=Merge(ret,Query(2*node+1,mid+1,r,L,R));
return ret;
};
Init(1,1,N);
cin>>Q;
while(Q--){
int l,r; cin>>l>>r;
int res=Query(1,1,N,l,r).ans;
cout<<res<<endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t; t=1;
//cin>>t;
while(t--){
Solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |