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 f first
#define s second
#define int long long
const int maxn = 5e5 + 69;
struct Node {
int p, s, ans, sm;
} st[4 * maxn];
int n, q;
string s;
Node combine(Node a, Node b) {
Node ret;
ret.p = max(a.p, a.sm + b.p);
ret.s = max(b.s, b.sm + a.s);
ret.sm = a.sm + b.sm;
ret.ans = max({a.sm + b.s, b.sm + a.p, a.p + b.s});
return ret;
}
void build(int v, int tl, int tr) {
if (tl == tr) {
if (s[tl - 1] == 'T')
st[v].p = st[v].s = st[v].sm = st[v].ans = 1;
else {
st[v].p = st[v].s = st[v].ans = 0;
st[v].sm = -1;
}
}
else {
int mid = (tl + tr) / 2;
build(v * 2, tl, mid);
build(v * 2 + 1, mid + 1, tr);
st[v] = combine(st[v * 2], st[v * 2 + 1]);
}
}
Node qry(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l)
return {0, 0, 0, 0};
else if (tl >= l && tr <= r)
return st[v];
else {
int mid = (tl + tr) / 2;
return combine(qry(v * 2, tl, mid, l, r), qry(v * 2 + 1, mid + 1, tr, l, r));
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> s >> q;
build(1, 1, n);
for (int i = 1;i <= q;i++) {
int l, r;
cin >> l >> r;
cout << qry(1, 1, n, l, r).ans << '\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... |