#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
const int MN = 500071;
int A[MN], Re[MN];
int n, q;
vector<ii> Q[MN];
stack<int> PozDez;
namespace AINT {
int V[4 * MN], Lz[4 * MN], Pmin[4 * MN], Vmin[4 * MN];
void init(int u = 1, int s = 1, int d = n) {
V[u] = Lz[u] = Vmin[u] = 0;
Pmin[u] = s;
if(s != d) {
init(u << 1, s, (s + d) >> 1);
init(u << 1 | 1, ((s + d) >> 1) + 1, d);
}
}
void prop(int u, int s, int d) {
V[u] += Lz[u];
Vmin[u] += Lz[u];
if(s != d) {
Lz[u << 1] += Lz[u];
Lz[u << 1 | 1] += Lz[u];
}
Lz[u] = 0;
}
void add(int l, int r, int v, int u = 1, int s = 1, int d = n) {
prop(u, s, d);
if(d < l || r < s) {
return;
}
if(l <= s && d <= r) {
Lz[u] = v;
prop(u, s, d);
return;
}
add(l, r, v, u << 1, s, (s + d) >> 1);
add(l, r, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
if(Vmin[u << 1] <= Vmin[u << 1 | 1]) {
Vmin[u] = Vmin[u << 1];
Pmin[u] = Pmin[u << 1];
} else {
Vmin[u] = Vmin[u << 1 | 1];
Pmin[u] = Pmin[u << 1 | 1];
}
}
int poz_neg() {
if(Vmin[1] >= 0) return -1;
assert(Vmin[1] == -1);
return Pmin[1];
}
}
namespace AINT2 {
int V[4 * MN], SufMin[4 * MN];
void update(int p, int v, int u = 1, int s = 1, int d = n) {
if(d < p || p < s) return;
if(s == d) {
V[u] = v;
SufMin[u] = min(0, v);
return;
}
update(p, v, u << 1, s, (s + d) >> 1);
update(p, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u] = V[u << 1] + V[u << 1 | 1];
SufMin[u] = min(SufMin[u << 1 | 1], SufMin[u << 1] + V[u << 1 | 1]);
}
pair<int, int> query(int l, int r, int u = 1, int s = 1, int d = n) { ///(sum, sufmin)
if(d < l || r < s) return {0, 0};
if(l <= s && d <= r) return {V[u], SufMin[u]};
ii r1 = query(l, r, u << 1, s, (s + d) >> 1), r2 = query(l, r, u << 1 | 1, ((s + d) >> 1) + 1, d);
return {r1.first + r2.first, min(r2.first + r1.second, r2.second)};
}
}
namespace AIB {
int V[MN];
void update(int p, int v) {
while(p < MN) {
V[p] += v;
p += p & -p;
}
}
int f(int p) {
int r = 0;
while(p) {
r += V[p];
p -= p & -p;
}
return r;
}
int query(int l, int r) {
if(!l) return f(r);
return f(r) - f(l - 1);
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; ++i) {
char c; cin >> c;
A[i] = (c == 'C') ? 1 : -1;
}
cin >> q;
for(int i = 1; i <= q; ++i) {
int l, r;
cin >> l >> r;
Q[l].push_back({r, i});
}
int nr_dez = 0;
AINT::init();
for(int i = n; i >= 1; --i) {
///adaugam i
AINT::add(i, n, A[i]);
AINT2::update(i, A[i]);
if(A[i] == 1) {
if(!PozDez.empty()) {
int p1 = PozDez.top();
// printf("L-am activat pe %d\n", p1);
AIB::update(p1, -1);
AINT::add(p1, n, -1);
AINT2::update(p1, -1);
PozDez.pop();
}
}
else {
int p = AINT::poz_neg();
if(p != -1) {
AINT::add(p, n, 1);
// printf("L-am deazactivat pe %d\n", p);
AIB::update(p, 1);
AINT2::update(p, 0);
PozDez.push(p);
}
}
for(auto it : Q[i]) {
int r = it.first, pre = it.second;
Re[pre] = AIB::query(i, r) + max(0, -AINT2::query(i, r).second);
}
}
for(int i = 1; i <= q; ++i)
cout << Re[i] << "\n";
return 0;
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:119:9: warning: unused variable 'nr_dez' [-Wunused-variable]
119 | int nr_dez = 0;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
12 ms |
12244 KB |
Output is correct |
3 |
Correct |
11 ms |
12244 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
10 ms |
12300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
12 ms |
12244 KB |
Output is correct |
3 |
Correct |
11 ms |
12244 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
10 ms |
12300 KB |
Output is correct |
6 |
Correct |
151 ms |
21664 KB |
Output is correct |
7 |
Correct |
133 ms |
21148 KB |
Output is correct |
8 |
Correct |
133 ms |
21308 KB |
Output is correct |
9 |
Correct |
140 ms |
21588 KB |
Output is correct |
10 |
Correct |
138 ms |
21632 KB |
Output is correct |
11 |
Correct |
137 ms |
21824 KB |
Output is correct |
12 |
Correct |
138 ms |
21816 KB |
Output is correct |
13 |
Correct |
138 ms |
21892 KB |
Output is correct |
14 |
Correct |
134 ms |
21736 KB |
Output is correct |
15 |
Correct |
130 ms |
21704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
12 ms |
12244 KB |
Output is correct |
3 |
Correct |
11 ms |
12244 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
10 ms |
12300 KB |
Output is correct |
6 |
Correct |
151 ms |
21664 KB |
Output is correct |
7 |
Correct |
133 ms |
21148 KB |
Output is correct |
8 |
Correct |
133 ms |
21308 KB |
Output is correct |
9 |
Correct |
140 ms |
21588 KB |
Output is correct |
10 |
Correct |
138 ms |
21632 KB |
Output is correct |
11 |
Correct |
137 ms |
21824 KB |
Output is correct |
12 |
Correct |
138 ms |
21816 KB |
Output is correct |
13 |
Correct |
138 ms |
21892 KB |
Output is correct |
14 |
Correct |
134 ms |
21736 KB |
Output is correct |
15 |
Correct |
130 ms |
21704 KB |
Output is correct |
16 |
Correct |
1181 ms |
62896 KB |
Output is correct |
17 |
Correct |
1012 ms |
58788 KB |
Output is correct |
18 |
Correct |
1087 ms |
59924 KB |
Output is correct |
19 |
Correct |
1058 ms |
61420 KB |
Output is correct |
20 |
Correct |
1112 ms |
61824 KB |
Output is correct |
21 |
Correct |
1156 ms |
64060 KB |
Output is correct |
22 |
Correct |
1122 ms |
63028 KB |
Output is correct |
23 |
Correct |
1164 ms |
63968 KB |
Output is correct |
24 |
Correct |
1115 ms |
63036 KB |
Output is correct |
25 |
Correct |
1043 ms |
61928 KB |
Output is correct |