// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node {
bool id = 0;
//fixed -> no negative pref sum
//not fixed -> any pref sum
int ans = 0;
int sum = 0;
int mx_suf = 0;
int mx_pref = 0;
};
node comb(node a, node b) {
node c;
if(a.id) return b;
if(b.id) return a;
c.ans = max(max(a.ans + b.sum, b.ans + a.sum), a.mx_pref + b.mx_suf);
c.sum = a.sum + b.sum;
c.mx_suf = max(b.mx_suf, a.mx_suf + b.sum);
c.mx_pref = max(a.mx_pref, b.mx_pref + a.sum);
return c;
}
node idnode;
const int N = 5e5 + 42;
node st[4 * N];
string s;
void build(int v, int tl, int tr) {
if(tl == tr) {
if(s[tl] == 'C') {
st[v].ans = 0;
st[v].sum = -1;
st[v].mx_suf = 0;
st[v].mx_pref = 0;
} else {
st[v].ans = 1;
st[v].sum = 1;
st[v].mx_suf = 1;
st[v].mx_pref = 1;
}
} else {
int tm = (tl + tr) / 2;
build((v << 1), tl, tm);
build((v << 1) + 1, tm + 1, tr);
st[v] = comb(st[(v << 1)], st[(v << 1) + 1]);
}
}
node get(int v, int tl, int tr, int l, int r) {
if(l > r) return idnode;
if(l == tl && r == tr) return st[v];
int tm = (tl + tr) / 2;
return comb(get((v << 1), tl, tm, l, min(r, tm)), get((v << 1) + 1, tm + 1, tr, max(l, tm + 1), r));
}
void solve() {
int n, q;
cin >> n;
cin >> s;
build(1, 0, n - 1);
cin >> q;
while(q--) {
int l, r;
cin >> l >> r; l--; r--;
node res = get(1, 0, n - 1, l, r);
cout << res.ans << '\n';
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
idnode.id = 1;
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
444 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
444 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
60 ms |
6784 KB |
Output is correct |
7 |
Correct |
56 ms |
6760 KB |
Output is correct |
8 |
Correct |
58 ms |
6732 KB |
Output is correct |
9 |
Correct |
47 ms |
6668 KB |
Output is correct |
10 |
Correct |
55 ms |
6692 KB |
Output is correct |
11 |
Correct |
61 ms |
6900 KB |
Output is correct |
12 |
Correct |
56 ms |
6864 KB |
Output is correct |
13 |
Correct |
56 ms |
6868 KB |
Output is correct |
14 |
Correct |
55 ms |
6872 KB |
Output is correct |
15 |
Correct |
57 ms |
6776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
444 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
60 ms |
6784 KB |
Output is correct |
7 |
Correct |
56 ms |
6760 KB |
Output is correct |
8 |
Correct |
58 ms |
6732 KB |
Output is correct |
9 |
Correct |
47 ms |
6668 KB |
Output is correct |
10 |
Correct |
55 ms |
6692 KB |
Output is correct |
11 |
Correct |
61 ms |
6900 KB |
Output is correct |
12 |
Correct |
56 ms |
6864 KB |
Output is correct |
13 |
Correct |
56 ms |
6868 KB |
Output is correct |
14 |
Correct |
55 ms |
6872 KB |
Output is correct |
15 |
Correct |
57 ms |
6776 KB |
Output is correct |
16 |
Correct |
540 ms |
31124 KB |
Output is correct |
17 |
Correct |
481 ms |
30752 KB |
Output is correct |
18 |
Correct |
506 ms |
30888 KB |
Output is correct |
19 |
Correct |
397 ms |
30392 KB |
Output is correct |
20 |
Correct |
550 ms |
30196 KB |
Output is correct |
21 |
Correct |
538 ms |
31996 KB |
Output is correct |
22 |
Correct |
533 ms |
31916 KB |
Output is correct |
23 |
Correct |
528 ms |
32112 KB |
Output is correct |
24 |
Correct |
535 ms |
31744 KB |
Output is correct |
25 |
Correct |
533 ms |
31276 KB |
Output is correct |