#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 500002;
const int MOD = 1000000007;
struct node {
int lmx, rmx, sum, ans;
node operator+(node o) {
node ret;
ret.lmx = max(lmx, o.lmx+sum);
ret.rmx = max(rmx+o.sum, o.rmx);
ret.sum = sum+o.sum;
ret.ans = max(max(ans+o.sum, sum+o.ans), lmx+o.rmx);
return ret;
}
};
int n, q;
bool votes[MAXN];
node st[1<<20];
void build(int l=1, int r=n, int x=1) {
if (l == r) {
if (votes[l]) st[x] = {1, 1, 1, 1};
else st[x] = {0, 0, -1, 0};
} else {
int mid = (l+r)/2;
build(l, mid, 2*x);
build(mid+1, r, 2*x+1);
st[x] = st[2*x]+st[2*x+1];
}
}
node qry(int a, int b, int l=1, int r=n, int x=1) {
if (a <= l && b >= r) return st[x];
node ret = {0, 0, 0, 0};
int mid = (l+r)/2;
if (a <= mid) ret = ret+qry(a, b, l, mid, 2*x);
if (b > mid) ret = ret+qry(a, b, mid+1, r, 2*x+1);
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
char c; cin >> c;
votes[i] = c == 'T';
}
build();
cin >> q;
for (int i = 0; i < q; i++) {
int a, b; cin >> a >> b;
cout << qry(a, b).ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
60 ms |
4736 KB |
Output is correct |
7 |
Correct |
59 ms |
4856 KB |
Output is correct |
8 |
Correct |
59 ms |
4728 KB |
Output is correct |
9 |
Correct |
55 ms |
4744 KB |
Output is correct |
10 |
Correct |
60 ms |
4608 KB |
Output is correct |
11 |
Correct |
66 ms |
4816 KB |
Output is correct |
12 |
Correct |
61 ms |
4856 KB |
Output is correct |
13 |
Correct |
65 ms |
4860 KB |
Output is correct |
14 |
Correct |
61 ms |
4856 KB |
Output is correct |
15 |
Correct |
60 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
60 ms |
4736 KB |
Output is correct |
7 |
Correct |
59 ms |
4856 KB |
Output is correct |
8 |
Correct |
59 ms |
4728 KB |
Output is correct |
9 |
Correct |
55 ms |
4744 KB |
Output is correct |
10 |
Correct |
60 ms |
4608 KB |
Output is correct |
11 |
Correct |
66 ms |
4816 KB |
Output is correct |
12 |
Correct |
61 ms |
4856 KB |
Output is correct |
13 |
Correct |
65 ms |
4860 KB |
Output is correct |
14 |
Correct |
61 ms |
4856 KB |
Output is correct |
15 |
Correct |
60 ms |
4856 KB |
Output is correct |
16 |
Correct |
604 ms |
19316 KB |
Output is correct |
17 |
Correct |
511 ms |
19460 KB |
Output is correct |
18 |
Correct |
562 ms |
19448 KB |
Output is correct |
19 |
Correct |
485 ms |
18936 KB |
Output is correct |
20 |
Correct |
606 ms |
18424 KB |
Output is correct |
21 |
Correct |
621 ms |
20216 KB |
Output is correct |
22 |
Correct |
602 ms |
20088 KB |
Output is correct |
23 |
Correct |
614 ms |
20276 KB |
Output is correct |
24 |
Correct |
611 ms |
19960 KB |
Output is correct |
25 |
Correct |
608 ms |
19424 KB |
Output is correct |