#include <bits/stdc++.h>
using namespace std; const int MX = 5e5 + 5; struct node { int sum, ans, mn, mx; node() { sum = 0, ans = 0, mn = 0, mx = 0; } node(int a, int b, int c, int d): sum(a), ans(b), mn(c), mx(d) { } } seg[4 * MX]; node comb(node &a, node &b) { node ret; ret.sum = a.sum+b.sum; ret.mn = min(a.mn, b.mn + a.sum); ret.mx = max(a.mx, b.mx + a.sum); ret.ans = max({a.ans - b.sum, b.ans-a.sum, b.mx + a.sum - a.mn - ret.sum}); return ret; } int sz = 1; void build(string &s, int n, int x=0, int lx=0, int rx=sz) { if(rx-lx==1) { if(lx<n) { if(s[lx]=='C') seg[x] = node(1, -1, 0, 1); else seg[x] = node(-1, 1, -1, 0); } else seg[x] = node(); return; } int m = (lx+rx)/2; build(s, n, 2*x+1, lx, m), build(s, n, 2*x+2, m, rx); seg[x] = comb(seg[2*x+1], seg[2*x+2]); } node query(int l, int r,int x=0, int lx=0, int rx=sz) { if(lx>=r || rx<=l) return node(); if(lx>=l && rx<=r) return seg[x]; int m = (lx+rx)/2; node a = query(l, r, 2*x+1, lx, m), b = query(l, r, 2*x+2, m, rx); return comb(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; while(sz<n) sz*=2; string s; cin >> s; build(s, n); int q; cin >> q; while(q--) { int l, r; cin >> l >> r; --l, --r; node x = query(l, r+1); cout << max(0, x.ans) << "\n"; } }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
31596 KB |
Output is correct |
2 |
Correct |
20 ms |
31596 KB |
Output is correct |
3 |
Correct |
21 ms |
31724 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
20 ms |
31596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
31596 KB |
Output is correct |
2 |
Correct |
20 ms |
31596 KB |
Output is correct |
3 |
Correct |
21 ms |
31724 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
20 ms |
31596 KB |
Output is correct |
6 |
Correct |
90 ms |
32272 KB |
Output is correct |
7 |
Correct |
81 ms |
32108 KB |
Output is correct |
8 |
Correct |
94 ms |
32108 KB |
Output is correct |
9 |
Correct |
78 ms |
31980 KB |
Output is correct |
10 |
Correct |
87 ms |
31988 KB |
Output is correct |
11 |
Correct |
86 ms |
32236 KB |
Output is correct |
12 |
Correct |
102 ms |
32236 KB |
Output is correct |
13 |
Correct |
90 ms |
32236 KB |
Output is correct |
14 |
Correct |
99 ms |
32204 KB |
Output is correct |
15 |
Correct |
95 ms |
32236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
31596 KB |
Output is correct |
2 |
Correct |
20 ms |
31596 KB |
Output is correct |
3 |
Correct |
21 ms |
31724 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
20 ms |
31596 KB |
Output is correct |
6 |
Correct |
90 ms |
32272 KB |
Output is correct |
7 |
Correct |
81 ms |
32108 KB |
Output is correct |
8 |
Correct |
94 ms |
32108 KB |
Output is correct |
9 |
Correct |
78 ms |
31980 KB |
Output is correct |
10 |
Correct |
87 ms |
31988 KB |
Output is correct |
11 |
Correct |
86 ms |
32236 KB |
Output is correct |
12 |
Correct |
102 ms |
32236 KB |
Output is correct |
13 |
Correct |
90 ms |
32236 KB |
Output is correct |
14 |
Correct |
99 ms |
32204 KB |
Output is correct |
15 |
Correct |
95 ms |
32236 KB |
Output is correct |
16 |
Correct |
735 ms |
34176 KB |
Output is correct |
17 |
Correct |
570 ms |
34304 KB |
Output is correct |
18 |
Correct |
660 ms |
34252 KB |
Output is correct |
19 |
Correct |
587 ms |
33792 KB |
Output is correct |
20 |
Correct |
720 ms |
33652 KB |
Output is correct |
21 |
Correct |
689 ms |
35164 KB |
Output is correct |
22 |
Correct |
682 ms |
34944 KB |
Output is correct |
23 |
Correct |
765 ms |
35284 KB |
Output is correct |
24 |
Correct |
688 ms |
34944 KB |
Output is correct |
25 |
Correct |
712 ms |
34560 KB |
Output is correct |