#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--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
34 ms |
3668 KB |
Output is correct |
7 |
Correct |
33 ms |
3592 KB |
Output is correct |
8 |
Correct |
30 ms |
3632 KB |
Output is correct |
9 |
Correct |
34 ms |
3676 KB |
Output is correct |
10 |
Correct |
35 ms |
3532 KB |
Output is correct |
11 |
Correct |
31 ms |
3740 KB |
Output is correct |
12 |
Correct |
31 ms |
3788 KB |
Output is correct |
13 |
Correct |
30 ms |
3860 KB |
Output is correct |
14 |
Correct |
31 ms |
3796 KB |
Output is correct |
15 |
Correct |
38 ms |
3752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
34 ms |
3668 KB |
Output is correct |
7 |
Correct |
33 ms |
3592 KB |
Output is correct |
8 |
Correct |
30 ms |
3632 KB |
Output is correct |
9 |
Correct |
34 ms |
3676 KB |
Output is correct |
10 |
Correct |
35 ms |
3532 KB |
Output is correct |
11 |
Correct |
31 ms |
3740 KB |
Output is correct |
12 |
Correct |
31 ms |
3788 KB |
Output is correct |
13 |
Correct |
30 ms |
3860 KB |
Output is correct |
14 |
Correct |
31 ms |
3796 KB |
Output is correct |
15 |
Correct |
38 ms |
3752 KB |
Output is correct |
16 |
Correct |
315 ms |
25984 KB |
Output is correct |
17 |
Correct |
274 ms |
25556 KB |
Output is correct |
18 |
Correct |
311 ms |
26008 KB |
Output is correct |
19 |
Correct |
267 ms |
25272 KB |
Output is correct |
20 |
Correct |
325 ms |
25100 KB |
Output is correct |
21 |
Correct |
321 ms |
26912 KB |
Output is correct |
22 |
Correct |
327 ms |
26788 KB |
Output is correct |
23 |
Correct |
318 ms |
27020 KB |
Output is correct |
24 |
Correct |
359 ms |
26576 KB |
Output is correct |
25 |
Correct |
300 ms |
26148 KB |
Output is correct |