#include <bits/stdc++.h>
using namespace std;
struct Node {
long long mpf = 0;
long long msf = 0;
long long sum = 0;
long long ans = 0;
};
struct SegTree {
vector<Node> segTree;
int realSize;
SegTree(int N, string &data) {
realSize = 1<<(int)ceil(log2(N));
segTree.resize(realSize * 2);
for (int i = 0; i < N; i++) {
if (data[i] == 'T') segTree[realSize + i] = {1, 1, 1, 1};
else segTree[realSize + i] = {0, 0, -1, 0};
}
for (int i = realSize - 1; i > 0; i--) {
segTree[i].mpf = max(segTree[i * 2].mpf, segTree[i * 2].sum + segTree[i * 2 + 1].mpf);
segTree[i].msf = max(segTree[i * 2 + 1].msf, segTree[i * 2 + 1].sum + segTree[i * 2].msf);
segTree[i].sum = segTree[i * 2].sum + segTree[i * 2 + 1].sum;
segTree[i].ans = max({
segTree[i * 2].sum + segTree[i * 2 + 1].ans,
segTree[i * 2 + 1].sum + segTree[i * 2].ans,
segTree[i * 2].mpf + segTree[i * 2 + 1].msf
});
}
}
Node qry(int u, int left, int right, int l, int r) {
if (right <= l || left >= r) return Node{0, 0, 0, 0};
if (left >= l && right <= r) return segTree[u];
Node lft = qry(u * 2, left, (left + right) / 2, l, r);
Node rgt = qry(u*2+1, (left + right)/ 2, right, l, r);
Node ret;
ret.mpf = max(lft.mpf, lft.sum + rgt.mpf);
ret.msf = max(rgt.msf, rgt.sum + lft.msf);
ret.sum = lft.sum + rgt.sum;
ret.ans = max({
lft.sum + rgt.ans,
rgt.sum + lft.ans,
lft.mpf + rgt.msf
});
return ret;
}
long long qry(int l, int r) {
return qry(1, 0, realSize, l, r).ans;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
string s; cin >> s;
int Q; cin >> Q;
SegTree st(N, s);
while (Q--) {
int l, r;
cin >> l >> r;
l--;
cout << st.qry(l, r) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
62 ms |
9792 KB |
Output is correct |
7 |
Correct |
59 ms |
9824 KB |
Output is correct |
8 |
Correct |
64 ms |
9844 KB |
Output is correct |
9 |
Correct |
60 ms |
9800 KB |
Output is correct |
10 |
Correct |
70 ms |
9760 KB |
Output is correct |
11 |
Correct |
61 ms |
9984 KB |
Output is correct |
12 |
Correct |
65 ms |
9956 KB |
Output is correct |
13 |
Correct |
62 ms |
9972 KB |
Output is correct |
14 |
Correct |
66 ms |
9928 KB |
Output is correct |
15 |
Correct |
86 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
62 ms |
9792 KB |
Output is correct |
7 |
Correct |
59 ms |
9824 KB |
Output is correct |
8 |
Correct |
64 ms |
9844 KB |
Output is correct |
9 |
Correct |
60 ms |
9800 KB |
Output is correct |
10 |
Correct |
70 ms |
9760 KB |
Output is correct |
11 |
Correct |
61 ms |
9984 KB |
Output is correct |
12 |
Correct |
65 ms |
9956 KB |
Output is correct |
13 |
Correct |
62 ms |
9972 KB |
Output is correct |
14 |
Correct |
66 ms |
9928 KB |
Output is correct |
15 |
Correct |
86 ms |
9856 KB |
Output is correct |
16 |
Correct |
612 ms |
40452 KB |
Output is correct |
17 |
Correct |
509 ms |
40468 KB |
Output is correct |
18 |
Correct |
593 ms |
40576 KB |
Output is correct |
19 |
Correct |
423 ms |
39900 KB |
Output is correct |
20 |
Correct |
556 ms |
39552 KB |
Output is correct |
21 |
Correct |
585 ms |
41364 KB |
Output is correct |
22 |
Correct |
616 ms |
41180 KB |
Output is correct |
23 |
Correct |
633 ms |
41416 KB |
Output is correct |
24 |
Correct |
596 ms |
41092 KB |
Output is correct |
25 |
Correct |
627 ms |
40536 KB |
Output is correct |