// M
#include<bits/stdc++.h>
#define lc (id << 1)
#define rc (lc ^ 1)
#define md ((l + r) >> 1)
using namespace std;
const int N = 500005, MXS = N * 4;
int n, q, R[N];
char A[N];
int LZ[MXS], MN[MXS];
vector < pair < int , int > > V[N];
inline void Shift(int id)
{
LZ[lc] += LZ[id];
MN[lc] += LZ[id];
LZ[rc] += LZ[id];
MN[rc] += LZ[id];
LZ[id] = 0;
}
void Add(int le, int ri, int val, int id = 1, int l = 0, int r = n + 1)
{
if (ri <= l || r <= le)
return ;
if (le <= l && r <= ri)
{
LZ[id] += val;
MN[id] += val;
return ;
}
Shift(id);
Add(le, ri, val, lc, l, md);;
Add(le, ri, val, rc, md, r);
MN[id] = min(MN[lc], MN[rc]);
}
int GetMin(int le, int ri, int id = 1, int l = 0, int r = n + 1)
{
if (ri <= l || r <= le)
return MXS;
if (le <= l && r <= ri)
return MN[id];
Shift(id);
return min(GetMin(le, ri, lc, l, md), GetMin(le, ri, rc, md, r));
}
int main()
{
scanf("%d%s%d", &n, &A[1], &q);
for (int i = 1; i <= q; i ++)
{
int l, r;
scanf("%d%d", &l, &r);
V[r].push_back({i, l});
}
vector < int > stk;
for (int i = 1; i <= n; i ++)
{
if (A[i] == 'C')
{
if (stk.size())
Add(stk.back(), n + 1, -1), stk.pop_back();
Add(i, n + 1, 1);
}
else
stk.push_back(i);
for (auto X : V[i])
R[X.first] = (int)stk.size() - (int)(lower_bound(stk.begin(), stk.end(), X.second) - stk.begin()) + max(-(GetMin(X.second, i + 1) - GetMin(X.second - 1, X.second)), 0);
}
for (int i = 1; i <= q; i ++)
printf("%d\n", R[i]);
return 0;
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%s%d", &n, &A[1], &q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:50:22: 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 |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
98 ms |
17016 KB |
Output is correct |
7 |
Correct |
86 ms |
16632 KB |
Output is correct |
8 |
Correct |
90 ms |
16996 KB |
Output is correct |
9 |
Correct |
87 ms |
17016 KB |
Output is correct |
10 |
Correct |
96 ms |
16888 KB |
Output is correct |
11 |
Correct |
98 ms |
17144 KB |
Output is correct |
12 |
Correct |
95 ms |
17272 KB |
Output is correct |
13 |
Correct |
97 ms |
17272 KB |
Output is correct |
14 |
Correct |
97 ms |
17144 KB |
Output is correct |
15 |
Correct |
95 ms |
17016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
10 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
98 ms |
17016 KB |
Output is correct |
7 |
Correct |
86 ms |
16632 KB |
Output is correct |
8 |
Correct |
90 ms |
16996 KB |
Output is correct |
9 |
Correct |
87 ms |
17016 KB |
Output is correct |
10 |
Correct |
96 ms |
16888 KB |
Output is correct |
11 |
Correct |
98 ms |
17144 KB |
Output is correct |
12 |
Correct |
95 ms |
17272 KB |
Output is correct |
13 |
Correct |
97 ms |
17272 KB |
Output is correct |
14 |
Correct |
97 ms |
17144 KB |
Output is correct |
15 |
Correct |
95 ms |
17016 KB |
Output is correct |
16 |
Correct |
976 ms |
41864 KB |
Output is correct |
17 |
Correct |
739 ms |
39032 KB |
Output is correct |
18 |
Correct |
767 ms |
40184 KB |
Output is correct |
19 |
Correct |
752 ms |
41680 KB |
Output is correct |
20 |
Correct |
837 ms |
40820 KB |
Output is correct |
21 |
Correct |
885 ms |
43124 KB |
Output is correct |
22 |
Correct |
863 ms |
43000 KB |
Output is correct |
23 |
Correct |
869 ms |
43504 KB |
Output is correct |
24 |
Correct |
865 ms |
42868 KB |
Output is correct |
25 |
Correct |
903 ms |
42144 KB |
Output is correct |