#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<int>;
using db = double;
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound
struct node{
int pref, suff, sum, ans;
node operator+(node b) {
node res;
res.pref = max(pref, sum + b.pref);
res.suff = max(b.suff, suff + b.sum);
res.sum = sum + b.sum;
res.ans = max({pref + b.suff, ans + b.sum, b.ans + sum});
return res;
}
};
node st[4 * 500005];
string s; int n, q;
void build(int v, int l, int r) {
if(l == r) {
if(s[l] == 'T') st[v] = {1, 1, 1, 1};
else st[v] = {0, 0, -1, 0};
return;
}
build(v * 2, l, (l + r) / 2);
build(v * 2 + 1, (l + r) / 2 + 1, r);
st[v] = st[v * 2] + st[v * 2 + 1];
}
node query(int v, int l, int r, int lo, int hi) {
if(l > hi || r < lo) return {0, 0, 0, 0};
if(l >= lo && r <= hi) return st[v];
return query(v * 2, l, (l + r) / 2, lo, hi) + query(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi);
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s >> q;
build(1, 0, n - 1);
while(q--) {
int l, r; cin >> l >> r; l--; r--;
cout << query(1, 0, n - 1, l, r).ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
45 ms |
5744 KB |
Output is correct |
7 |
Correct |
42 ms |
5712 KB |
Output is correct |
8 |
Correct |
46 ms |
5712 KB |
Output is correct |
9 |
Correct |
38 ms |
5680 KB |
Output is correct |
10 |
Correct |
44 ms |
5676 KB |
Output is correct |
11 |
Correct |
46 ms |
5832 KB |
Output is correct |
12 |
Correct |
46 ms |
5832 KB |
Output is correct |
13 |
Correct |
45 ms |
5816 KB |
Output is correct |
14 |
Correct |
55 ms |
6052 KB |
Output is correct |
15 |
Correct |
45 ms |
5796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
45 ms |
5744 KB |
Output is correct |
7 |
Correct |
42 ms |
5712 KB |
Output is correct |
8 |
Correct |
46 ms |
5712 KB |
Output is correct |
9 |
Correct |
38 ms |
5680 KB |
Output is correct |
10 |
Correct |
44 ms |
5676 KB |
Output is correct |
11 |
Correct |
46 ms |
5832 KB |
Output is correct |
12 |
Correct |
46 ms |
5832 KB |
Output is correct |
13 |
Correct |
45 ms |
5816 KB |
Output is correct |
14 |
Correct |
55 ms |
6052 KB |
Output is correct |
15 |
Correct |
45 ms |
5796 KB |
Output is correct |
16 |
Correct |
446 ms |
27020 KB |
Output is correct |
17 |
Correct |
344 ms |
26452 KB |
Output is correct |
18 |
Correct |
395 ms |
26840 KB |
Output is correct |
19 |
Correct |
331 ms |
26236 KB |
Output is correct |
20 |
Correct |
421 ms |
26072 KB |
Output is correct |
21 |
Correct |
423 ms |
27764 KB |
Output is correct |
22 |
Correct |
433 ms |
27804 KB |
Output is correct |
23 |
Correct |
431 ms |
27992 KB |
Output is correct |
24 |
Correct |
445 ms |
27632 KB |
Output is correct |
25 |
Correct |
453 ms |
27056 KB |
Output is correct |