#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 5e5;
const int inf = 1e9;
int n, q;
vector<pii> ranges[MAXN];
int pre[MAXN+1];
pii operator+(pii a, pii b) {
return pii(a.first+b.first, a.second+b.second);
}
class stree {
public:
int lz = 0, mxh = -inf;
// int sum = 0;
stree *l = nullptr;
stree *r = nullptr;
int lp, rp;
stree(int lv, int rv) {
lp = lv;
rp = rv;
if (lp == rp) {
mxh = pre[lp];
}
else {
int mid = (lp+rp)/2;
l = new stree(lp, mid);
r = new stree(mid+1, rp);
mxh = max(l->mxh, r->mxh);
}
}
// pii query(int lv, int rv) { // height, sum
// if (lp > rv || rp < lv) return pii(0, 0);
// if (lp >= rv && rp <= rv) return pii(mxh, sum);
// return l->query(lv, rv)+r->query(lv, rv)+pii(lz, 0);
// }
int query(int lv, int rv) { // height, sum
if (lp > rv || rp < lv) return -inf;
if (lp >= lv && rp <= rv) return mxh;
return max(l->query(lv, rv), r->query(lv, rv))+lz;
}
void height_update(int lv, int rv, int v) {
if (lp > rv || rp < lv) return;
if (lp >= lv && rp <= rv) {
lz += v;
mxh += v;
return;
}
l->height_update(lv, rv, v);
r->height_update(lv, rv, v);
mxh = lz+max(l->mxh, r->mxh);
}
// void sum_upd(int p, int v) {
// if (lp > p || rp < p) return;
// if (lp == rp) {
// sum += v;
// return;
// }
// l->sum_upd(p, v);
// r->sum_upd(p, v);
// sum = l->sum+r->sum;
// }
};
stree *tree;
vector<pii> queries[MAXN]; // right point, id
int qansws[MAXN];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
string s; cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'C') pre[i+1] = pre[i]+1;
else pre[i+1] = pre[i]-1;
}
tree = new stree(0, n);
cin >> q;
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r; l--;
queries[l].push_back(pii(r, i));
}
vector<int> mon_stack;
mon_stack.push_back(n);
for (int i = n-1; i >= 0; i--) {
if (pre[i] > pre[i+1]) {
// tree->sum_upd(i+1, -1);
tree->height_update(i+2, n, 1);
}
else {
mon_stack.pop_back();
if (!mon_stack.empty()) {
int nxt = mon_stack.back();
// tree->sum_upd(nxt, 1);
tree->height_update(nxt+1, n, -1);
mon_stack.pop_back();
}
}
mon_stack.push_back(i);
for (pii p: queries[i]) {
qansws[p.second] = tree->query(i, p.first)-pre[p.first];
}
}
for (int i = 0; i < q; i++) cout << qansws[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24092 KB |
Output is correct |
2 |
Correct |
14 ms |
24088 KB |
Output is correct |
3 |
Correct |
14 ms |
24020 KB |
Output is correct |
4 |
Correct |
14 ms |
24068 KB |
Output is correct |
5 |
Correct |
14 ms |
24064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24092 KB |
Output is correct |
2 |
Correct |
14 ms |
24088 KB |
Output is correct |
3 |
Correct |
14 ms |
24020 KB |
Output is correct |
4 |
Correct |
14 ms |
24068 KB |
Output is correct |
5 |
Correct |
14 ms |
24064 KB |
Output is correct |
6 |
Correct |
82 ms |
33608 KB |
Output is correct |
7 |
Correct |
69 ms |
33100 KB |
Output is correct |
8 |
Correct |
71 ms |
33224 KB |
Output is correct |
9 |
Correct |
69 ms |
33548 KB |
Output is correct |
10 |
Correct |
79 ms |
33548 KB |
Output is correct |
11 |
Correct |
83 ms |
33836 KB |
Output is correct |
12 |
Correct |
87 ms |
33740 KB |
Output is correct |
13 |
Correct |
76 ms |
33996 KB |
Output is correct |
14 |
Correct |
79 ms |
33748 KB |
Output is correct |
15 |
Correct |
79 ms |
33628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24092 KB |
Output is correct |
2 |
Correct |
14 ms |
24088 KB |
Output is correct |
3 |
Correct |
14 ms |
24020 KB |
Output is correct |
4 |
Correct |
14 ms |
24068 KB |
Output is correct |
5 |
Correct |
14 ms |
24064 KB |
Output is correct |
6 |
Correct |
82 ms |
33608 KB |
Output is correct |
7 |
Correct |
69 ms |
33100 KB |
Output is correct |
8 |
Correct |
71 ms |
33224 KB |
Output is correct |
9 |
Correct |
69 ms |
33548 KB |
Output is correct |
10 |
Correct |
79 ms |
33548 KB |
Output is correct |
11 |
Correct |
83 ms |
33836 KB |
Output is correct |
12 |
Correct |
87 ms |
33740 KB |
Output is correct |
13 |
Correct |
76 ms |
33996 KB |
Output is correct |
14 |
Correct |
79 ms |
33748 KB |
Output is correct |
15 |
Correct |
79 ms |
33628 KB |
Output is correct |
16 |
Correct |
738 ms |
89864 KB |
Output is correct |
17 |
Correct |
559 ms |
86284 KB |
Output is correct |
18 |
Correct |
646 ms |
87104 KB |
Output is correct |
19 |
Correct |
595 ms |
88700 KB |
Output is correct |
20 |
Correct |
770 ms |
89044 KB |
Output is correct |
21 |
Correct |
773 ms |
91392 KB |
Output is correct |
22 |
Correct |
726 ms |
90856 KB |
Output is correct |
23 |
Correct |
831 ms |
91796 KB |
Output is correct |
24 |
Correct |
802 ms |
91012 KB |
Output is correct |
25 |
Correct |
766 ms |
90116 KB |
Output is correct |