#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
#define eb emplace_back
#define pb push_back
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const int MN = 3e5 + 5;
int N, Q, a[MN];
set<int> dat;
int b[MN];
void upd(int i, int v) { for (; i < MN; i += i & -i) b[i] += v; }
int qry(int i) { int r = 0; for (; i; i -= i & -i) r += b[i]; return r; }
struct Query {
int x, y, v, id;
};
vector<Query> qs;
int ans[MN];
// maintain relative ordering in terms of queries
// split left right by x value
void cdq(int l, int r, vector<Query> &v) {
if (v.empty() || l > r) return;
int m = (l + r) / 2;
vector<Query> vl, vr;
for (auto q : v) {
if (q.x <= m) {
if (q.id == -1) upd(q.y, q.v);
if (q.id != -1 && l == r) ans[q.id] += qry(q.y);
vl.push_back(q);
} else {
if (q.id != -1) ans[q.id] += qry(q.y);
vr.push_back(q);
}
}
for (auto q : vl) if (q.id == -1) upd(q.y, -q.v);
if (l < r) cdq(l, m, vl), cdq(m + 1, r, vr);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> Q;
dat.insert(0), dat.insert(N + 1);
for (int i = 1; i <= N; i++) {
char c; cin >> c;
a[i] = (c == '1');
if (!a[i]) dat.insert(i);
}
int pos = 0;
for (int i = 1; i <= Q; i++) {
string op; cin >> op;
if (op == "query") {
int l, r; cin >> l >> r; r--;
pos++;
if (*dat.lower_bound(l) > r) ans[pos] += i;
qs.push_back({l, r, -1, pos});
} else {
int x, u ,v, sgn; cin >> x;
if (a[x]) {
auto nxt = dat.lower_bound(x), pre = nxt;
pre--;
u = *pre, v = *nxt;
dat.insert(x);
a[x] = 0;
sgn = 1;
} else {
auto it = dat.find(x), pre = it, nxt = it;
pre--, nxt++;
u = *pre, v = *nxt;
dat.erase(x);
a[x] = 1;
sgn = -1;
}
// [u+1,x] * [x,v-1]
qs.push_back({u + 1, x, sgn * i, -1});
qs.push_back({u + 1, v, -sgn * i, -1});
qs.push_back({x + 1, x, -sgn * i, -1});
qs.push_back({x + 1, v, sgn * i, -1});
}
}
cdq(1, N, qs);
for (int i = 1; i <= pos; i++) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
420 ms |
47520 KB |
Output is correct |
2 |
Correct |
478 ms |
44368 KB |
Output is correct |
3 |
Correct |
590 ms |
45884 KB |
Output is correct |
4 |
Correct |
990 ms |
56188 KB |
Output is correct |
5 |
Correct |
813 ms |
48620 KB |
Output is correct |
6 |
Correct |
1020 ms |
59196 KB |
Output is correct |
7 |
Correct |
627 ms |
39596 KB |
Output is correct |
8 |
Correct |
317 ms |
29396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1414 ms |
83096 KB |
Output is correct |
6 |
Correct |
1081 ms |
57084 KB |
Output is correct |
7 |
Correct |
841 ms |
49864 KB |
Output is correct |
8 |
Correct |
319 ms |
29136 KB |
Output is correct |
9 |
Correct |
112 ms |
19492 KB |
Output is correct |
10 |
Correct |
144 ms |
22240 KB |
Output is correct |
11 |
Correct |
126 ms |
21592 KB |
Output is correct |
12 |
Correct |
597 ms |
43080 KB |
Output is correct |
13 |
Correct |
311 ms |
30672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
513 ms |
37212 KB |
Output is correct |
6 |
Correct |
771 ms |
48948 KB |
Output is correct |
7 |
Correct |
983 ms |
59932 KB |
Output is correct |
8 |
Correct |
1267 ms |
85264 KB |
Output is correct |
9 |
Correct |
623 ms |
51552 KB |
Output is correct |
10 |
Correct |
627 ms |
71768 KB |
Output is correct |
11 |
Correct |
610 ms |
51948 KB |
Output is correct |
12 |
Correct |
622 ms |
74392 KB |
Output is correct |
13 |
Correct |
618 ms |
52540 KB |
Output is correct |
14 |
Correct |
647 ms |
74764 KB |
Output is correct |
15 |
Correct |
575 ms |
43336 KB |
Output is correct |
16 |
Correct |
307 ms |
28880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
420 ms |
47520 KB |
Output is correct |
9 |
Correct |
478 ms |
44368 KB |
Output is correct |
10 |
Correct |
590 ms |
45884 KB |
Output is correct |
11 |
Correct |
990 ms |
56188 KB |
Output is correct |
12 |
Correct |
813 ms |
48620 KB |
Output is correct |
13 |
Correct |
1020 ms |
59196 KB |
Output is correct |
14 |
Correct |
627 ms |
39596 KB |
Output is correct |
15 |
Correct |
317 ms |
29396 KB |
Output is correct |
16 |
Correct |
4 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
1414 ms |
83096 KB |
Output is correct |
21 |
Correct |
1081 ms |
57084 KB |
Output is correct |
22 |
Correct |
841 ms |
49864 KB |
Output is correct |
23 |
Correct |
319 ms |
29136 KB |
Output is correct |
24 |
Correct |
112 ms |
19492 KB |
Output is correct |
25 |
Correct |
144 ms |
22240 KB |
Output is correct |
26 |
Correct |
126 ms |
21592 KB |
Output is correct |
27 |
Correct |
597 ms |
43080 KB |
Output is correct |
28 |
Correct |
311 ms |
30672 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
3 ms |
620 KB |
Output is correct |
32 |
Correct |
4 ms |
620 KB |
Output is correct |
33 |
Correct |
513 ms |
37212 KB |
Output is correct |
34 |
Correct |
771 ms |
48948 KB |
Output is correct |
35 |
Correct |
983 ms |
59932 KB |
Output is correct |
36 |
Correct |
1267 ms |
85264 KB |
Output is correct |
37 |
Correct |
623 ms |
51552 KB |
Output is correct |
38 |
Correct |
627 ms |
71768 KB |
Output is correct |
39 |
Correct |
610 ms |
51948 KB |
Output is correct |
40 |
Correct |
622 ms |
74392 KB |
Output is correct |
41 |
Correct |
618 ms |
52540 KB |
Output is correct |
42 |
Correct |
647 ms |
74764 KB |
Output is correct |
43 |
Correct |
575 ms |
43336 KB |
Output is correct |
44 |
Correct |
307 ms |
28880 KB |
Output is correct |
45 |
Correct |
214 ms |
32592 KB |
Output is correct |
46 |
Correct |
281 ms |
33296 KB |
Output is correct |
47 |
Correct |
537 ms |
35144 KB |
Output is correct |
48 |
Correct |
934 ms |
55860 KB |
Output is correct |
49 |
Correct |
140 ms |
25044 KB |
Output is correct |
50 |
Correct |
137 ms |
25408 KB |
Output is correct |
51 |
Correct |
143 ms |
25940 KB |
Output is correct |
52 |
Correct |
154 ms |
27788 KB |
Output is correct |
53 |
Correct |
149 ms |
26976 KB |
Output is correct |
54 |
Correct |
150 ms |
26976 KB |
Output is correct |