#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAX = 500050;
int nPer;
int nQue;
string S;
struct node {
int ans, sum, pre, suf;
node(int value = 0) {
sum = value;
ans = max(0, value);
pre = max(0, value);
suf = max(0, value);
}
} st[(1 << 20) + 5];
node combine(const node &lf, const node &rg) {
node res;
res.sum = lf.sum + rg.sum;
res.pre = max(lf.pre, lf.sum + rg.pre);
res.suf = max(rg.suf, lf.suf + rg.sum);
res.ans = max({lf.ans + rg.sum, lf.sum + rg.ans, lf.pre + rg.suf});
return res;
}
void build(int id, int l, int r) {
if (l == r) {
st[id] = node(S[l] == 'C' ? -1 : +1);
return;
}
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = combine(st[id << 1], st[id << 1 | 1]);
}
node get(int id, int l, int r, int u, int v) {
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1;
if (u <= mid && mid + 1 <= v)
return combine(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
if (u <= mid)
return get(id << 1, l, mid, u, v);
return get(id << 1 | 1, mid + 1, r, u, v);
}
void process(void) {
cin >> nPer >> S >> nQue; S = ' ' + S;
build(1, 1, nPer);
while (nQue--) {
int l, r; cin >> l >> r;
cout << get(1, 1, nPer, l, r).ans << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
process();
return 0;
}
Compilation message
election.cpp: In function 'void build(int, int, int)':
election.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
election.cpp: In function 'node get(int, int, int, int, int)':
election.cpp:56:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16732 KB |
Output is correct |
3 |
Correct |
5 ms |
16732 KB |
Output is correct |
4 |
Correct |
5 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16732 KB |
Output is correct |
3 |
Correct |
5 ms |
16732 KB |
Output is correct |
4 |
Correct |
5 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16888 KB |
Output is correct |
6 |
Correct |
37 ms |
17060 KB |
Output is correct |
7 |
Correct |
40 ms |
17240 KB |
Output is correct |
8 |
Correct |
41 ms |
17228 KB |
Output is correct |
9 |
Correct |
31 ms |
17228 KB |
Output is correct |
10 |
Correct |
39 ms |
16984 KB |
Output is correct |
11 |
Correct |
37 ms |
17340 KB |
Output is correct |
12 |
Correct |
37 ms |
17228 KB |
Output is correct |
13 |
Correct |
39 ms |
17240 KB |
Output is correct |
14 |
Correct |
39 ms |
17200 KB |
Output is correct |
15 |
Correct |
37 ms |
17240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16732 KB |
Output is correct |
3 |
Correct |
5 ms |
16732 KB |
Output is correct |
4 |
Correct |
5 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16888 KB |
Output is correct |
6 |
Correct |
37 ms |
17060 KB |
Output is correct |
7 |
Correct |
40 ms |
17240 KB |
Output is correct |
8 |
Correct |
41 ms |
17228 KB |
Output is correct |
9 |
Correct |
31 ms |
17228 KB |
Output is correct |
10 |
Correct |
39 ms |
16984 KB |
Output is correct |
11 |
Correct |
37 ms |
17340 KB |
Output is correct |
12 |
Correct |
37 ms |
17228 KB |
Output is correct |
13 |
Correct |
39 ms |
17240 KB |
Output is correct |
14 |
Correct |
39 ms |
17200 KB |
Output is correct |
15 |
Correct |
37 ms |
17240 KB |
Output is correct |
16 |
Correct |
326 ms |
19368 KB |
Output is correct |
17 |
Correct |
289 ms |
19268 KB |
Output is correct |
18 |
Correct |
300 ms |
19340 KB |
Output is correct |
19 |
Correct |
239 ms |
18936 KB |
Output is correct |
20 |
Correct |
300 ms |
18432 KB |
Output is correct |
21 |
Correct |
323 ms |
20132 KB |
Output is correct |
22 |
Correct |
301 ms |
20104 KB |
Output is correct |
23 |
Correct |
337 ms |
20192 KB |
Output is correct |
24 |
Correct |
303 ms |
20216 KB |
Output is correct |
25 |
Correct |
328 ms |
19448 KB |
Output is correct |