This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
const ll mod2=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=501000;
int n,q;
string s;
struct node {
int pf,sf,s,f;
}nd[4*N];
node comb(node x,node y) {
node ret;
ret.pf=max(x.pf,x.s+y.pf);
ret.sf=max(y.sf,y.s+x.sf);
ret.s=x.s+y.s;
ret.f=max({x.pf+y.sf,x.f+y.s,x.s+y.f});
return ret;
}
void build(int p,int l,int r) {
if (l==r) {
if (s[l]=='T') {
nd[p].pf=nd[p].sf=nd[p].s=nd[p].f=1;
} else {
nd[p].pf=nd[p].sf=nd[p].f=0;
nd[p].s=-1;
}
} else {
int md=l+r>>1;
build(p+p,l,md);
build(p+p+1,md+1,r);
nd[p]=comb(nd[p+p],nd[p+p+1]);
}
}
node get(int p,int l,int r,int ql,int qr) {
if (ql>r||qr<l) return {0,0,0,0};
if (ql<=l&&r<=qr) return nd[p];
int md=l+r>>1;
return comb(get(p+p,l,md,ql,qr),get(p+p+1,md+1,r,ql,qr));
}
int main() {
cin>>n;
cin>>s;
cin>>q;
build(1,0,n-1);
while (q--) {
int l,r;
cin>>l>>r;
--l; --r;
cout<<get(1,0,n-1,l,r).f<<"\n";
}
}
Compilation message (stderr)
election.cpp: In function 'void build(int, int, int)':
election.cpp:47:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int md=l+r>>1;
| ~^~
election.cpp: In function 'node get(int, int, int, int, int)':
election.cpp:56:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int md=l+r>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |