#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
#ifdef __APPLE__
#include "../../debug.h"
#else
#define debug(...) 42
#endif
template<typename T>
void unique(vector<T> &a) {
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
}
struct Fenwick {
vector<int> t;
int n;
Fenwick() {}
Fenwick(int n) : n(n) {
t.resize(n + 1);
}
void add(int i, int x) {
for (++i; i <= n; i += i & (-i)) {
t[i] += x;
}
}
void add(int l, int r, int x) { // add on [l, r)
add(l, x);
add(r, -x);
}
int get(int i) {
int ans = 0;
for (; i > 0; i -= i & (-i)) {
ans += t[i];
}
return ans;
}
int get(int l, int r) {
return get(r) - get(l);
}
};
struct SegTree { // merge sort tree szhatih fenwickov
struct Node {
Fenwick st;
vector<int> coords;
};
vector<Node> tree;
int n;
SegTree(int n) : n(n) {
tree.resize(4 * n);
}
void insert(int v, int l, int r, int i, int x) {
tree[v].coords.push_back(x);
if (l + 1 == r) {
return;
}
int m = (l + r) / 2;
if (i < m) {
insert(2 * v + 1, l, m, i, x);
} else {
insert(2 * v + 2, m, r, i, x);
}
}
void insert(int i, int x) {
insert(0, 0, n, i, x);
}
void init(int v, int l, int r) {
unique(tree[v].coords);
tree[v].st = Fenwick(tree[v].coords.size());
if (l + 1 == r) {
return;
}
int m = (l + r) / 2;
init(2 * v + 1, l, m);
init(2 * v + 2, m, r);
}
void init() {
init(0, 0, n);
}
void update(int v, int l, int r, int li, int ri, int ly, int ry, int x) {
if (li >= r || l >= ri) {
return;
}
if (li <= l && r <= ri) {
int itl = lower_bound(tree[v].coords.begin(), tree[v].coords.end(), ly) - tree[v].coords.begin();
int itr = lower_bound(tree[v].coords.begin(), tree[v].coords.end(), ry) - tree[v].coords.begin();
debug(tree[v].coords, ly, ry, itl, itr);
tree[v].st.add(itl, itr, x);
return;
}
int m = (l + r) / 2;
update(2 * v + 1, l, m, li, ri, ly, ry, x);
update(2 * v + 2, m, r, li, ri, ly, ry, x);
}
void update(int lx, int rx, int ly, int ry, int x) {
update(0, 0, n, lx, rx, ly, ry, x);
}
int get(int v, int l, int r, int i, int j) {
int it = lower_bound(tree[v].coords.begin(), tree[v].coords.end(), j) - tree[v].coords.begin();
int ans = tree[v].st.get(it + 1);
if (l + 1 == r) {
return ans;
}
int m = (l + r) / 2;
if (i < m) {
return ans + get(2 * v + 1, l, m, i, j);
} else {
return ans + get(2 * v + 2, m, r, i, j);
}
}
int get(int i, int j) {
return get(0, 0, n, i, j);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
string s;
cin >> s;
set<int> zero;
vector<pair<int, int>> lr, qq;
for (int i = 0; i < q; ++i) {
string s;
cin >> s;
if (s == "toggle") {
int j;
cin >> j;
--j;
qq.push_back({0, j});
} else {
int a, b;
cin >> a >> b;
a -= 1;
b -= 2;
lr.push_back({a, b});
qq.push_back({1, (int) lr.size() - 1});
}
}
vector<int> ans(lr.size());
for (int i = -1; i <= n; ++i) {
zero.insert(i);
}
SegTree mst(n);
for (auto [l, r] : lr) {
mst.insert(l, r);
}
mst.init();
auto upd = [&](int pos, int t) {
auto itr = zero.upper_bound(pos);
auto itl = zero.lower_bound(pos);
int r = *itr;
int l = *prev(itl) + 1;
// += t on [l, pos] [pos, r)
mst.update(l, pos + 1, pos, r, t);
// for (int i = 0; i < (int) lr.size(); ++i) {
// auto [li, ri] = lr[i];
// if (l <= li && li <= pos && pos <= ri && ri < r) {
// debug(li + 1, ri + 1, t);
// ans[i] += t;
// }
// }
if (!zero.count(pos)) {
zero.insert(pos);
} else {
zero.erase(pos);
}
};
Fenwick bebra(n);
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
upd(i, 0);
bebra.add(i, 1);
}
}
for (int i = 1; i <= q; ++i) {
auto [t, j] = qq[i - 1];
if (t == 0) {
if (s[j] == '0') {
s[j] = '1';
upd(j, -i);
bebra.add(j, 1);
} else {
s[j] = '0';
upd(j, i);
bebra.add(j, -1);
}
} else {
auto [l, r] = lr[j];
int cur = mst.get(l, r);
// debug(ans[j], cur);
if (bebra.get(l, r + 1) != r - l + 1) {
cout << cur << "\n";
} else {
cout << i + cur << "\n";
}
}
}
}
Compilation message
street_lamps.cpp: In member function 'void SegTree::update(int, int, int, int, int, int, int, int)':
street_lamps.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
street_lamps.cpp:113:13: note: in expansion of macro 'debug'
113 | debug(tree[v].coords, ly, ry, itl, itr);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
216 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
15200 KB |
Output is correct |
2 |
Correct |
242 ms |
16636 KB |
Output is correct |
3 |
Correct |
431 ms |
22744 KB |
Output is correct |
4 |
Correct |
1834 ms |
140824 KB |
Output is correct |
5 |
Correct |
1854 ms |
145040 KB |
Output is correct |
6 |
Correct |
1459 ms |
133108 KB |
Output is correct |
7 |
Correct |
2275 ms |
165956 KB |
Output is correct |
8 |
Correct |
2675 ms |
167204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
3 ms |
668 KB |
Output is correct |
4 |
Correct |
3 ms |
720 KB |
Output is correct |
5 |
Correct |
702 ms |
108092 KB |
Output is correct |
6 |
Correct |
1394 ms |
128060 KB |
Output is correct |
7 |
Correct |
1953 ms |
145752 KB |
Output is correct |
8 |
Correct |
2758 ms |
168944 KB |
Output is correct |
9 |
Correct |
208 ms |
17912 KB |
Output is correct |
10 |
Correct |
211 ms |
20288 KB |
Output is correct |
11 |
Correct |
228 ms |
20664 KB |
Output is correct |
12 |
Correct |
2743 ms |
167516 KB |
Output is correct |
13 |
Correct |
2949 ms |
168856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2627 ms |
167076 KB |
Output is correct |
6 |
Correct |
1928 ms |
151072 KB |
Output is correct |
7 |
Correct |
1429 ms |
133948 KB |
Output is correct |
8 |
Correct |
865 ms |
108096 KB |
Output is correct |
9 |
Correct |
279 ms |
11808 KB |
Output is correct |
10 |
Correct |
174 ms |
7232 KB |
Output is correct |
11 |
Correct |
292 ms |
11756 KB |
Output is correct |
12 |
Correct |
187 ms |
7232 KB |
Output is correct |
13 |
Correct |
362 ms |
11792 KB |
Output is correct |
14 |
Correct |
220 ms |
7264 KB |
Output is correct |
15 |
Correct |
2922 ms |
167524 KB |
Output is correct |
16 |
Correct |
2857 ms |
168960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
216 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
198 ms |
15200 KB |
Output is correct |
9 |
Correct |
242 ms |
16636 KB |
Output is correct |
10 |
Correct |
431 ms |
22744 KB |
Output is correct |
11 |
Correct |
1834 ms |
140824 KB |
Output is correct |
12 |
Correct |
1854 ms |
145040 KB |
Output is correct |
13 |
Correct |
1459 ms |
133108 KB |
Output is correct |
14 |
Correct |
2275 ms |
165956 KB |
Output is correct |
15 |
Correct |
2675 ms |
167204 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
668 KB |
Output is correct |
19 |
Correct |
3 ms |
720 KB |
Output is correct |
20 |
Correct |
702 ms |
108092 KB |
Output is correct |
21 |
Correct |
1394 ms |
128060 KB |
Output is correct |
22 |
Correct |
1953 ms |
145752 KB |
Output is correct |
23 |
Correct |
2758 ms |
168944 KB |
Output is correct |
24 |
Correct |
208 ms |
17912 KB |
Output is correct |
25 |
Correct |
211 ms |
20288 KB |
Output is correct |
26 |
Correct |
228 ms |
20664 KB |
Output is correct |
27 |
Correct |
2743 ms |
167516 KB |
Output is correct |
28 |
Correct |
2949 ms |
168856 KB |
Output is correct |
29 |
Correct |
3 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
2627 ms |
167076 KB |
Output is correct |
34 |
Correct |
1928 ms |
151072 KB |
Output is correct |
35 |
Correct |
1429 ms |
133948 KB |
Output is correct |
36 |
Correct |
865 ms |
108096 KB |
Output is correct |
37 |
Correct |
279 ms |
11808 KB |
Output is correct |
38 |
Correct |
174 ms |
7232 KB |
Output is correct |
39 |
Correct |
292 ms |
11756 KB |
Output is correct |
40 |
Correct |
187 ms |
7232 KB |
Output is correct |
41 |
Correct |
362 ms |
11792 KB |
Output is correct |
42 |
Correct |
220 ms |
7264 KB |
Output is correct |
43 |
Correct |
2922 ms |
167524 KB |
Output is correct |
44 |
Correct |
2857 ms |
168960 KB |
Output is correct |
45 |
Correct |
90 ms |
7956 KB |
Output is correct |
46 |
Correct |
198 ms |
9504 KB |
Output is correct |
47 |
Correct |
855 ms |
56776 KB |
Output is correct |
48 |
Correct |
1976 ms |
139844 KB |
Output is correct |
49 |
Correct |
303 ms |
22492 KB |
Output is correct |
50 |
Correct |
296 ms |
21588 KB |
Output is correct |
51 |
Correct |
291 ms |
22512 KB |
Output is correct |
52 |
Correct |
374 ms |
26704 KB |
Output is correct |
53 |
Correct |
375 ms |
26648 KB |
Output is correct |
54 |
Correct |
329 ms |
26656 KB |
Output is correct |