// File C.cpp created on 18.08.2024 at 15:17:15
#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "../debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int maxN = 3E5 + 5;
int lsb(int x) {
return x & -x;
}
struct fenwick {
int n;
std::vector<i64> tree;
std::vector<std::pair<int, int>> his;
fenwick(int _n) : n(_n), tree(n + 1) {}
void modify(int p, int x, bool flag = true) {
if(flag) his.emplace_back(p, x);
for(p += 1; p <= n; p += lsb(p)) {
tree[p] += x;
}
}
i64 query(int p) {
i64 res = 0;
for(p += 1; p; p -= lsb(p)) {
res += tree[p];
}
return res;
}
i64 query(int l, int r) {
return query(r) - query(l - 1);
}
void roll() {
while(!his.empty()) {
modify(his.back().first, -his.back().second, false);
his.pop_back();
}
}
} fen(maxN);
bool isq[maxN];
int N, Q, ans[maxN];
std::string S;
std::vector<std::array<int, 4>> segs;
void dnq(int l, int r) {
if(l >= r) {
return;
}
int mid = (l + r) >> 1;
dnq(l, mid);
dnq(mid + 1, r);
debug(l, r);
int ptr[2]{l, mid + 1};
std::vector<std::array<int, 4>> tmp;
while(ptr[0] <= mid && ptr[1] <= r) {
if(segs[ptr[0]][2] <= segs[ptr[1]][2]) {
tmp.push_back(segs[ptr[0]++]);
if(tmp.back()[0] == 0) {
debug(tmp.back()[3], tmp.back()[1]);
fen.modify(tmp.back()[3], tmp.back()[1]);
}
} else {
tmp.push_back(segs[ptr[1]++]);
if(tmp.back()[0] == 1) {
debug(tmp.back()[1], fen.query(tmp.back()[3], maxN - 1));
ans[tmp.back()[1]] += fen.query(tmp.back()[3], maxN - 1);
}
}
}
while(ptr[0] <= mid) {
tmp.push_back(segs[ptr[0]++]);
if(tmp.back()[0] == 0) {
debug(tmp.back()[3], tmp.back()[1]);
fen.modify(tmp.back()[3], tmp.back()[1]);
}
}
while(ptr[1] <= r) {
tmp.push_back(segs[ptr[1]++]);
if(tmp.back()[0] == 1) {
debug(tmp.back()[1], fen.query(tmp.back()[3], maxN - 1));
ans[tmp.back()[1]] += fen.query(tmp.back()[3], maxN - 1);
}
}
debug(tmp);
for(int i = l; i <= r; ++i) {
segs[i] = tmp[i - l];
}
debug("wtf");
fen.roll();
debug("ok");
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> N >> Q;
std::cin >> S;
S = "0" + S + "0";
N += 2;
std::set<int> cl;
for(int i = 0, j = 0; i < N; i = j) {
if(S[i] == '0') {
++j;
cl.emplace(i);
} else {
while(j < N && S[j] == '1') ++j;
segs.push_back({0, -1, i, j - 1});
}
}
for(int i = 2; i <= Q + 1; ++i) {
std::string T;
std::cin >> T;
if(T[0] == 'q') {
isq[i] = true;
int L, R;
std::cin >> L >> R;
--R;
segs.push_back({1, i, L, R});
} else {
int X;
std::cin >> X;
if(S[X] == '1') {
int nxt = *cl.lower_bound(X);
int bef = *--cl.lower_bound(X);
segs.push_back({0, +i, bef + 1, nxt - 1});
segs.push_back({0, -i, bef + 1, X - 1});
segs.push_back({0, -i, X + 1, nxt - 1});
S[X] = '0';
cl.emplace(X);
} else {
S[X] = '1';
cl.erase(X);
int nxt = *cl.lower_bound(X);
int bef = *--cl.lower_bound(X);
segs.push_back({0, +i, bef + 1, X - 1});
segs.push_back({0, +i, X + 1, nxt - 1});
segs.push_back({0, -i, bef + 1, nxt - 1});
}
}
}
debug(segs);
dnq(0, int(segs.size()) - 1);
for(int i = 0; i <= Q + 1; ++i) {
if(isq[i]) {
if(ans[i] < 0) ans[i] += i;
std::cout << ans[i] << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2588 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
501 ms |
43676 KB |
Output is correct |
2 |
Correct |
524 ms |
43940 KB |
Output is correct |
3 |
Correct |
485 ms |
44700 KB |
Output is correct |
4 |
Correct |
636 ms |
56168 KB |
Output is correct |
5 |
Correct |
483 ms |
51056 KB |
Output is correct |
6 |
Correct |
604 ms |
56936 KB |
Output is correct |
7 |
Correct |
254 ms |
41464 KB |
Output is correct |
8 |
Correct |
213 ms |
27416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2908 KB |
Output is correct |
2 |
Correct |
3 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
3 ms |
2908 KB |
Output is correct |
5 |
Correct |
737 ms |
67292 KB |
Output is correct |
6 |
Correct |
621 ms |
57872 KB |
Output is correct |
7 |
Correct |
489 ms |
50832 KB |
Output is correct |
8 |
Correct |
235 ms |
27420 KB |
Output is correct |
9 |
Correct |
109 ms |
16700 KB |
Output is correct |
10 |
Correct |
129 ms |
23644 KB |
Output is correct |
11 |
Correct |
135 ms |
23592 KB |
Output is correct |
12 |
Correct |
308 ms |
41464 KB |
Output is correct |
13 |
Correct |
228 ms |
27312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2940 KB |
Output is correct |
2 |
Correct |
3 ms |
2908 KB |
Output is correct |
3 |
Correct |
4 ms |
2908 KB |
Output is correct |
4 |
Correct |
3 ms |
2908 KB |
Output is correct |
5 |
Correct |
286 ms |
38676 KB |
Output is correct |
6 |
Correct |
488 ms |
52480 KB |
Output is correct |
7 |
Correct |
648 ms |
56984 KB |
Output is correct |
8 |
Correct |
861 ms |
63076 KB |
Output is correct |
9 |
Correct |
698 ms |
48056 KB |
Output is correct |
10 |
Correct |
810 ms |
52076 KB |
Output is correct |
11 |
Correct |
624 ms |
48104 KB |
Output is correct |
12 |
Correct |
813 ms |
51940 KB |
Output is correct |
13 |
Correct |
701 ms |
48048 KB |
Output is correct |
14 |
Correct |
810 ms |
51912 KB |
Output is correct |
15 |
Correct |
311 ms |
41616 KB |
Output is correct |
16 |
Correct |
227 ms |
27420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2588 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
501 ms |
43676 KB |
Output is correct |
9 |
Correct |
524 ms |
43940 KB |
Output is correct |
10 |
Correct |
485 ms |
44700 KB |
Output is correct |
11 |
Correct |
636 ms |
56168 KB |
Output is correct |
12 |
Correct |
483 ms |
51056 KB |
Output is correct |
13 |
Correct |
604 ms |
56936 KB |
Output is correct |
14 |
Correct |
254 ms |
41464 KB |
Output is correct |
15 |
Correct |
213 ms |
27416 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
3 ms |
2908 KB |
Output is correct |
20 |
Correct |
737 ms |
67292 KB |
Output is correct |
21 |
Correct |
621 ms |
57872 KB |
Output is correct |
22 |
Correct |
489 ms |
50832 KB |
Output is correct |
23 |
Correct |
235 ms |
27420 KB |
Output is correct |
24 |
Correct |
109 ms |
16700 KB |
Output is correct |
25 |
Correct |
129 ms |
23644 KB |
Output is correct |
26 |
Correct |
135 ms |
23592 KB |
Output is correct |
27 |
Correct |
308 ms |
41464 KB |
Output is correct |
28 |
Correct |
228 ms |
27312 KB |
Output is correct |
29 |
Correct |
2 ms |
2940 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
4 ms |
2908 KB |
Output is correct |
32 |
Correct |
3 ms |
2908 KB |
Output is correct |
33 |
Correct |
286 ms |
38676 KB |
Output is correct |
34 |
Correct |
488 ms |
52480 KB |
Output is correct |
35 |
Correct |
648 ms |
56984 KB |
Output is correct |
36 |
Correct |
861 ms |
63076 KB |
Output is correct |
37 |
Correct |
698 ms |
48056 KB |
Output is correct |
38 |
Correct |
810 ms |
52076 KB |
Output is correct |
39 |
Correct |
624 ms |
48104 KB |
Output is correct |
40 |
Correct |
813 ms |
51940 KB |
Output is correct |
41 |
Correct |
701 ms |
48048 KB |
Output is correct |
42 |
Correct |
810 ms |
51912 KB |
Output is correct |
43 |
Correct |
311 ms |
41616 KB |
Output is correct |
44 |
Correct |
227 ms |
27420 KB |
Output is correct |
45 |
Correct |
322 ms |
27316 KB |
Output is correct |
46 |
Correct |
350 ms |
27484 KB |
Output is correct |
47 |
Correct |
358 ms |
31844 KB |
Output is correct |
48 |
Correct |
607 ms |
56176 KB |
Output is correct |
49 |
Correct |
156 ms |
24568 KB |
Output is correct |
50 |
Correct |
143 ms |
24636 KB |
Output is correct |
51 |
Correct |
186 ms |
24916 KB |
Output is correct |
52 |
Correct |
169 ms |
25136 KB |
Output is correct |
53 |
Correct |
192 ms |
25140 KB |
Output is correct |
54 |
Correct |
145 ms |
25032 KB |
Output is correct |