#include <stdio.h>
#define N 500000
int max(int a, int b) { return a > b ? a : b; }
int ss[N * 2], pp[N * 2], qq[N * 2], xx[N * 2], n;
void build(char *cc) {
int i;
for (i = 0; i < n; i++)
if (cc[i] == 'C')
ss[n + i] = pp[n + i] = qq[n + i] = xx[n + i] = 1;
else
ss[n + i] = -1, pp[n + i] = qq[n + i] = xx[n + i] = 0;
for (i = n - 1; i > 0; i--) {
int l = i << 1 | 0, r = i << 1 | 1;
ss[i] = ss[l] + ss[r];
pp[i] = max(pp[l], ss[l] + pp[r]);
qq[i] = max(ss[r] + qq[l], qq[r]);
xx[i] = max(max(xx[l], xx[r]), qq[l] + pp[r]);
}
}
int query(int l, int r) {
int s = 0, x = 0, p = 0, q = 0;
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1) {
s += ss[l];
x = max(max(x, xx[l]), q + pp[l]);
q = max(q + ss[l], qq[l]);
l++;
}
if ((r & 1) == 0) {
s += ss[r];
x = max(max(xx[r], x), qq[r] + p);
p = max(p + ss[r], pp[r]);
r--;
}
}
x = max(x, q + p);
return x - s;
}
int main() {
static char cc[N + 1];
int q;
scanf("%d%s%d", &n, cc, &q);
build(cc);
while (q--) {
int l, r;
scanf("%d%d", &l, &r), l--, r--;
printf("%d\n", query(l, r));
}
return 0;
}
Compilation message
election.c: In function 'main':
election.c:52:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d%s%d", &n, cc, &q);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
election.c:57:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &l, &r), l--, r--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
2 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
2 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
408 KB |
Output is correct |
6 |
Correct |
33 ms |
3756 KB |
Output is correct |
7 |
Correct |
30 ms |
3692 KB |
Output is correct |
8 |
Correct |
42 ms |
3696 KB |
Output is correct |
9 |
Correct |
38 ms |
3664 KB |
Output is correct |
10 |
Correct |
45 ms |
3624 KB |
Output is correct |
11 |
Correct |
32 ms |
3820 KB |
Output is correct |
12 |
Correct |
31 ms |
3732 KB |
Output is correct |
13 |
Correct |
33 ms |
3824 KB |
Output is correct |
14 |
Correct |
38 ms |
3752 KB |
Output is correct |
15 |
Correct |
37 ms |
3764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
2 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
408 KB |
Output is correct |
6 |
Correct |
33 ms |
3756 KB |
Output is correct |
7 |
Correct |
30 ms |
3692 KB |
Output is correct |
8 |
Correct |
42 ms |
3696 KB |
Output is correct |
9 |
Correct |
38 ms |
3664 KB |
Output is correct |
10 |
Correct |
45 ms |
3624 KB |
Output is correct |
11 |
Correct |
32 ms |
3820 KB |
Output is correct |
12 |
Correct |
31 ms |
3732 KB |
Output is correct |
13 |
Correct |
33 ms |
3824 KB |
Output is correct |
14 |
Correct |
38 ms |
3752 KB |
Output is correct |
15 |
Correct |
37 ms |
3764 KB |
Output is correct |
16 |
Correct |
320 ms |
25948 KB |
Output is correct |
17 |
Correct |
287 ms |
25628 KB |
Output is correct |
18 |
Correct |
292 ms |
25936 KB |
Output is correct |
19 |
Correct |
266 ms |
25352 KB |
Output is correct |
20 |
Correct |
337 ms |
25064 KB |
Output is correct |
21 |
Correct |
345 ms |
26916 KB |
Output is correct |
22 |
Correct |
357 ms |
26736 KB |
Output is correct |
23 |
Correct |
383 ms |
27004 KB |
Output is correct |
24 |
Correct |
355 ms |
26772 KB |
Output is correct |
25 |
Correct |
363 ms |
26132 KB |
Output is correct |