#include<bits/stdc++.h>
using namespace std;
int N, M, getCount[500009][19], nxt[500009][19], nxtS[500009], nextC[500009], s[500009], a[500009], maxS[19][500009], minSuf[500009][19], lg[500009], lst[1000009];
char sir[500009];
void buildRMQ ()
{
for (int i=1; i<=N; i++)
maxS[0][i] = s[i];
for (int i=2; i<=N; i++)
lg[i] = lg[i >> 1] + 1;
for (int i=1; i<=lg[N]; i++)
for (int j=1; j + (1 << i) - 1 <= N; j++)
maxS[i][j] = max (maxS[i - 1][j], maxS[i - 1][j + (1 << (i - 1))]);
}
int getMinSuf (int i, int j)
{
int p = lg[j - i + 1];
///s[j] - max (s[i - 1 <= p <= j])
int curr = max (max (maxS[p][i], maxS[p][j - (1 << p) + 1]), s[i - 1]);
return s[j] - curr;
}
int getS (int i, int j)
{
return s[j] - s[i - 1];
}
int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);
scanf ("%d\n", &N);
scanf ("%s", sir + 1);
for (int i=1; i<=N; i++)
a[i] = (sir[i] == 'C' ? +1 : -1),
s[i] = s[i - 1] + a[i];
buildRMQ ();
for (int i=N; i>=1; i--)
if (sir[i] == 'C') nextC[i] = i;
else nextC[i] = nextC[i + 1];
for (int i=N; i>=0; i--)
nxtS[i] = lst[s[i] + N],
lst[s[i] + N] = i;
for (int i=1; i<=N; i++)
if (nxtS[i - 1] == 0) nxt[i][0] = N + 1;
else
nxt[i][0] = nextC[nxtS[i - 1] + 1],
minSuf[i][0] = getMinSuf (i, nxtS[i - 1]),
getCount[i][0] = nxt[i][0] - (nxtS[i - 1] + 1);
for (int i=1; i<=N; i++)
if (nxt[i][0] == 0)
nxt[i][0] = N + 1;
for (int i=1; i<=lg[N]; i++)
for (int j=1; j<=N; j++)
nxt[j][i] = nxt[nxt[j][i - 1]][i - 1],
minSuf[j][i] = min (minSuf[j][i - 1], minSuf[nxt[j][i - 1]][i - 1]),
getCount[j][i] = getCount[j][i - 1] + getCount[nxt[j][i - 1]][i - 1];
scanf ("%d", &M);
while (M --)
{
int L, R;
scanf ("%d %d", &L, &R);
if (nextC[L] == 0 || nextC[L] > R)
{
printf ("%d\n", R - L + 1);
continue;
}
int mini = 0, ans = 0, cnt = nextC[L] - L, pos = nextC[L];
for (int i=lg[R - pos + 1]; i>=0; i--)
if (nxt[pos][i] != 0 && nxt[pos][i] <= R)
mini = min (mini, minSuf[pos][i]),
cnt += getCount[pos][i],
pos = nxt[pos][i];
if (nxtS[pos - 1] == 0 || nxtS[pos - 1] >= R)
ans = cnt - min (getMinSuf (pos, R), mini + getS (pos, R));
else
ans = R - nxtS[pos - 1] + cnt - min (mini, minSuf[pos][0]);
printf ("%d\n", ans);
}
return 0;
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d\n", &N);
~~~~~~^~~~~~~~~~~~
election.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%s", sir + 1);
~~~~~~^~~~~~~~~~~~~~~
election.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d", &M);
~~~~~~^~~~~~~~~~
election.cpp:67:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d", &L, &R);
~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1016 KB |
Output is correct |
2 |
Correct |
5 ms |
1256 KB |
Output is correct |
3 |
Correct |
4 ms |
1348 KB |
Output is correct |
4 |
Correct |
4 ms |
1348 KB |
Output is correct |
5 |
Correct |
5 ms |
1348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1016 KB |
Output is correct |
2 |
Correct |
5 ms |
1256 KB |
Output is correct |
3 |
Correct |
4 ms |
1348 KB |
Output is correct |
4 |
Correct |
4 ms |
1348 KB |
Output is correct |
5 |
Correct |
5 ms |
1348 KB |
Output is correct |
6 |
Correct |
131 ms |
22388 KB |
Output is correct |
7 |
Correct |
134 ms |
22388 KB |
Output is correct |
8 |
Correct |
145 ms |
22388 KB |
Output is correct |
9 |
Correct |
151 ms |
22388 KB |
Output is correct |
10 |
Correct |
138 ms |
22388 KB |
Output is correct |
11 |
Correct |
193 ms |
22500 KB |
Output is correct |
12 |
Correct |
143 ms |
22500 KB |
Output is correct |
13 |
Correct |
134 ms |
22500 KB |
Output is correct |
14 |
Correct |
126 ms |
22500 KB |
Output is correct |
15 |
Correct |
173 ms |
22500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1016 KB |
Output is correct |
2 |
Correct |
5 ms |
1256 KB |
Output is correct |
3 |
Correct |
4 ms |
1348 KB |
Output is correct |
4 |
Correct |
4 ms |
1348 KB |
Output is correct |
5 |
Correct |
5 ms |
1348 KB |
Output is correct |
6 |
Correct |
131 ms |
22388 KB |
Output is correct |
7 |
Correct |
134 ms |
22388 KB |
Output is correct |
8 |
Correct |
145 ms |
22388 KB |
Output is correct |
9 |
Correct |
151 ms |
22388 KB |
Output is correct |
10 |
Correct |
138 ms |
22388 KB |
Output is correct |
11 |
Correct |
193 ms |
22500 KB |
Output is correct |
12 |
Correct |
143 ms |
22500 KB |
Output is correct |
13 |
Correct |
134 ms |
22500 KB |
Output is correct |
14 |
Correct |
126 ms |
22500 KB |
Output is correct |
15 |
Correct |
173 ms |
22500 KB |
Output is correct |
16 |
Correct |
1451 ms |
159600 KB |
Output is correct |
17 |
Correct |
1173 ms |
159660 KB |
Output is correct |
18 |
Correct |
1281 ms |
159688 KB |
Output is correct |
19 |
Correct |
1185 ms |
159688 KB |
Output is correct |
20 |
Correct |
1181 ms |
159688 KB |
Output is correct |
21 |
Correct |
1779 ms |
160940 KB |
Output is correct |
22 |
Correct |
1273 ms |
160940 KB |
Output is correct |
23 |
Correct |
1024 ms |
161140 KB |
Output is correct |
24 |
Correct |
1328 ms |
161140 KB |
Output is correct |
25 |
Correct |
1224 ms |
161140 KB |
Output is correct |