#include <bits/stdc++.h>
using namespace std;
/// 123
class segtree_t {
public:
segtree_t *left, *right;
int l, r, m, val, lazy;
segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(0), lazy(0) {
if (l == r) return;
left = new segtree_t(l, m);
right = new segtree_t(m + 1, r);
}
void Update(int s, int t, int x) {
if (r < s or l > t) return;
if (s <= l && r <= t) {
val += x;
lazy += x;
return;
}
Down();
left->Update(s, t, x);
right->Update(s, t, x);
Up();
}
int Get(int s, int t) {
if (r < s or l > t) return -1e9;
if (s <= l && r <= t) return val;
Down();
return max(left->Get(s, t), right->Get(s, t));
}
void Up() {
val = max(left->val, right->val);
}
void Down() {
left->lazy += lazy;
right->lazy += lazy;
right->val += lazy;
left->val += lazy;
lazy = 0;
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
string s;
cin >> n >> s >> q;
vector<int> l(q), r(q);
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
}
vector<int> ord(q);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return r[i] < r[j];
});
priority_queue<int> pq;
map<char, int> c;
c['C'] = -1;
c['T'] = +1;
segtree_t *tree = new segtree_t(0, n);
for (int i = 0; i < n; i++) tree->Update(i + 1, n, c[s[i]]);
vector<int> ans(q, -1);
for (int i = 0, j = 0; i < n; i++) {
if (s[i] == 'T') {
tree->Update(i + 1, n, -c[s[i]]);
pq.emplace(-i);
} else {
if (pq.size()) {
tree->Update(-pq.top() + 1, n, c[s[-pq.top()]]);
pq.pop();
}
}
while (j < q && r[ord[j]] == i) {
ans[ord[j]] = pq.size() + tree->Get(l[ord[j]], r[ord[j]] + 1) - tree->Get(l[ord[j]], l[ord[j]]);
j++;
}
}
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}
Compilation message
election.cpp: In constructor 'segtree_t::segtree_t(int, int)':
election.cpp:11:51: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
11 | segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(0), lazy(0) {
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |