#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 2e6+10;
string s, s1;
array<int, 2> st1[mxN], st2[mxN];
array<int, 2> comb(array<int, 2> a, array<int, 2> b) {
array<int, 2> ans = {0, 0};
ans[0] = max(a[0], max(0, b[0] - a[1]));
ans[1] = b[1] + a[1];
return ans;
}
void build(int node, int l, int r) {
if(l == r) {
st1[node] = {s[l] == 'T', s[l] == 'C' ? 1 : -1};
st2[node] = {s1[l] == 'T', s1[l] == 'C' ? 1 : -1};
return ;
}
int mid = (l+r)/2;
build(node*2, l, mid);
build(node*2+1, mid+1, r);
st1[node] = comb(st1[node*2], st1[node*2+1]);
st2[node] = comb(st2[node*2], st2[node*2+1]);
}
array<int, 2> qry(int node, int l, int r, int l1, int r1, int flag) {
if(l1 <= l && r <= r1) {
if(flag == 1) return st1[node];
else return st2[node];
}
if(l > r1 || r < l1) return {0, 0};
int mid = (l+r)/2;
return comb(qry(node*2, l, mid, l1, r1, flag),
qry(node*2+1, mid+1, r, l1, r1, flag));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
cin >> s;
s1 = s;
reverse(s1.begin(), s1.end());
int q;
cin >> q;
build(1, 0, n-1);
while(q--) {
int l, r;
cin >> l >> r;
--l; --r;
array<int, 2> now = qry(1, 0, n-1, l, r, 1);
array<int, 2> now1 = qry(1, 0, n-1, n-r-1, n-l-1, 2);
cout << max(now[0], now1[0]) << '\n';
}
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... |