#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define endl '\n'
struct node
{
int mr=0, ml=0, sum=0, ans;
};
const int N=(1ll<<20);
node tree[4*N];
node merge(node x, node y)
{
node res;
res.mr=max(y.sum+x.mr, y.mr);
res.ml=max(x.sum+y.ml, x.ml);
res.sum=x.sum+y.sum;
res.ans=max({x.sum+y.ans, x.ans+y.sum, x.ml+y.mr});
return res;
}
void update(int x, int y)
{
x+=N;
tree[x].ml=max(0, y);
tree[x].mr=max(0, y);
tree[x].sum=y;
while(x>1)
{
x/=2;
tree[x]=merge(tree[2*x], tree[2*x+1]);
}
}
node query(int l, int r, int ql, int qr, int v)
{
if(r<ql||l>qr) return {0, 0, 0};
if(ql<=l&&r<=qr) return tree[v];
int mid=(l+r)/2;
return merge(query(l, mid, ql, qr, 2*v), query(mid+1, r, ql, qr, 2*v+1));
}
int main()
{
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n;
string a;
cin >> a;
for(int i = 0;i < n;i++) update(i, (a[i]=='T'?1:-1));
int q;
cin >> q;
while(q--)
{
int l, r;
cin >> l >> r;
l--, r--;
node x=query(0, N-1, l, r, 1);
cout << x.ans << endl;
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |