#include <bits/stdc++.h>
using namespace std;
#define int long long
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pp pop_back
#define endl '\n'
#define ff first
#define ss second
#define stop exit(0)
#define sz(x) (int)x.size()
#define pause system("pause")
#define all(x) (x).begin(), (x).end()
#define deb(x) cout << #x << "-" << x << endl
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef string str;
typedef long long ll;
typedef vector<int> vii;
typedef pair<int, int> pii;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;
const int mod = 998244353;
const int MOD = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
const double PI = 2 * acos(0.0);
const int N = 2e5+5;
int n;
string c;
map <int, int> id;
set<int> s;
int a[N];
int pref[N],suf[N],preft[N];
struct NODE{
int sum = 0; int pref = 0;
int suf = 0;
int ans = 0;
} t[4*N];
NODE merge(NODE a, NODE b) {
NODE res;
res.sum = a.sum + b.sum;
res.pref =max(a.pref, a.sum + b.pref);
res.suf = max(a.suf + b.sum,b.suf);
res.ans = max(max(a.ans+b.sum,b.ans+a.sum),a.pref+b.suf);
return res;
}
void build(int node, int l, int r) {
if (l == r) {
if (c[l-1] == 'T') {
t[node] = {1,1,1,1};
} else {
t[node] = {-1,0,0,0};
}
return;
}
int mid = (l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
t[node] = merge(t[node*2],t[node*2+1]);
}
NODE getans(int node, int l, int r, int L, int R) {
if (r < L || l > R) return {0,0,0,0};
if (l >= L && r <= R) return t[node];
int mid = (l+r)/2;
return merge(getans(node*2,l,mid,L,R), getans(node*2+1,mid+1,r,L,R));
}
inline void test_case() {
cin >> n;
cin >> c;
build(1,1,n);
int q;
cin >> q;
while (q--) {
int l; int r;
cin >> l >> r;
cout << getans(1,1,n,l,r).ans << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) test_case();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |