#include<bits/stdc++.h>
using namespace std;
const int N = 500005, L = 524288, G = 19, inf = 1e9;
int n, q, a[N];
char ipt[N];
struct data {int i, m, x;} s[N][G];
struct segtree {
int v[2*L];
void build () {
for(int i=1;i<=n;i++) {
v[L+i] = a[i];
}
for(int i=L;--i;) {
v[i] = max(v[2*i], v[2*i+1]);
}
}
int get (int S, int E) {
int R = -inf;
S += L;
E += L;
while(S <= E) {
if(S%2 == 1) R = max(R, v[S++]);
if(E%2 == 0) R = max(R, v[E--]);
S /= 2;
E /= 2;
}
return R;
}
} seg;
int main()
{
scanf("%d%s",&n,ipt+1);
for(int i=1;i<=n;i++) {
a[i] = a[i-1] + (1 - 2 * (ipt[i] == 'T'));
}
seg.build();
s[n+1][0] = {n+1, 0, 0};
for(int i=n+1;i>=1;i--) {
if(ipt[i] == 'T') {
s[i][0] = {i+1, 0, 1};
}
if(ipt[i] == 'C') {
int I = i+1, M = 0;
for(int j=G;j--;) {
if(s[I][j].x) continue;
M = max(M, s[I][j].m);
I = s[I][j].i;
}
I = min(I, n);
s[i][0] = {I+1, M+1, 0};
}
for(int j=1;j<G;j++) {
int I = s[i][j-1].i;
s[i][j] = {s[I][j-1].i, max(s[i][j-1].m, s[I][j-1].m), s[i][j-1].x + s[I][j-1].x};
}
}
scanf("%d",&q);
while(q--) {
int A, B, M = 0, R = 0;
scanf("%d%d",&A,&B);
for(int i=G;i--;) {
if(s[A][i].i > B) continue;
M = max(M, s[A][i].m);
R += s[A][i].x;
A = s[A][i].i;
}
M = max(M, seg.get(A, B) - a[A-1]);
printf("%d\n",R+M-(a[B]-a[A-1]));
}
}
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%s",&n,ipt+1);
~~~~~^~~~~~~~~~~~~~~~~
election.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
election.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&A,&B);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2808 KB |
Output is correct |
2 |
Correct |
9 ms |
3048 KB |
Output is correct |
3 |
Correct |
7 ms |
3048 KB |
Output is correct |
4 |
Correct |
6 ms |
3048 KB |
Output is correct |
5 |
Correct |
8 ms |
3048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2808 KB |
Output is correct |
2 |
Correct |
9 ms |
3048 KB |
Output is correct |
3 |
Correct |
7 ms |
3048 KB |
Output is correct |
4 |
Correct |
6 ms |
3048 KB |
Output is correct |
5 |
Correct |
8 ms |
3048 KB |
Output is correct |
6 |
Correct |
113 ms |
19196 KB |
Output is correct |
7 |
Correct |
79 ms |
19196 KB |
Output is correct |
8 |
Correct |
94 ms |
19244 KB |
Output is correct |
9 |
Correct |
99 ms |
19244 KB |
Output is correct |
10 |
Correct |
100 ms |
19244 KB |
Output is correct |
11 |
Correct |
142 ms |
19452 KB |
Output is correct |
12 |
Correct |
160 ms |
19452 KB |
Output is correct |
13 |
Correct |
173 ms |
19452 KB |
Output is correct |
14 |
Correct |
143 ms |
19452 KB |
Output is correct |
15 |
Correct |
162 ms |
19452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2808 KB |
Output is correct |
2 |
Correct |
9 ms |
3048 KB |
Output is correct |
3 |
Correct |
7 ms |
3048 KB |
Output is correct |
4 |
Correct |
6 ms |
3048 KB |
Output is correct |
5 |
Correct |
8 ms |
3048 KB |
Output is correct |
6 |
Correct |
113 ms |
19196 KB |
Output is correct |
7 |
Correct |
79 ms |
19196 KB |
Output is correct |
8 |
Correct |
94 ms |
19244 KB |
Output is correct |
9 |
Correct |
99 ms |
19244 KB |
Output is correct |
10 |
Correct |
100 ms |
19244 KB |
Output is correct |
11 |
Correct |
142 ms |
19452 KB |
Output is correct |
12 |
Correct |
160 ms |
19452 KB |
Output is correct |
13 |
Correct |
173 ms |
19452 KB |
Output is correct |
14 |
Correct |
143 ms |
19452 KB |
Output is correct |
15 |
Correct |
162 ms |
19452 KB |
Output is correct |
16 |
Correct |
1048 ms |
120760 KB |
Output is correct |
17 |
Correct |
700 ms |
120760 KB |
Output is correct |
18 |
Correct |
952 ms |
120760 KB |
Output is correct |
19 |
Correct |
983 ms |
120760 KB |
Output is correct |
20 |
Correct |
1112 ms |
120760 KB |
Output is correct |
21 |
Correct |
1662 ms |
121460 KB |
Output is correct |
22 |
Correct |
1146 ms |
121460 KB |
Output is correct |
23 |
Correct |
1565 ms |
121640 KB |
Output is correct |
24 |
Correct |
1038 ms |
121640 KB |
Output is correct |
25 |
Correct |
1014 ms |
121640 KB |
Output is correct |