//from duckindog wth depression
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, q;
bool a[N], b[N];
struct Query {
int ty, x, y;
Query() : ty(0), x(0), y(0) {}
Query(int ty, int x, int y) : ty(ty), x(x), y(y) {}
} Q[N];
int f[4 * N];
void build(int s, int l, int r) {
if (l == r) {
f[s] = a[l];
return;
}
int mid = l + r >> 1;
build(s * 2, l, mid); build(s * 2 + 1, mid + 1, r);
f[s] = min(f[s * 2], f[s * 2 + 1]);
}
void update(int s, int l, int r, int x, int y) {
if (y < l || y > r) return;
if (l == r) {
f[s] = x;
return;
}
int mid = l + r >> 1;
update(s * 2, l, mid, x, y); update(s * 2 + 1, mid + 1, r, x, y);
f[s] = min(f[s * 2], f[s * 2 + 1]);
}
int query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return 1;
if (u <= l && r <= v) return f[s];
int mid = l + r >> 1;
return min(query(s * 2, l, mid, u, v), query(s * 2 + 1, mid + 1, r, u, v));
}
int get(int x, bool w) {
int ret = x;
int l, r;
if (w) l = x, r = n;
else l = 1, r = x;
while (l <= r) {
int mid = l + r >> 1;
bool s = (w == 1 ? query(1, 1, n, x, mid) : query(1, 1, n, mid, x));
if (s) {
if (w) l = (ret = mid) + 1;
else r = (ret = mid) - 1;
} else {
if (w) r = mid - 1;
else l = mid + 1;
}
}
return ret;
}
vector<int> bit[N], vt[N];
void upd(int i, int j, int x) {
for (; i; i -= i & -i) {
auto p = lower_bound(vt[i].begin(), vt[i].end(), j) - vt[i].begin() + 1;
for (int t = p; t <= vt[i].size(); t += t & -t) bit[i][t] += x;
}
}
int que(int i, int j) {
int ret = 0;
for (; i <= n; i += i & -i) {
auto p = upper_bound(vt[i].begin(), vt[i].end(), j) - vt[i].begin();
for (int t = p; t; t -= t & -t) ret += bit[i][t];
}
return ret;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
char x; cin >> x;
a[i] = b[i] = (x == '1');
}
vector<pair<int, int>> updlist;
build(1, 1, n);
for (int i = 1; i <= q; ++i) {
string ty; cin >> ty;
if (ty == "toggle") {
int x; cin >> x;
a[x] = 1 - a[x];
update(1, 1, n, a[x], x);
int l = (a[x - 1] == 1 ? get(x - 1, 0) : x), r = (a[x + 1] == 1 ? get(x + 1, 1) : x);
updlist.push_back({x, x});
updlist.push_back({l - 1, x});
updlist.push_back({x, r + 1});
updlist.push_back({l - 1, r + 1});
Q[i] = {0, x, 0};
} else {
int x, y; cin >> x >> y;
Q[i] = {1, x, y - 1};
}
}
sort(updlist.begin(), updlist.end());
updlist.resize(unique(updlist.begin(), updlist.end()) - updlist.begin());
for (auto duck : updlist) {
int x, y; tie(x, y) = duck;
for (int i = x; i; i -= i & -i) vt[i].push_back(y);
}
for (int i = 1; i <= n; ++i) {
sort(vt[i].begin(), vt[i].end());
vt[i].resize(unique(vt[i].begin(), vt[i].end()) - vt[i].begin());
bit[i].resize(vt[i].size() + 5);
}
for (int i = 1; i <= n; ++i) a[i] = b[i];
build(1, 1, n);
for (int i = 1; i <= q; ++i) {
int ty = Q[i].ty, x = Q[i].x, y = Q[i].y;
if (!ty) {
a[x] = 1 - a[x];
update(1, 1, n, a[x], x);
int l = (a[x - 1] == 1 ? get(x - 1, 0) : x), r = (a[x + 1] == 1 ? get(x + 1, 1) : x);
if (a[x]) {
upd(x, x, -i);
upd(l - 1, x, i);
upd(x, r + 1, i);
upd(l - 1, r + 1, -i);
} else {
upd(x, x, i);
upd(l - 1, x, -i);
upd(x, r + 1, -i);
upd(l - 1, r + 1, i);
}
} else {
if (!query(1, 1, n, x, y)) cout << que(x, y) << "\n";
else cout << i + que(x, y) << '\n';
}
}
}
Compilation message
street_lamps.cpp: In function 'void build(int, int, int)':
street_lamps.cpp:21:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'void update(int, int, int, int, int)':
street_lamps.cpp:31:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'int query(int, int, int, int, int)':
street_lamps.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'int get(int, bool)':
street_lamps.cpp:48:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'void upd(int, int, int)':
street_lamps.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int t = p; t <= vt[i].size(); t += t & -t) bit[i][t] += x;
| ~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("duck.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:84:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen("duck.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19068 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
5 ms |
19032 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
30912 KB |
Output is correct |
2 |
Correct |
402 ms |
29472 KB |
Output is correct |
3 |
Correct |
828 ms |
31680 KB |
Output is correct |
4 |
Correct |
2059 ms |
75548 KB |
Output is correct |
5 |
Correct |
2299 ms |
69444 KB |
Output is correct |
6 |
Correct |
2459 ms |
77364 KB |
Output is correct |
7 |
Correct |
158 ms |
39164 KB |
Output is correct |
8 |
Correct |
163 ms |
40428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19288 KB |
Output is correct |
2 |
Correct |
10 ms |
19036 KB |
Output is correct |
3 |
Correct |
7 ms |
19036 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
4141 ms |
106412 KB |
Output is correct |
6 |
Correct |
3490 ms |
92344 KB |
Output is correct |
7 |
Correct |
2415 ms |
68920 KB |
Output is correct |
8 |
Correct |
258 ms |
40556 KB |
Output is correct |
9 |
Correct |
82 ms |
22616 KB |
Output is correct |
10 |
Correct |
94 ms |
22924 KB |
Output is correct |
11 |
Correct |
89 ms |
23144 KB |
Output is correct |
12 |
Correct |
215 ms |
39460 KB |
Output is correct |
13 |
Correct |
214 ms |
40528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19036 KB |
Output is correct |
2 |
Correct |
7 ms |
19288 KB |
Output is correct |
3 |
Correct |
9 ms |
19032 KB |
Output is correct |
4 |
Correct |
9 ms |
19036 KB |
Output is correct |
5 |
Correct |
334 ms |
40244 KB |
Output is correct |
6 |
Correct |
1553 ms |
62788 KB |
Output is correct |
7 |
Correct |
2475 ms |
77184 KB |
Output is correct |
8 |
Correct |
3870 ms |
95196 KB |
Output is correct |
9 |
Correct |
588 ms |
30144 KB |
Output is correct |
10 |
Correct |
453 ms |
38840 KB |
Output is correct |
11 |
Correct |
590 ms |
29740 KB |
Output is correct |
12 |
Correct |
452 ms |
38584 KB |
Output is correct |
13 |
Correct |
587 ms |
29552 KB |
Output is correct |
14 |
Correct |
453 ms |
38588 KB |
Output is correct |
15 |
Correct |
237 ms |
39476 KB |
Output is correct |
16 |
Correct |
247 ms |
40684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19036 KB |
Output is correct |
3 |
Correct |
5 ms |
19068 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
5 ms |
19032 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Correct |
5 ms |
19036 KB |
Output is correct |
8 |
Correct |
295 ms |
30912 KB |
Output is correct |
9 |
Correct |
402 ms |
29472 KB |
Output is correct |
10 |
Correct |
828 ms |
31680 KB |
Output is correct |
11 |
Correct |
2059 ms |
75548 KB |
Output is correct |
12 |
Correct |
2299 ms |
69444 KB |
Output is correct |
13 |
Correct |
2459 ms |
77364 KB |
Output is correct |
14 |
Correct |
158 ms |
39164 KB |
Output is correct |
15 |
Correct |
163 ms |
40428 KB |
Output is correct |
16 |
Correct |
11 ms |
19288 KB |
Output is correct |
17 |
Correct |
10 ms |
19036 KB |
Output is correct |
18 |
Correct |
7 ms |
19036 KB |
Output is correct |
19 |
Correct |
5 ms |
19036 KB |
Output is correct |
20 |
Correct |
4141 ms |
106412 KB |
Output is correct |
21 |
Correct |
3490 ms |
92344 KB |
Output is correct |
22 |
Correct |
2415 ms |
68920 KB |
Output is correct |
23 |
Correct |
258 ms |
40556 KB |
Output is correct |
24 |
Correct |
82 ms |
22616 KB |
Output is correct |
25 |
Correct |
94 ms |
22924 KB |
Output is correct |
26 |
Correct |
89 ms |
23144 KB |
Output is correct |
27 |
Correct |
215 ms |
39460 KB |
Output is correct |
28 |
Correct |
214 ms |
40528 KB |
Output is correct |
29 |
Correct |
6 ms |
19036 KB |
Output is correct |
30 |
Correct |
7 ms |
19288 KB |
Output is correct |
31 |
Correct |
9 ms |
19032 KB |
Output is correct |
32 |
Correct |
9 ms |
19036 KB |
Output is correct |
33 |
Correct |
334 ms |
40244 KB |
Output is correct |
34 |
Correct |
1553 ms |
62788 KB |
Output is correct |
35 |
Correct |
2475 ms |
77184 KB |
Output is correct |
36 |
Correct |
3870 ms |
95196 KB |
Output is correct |
37 |
Correct |
588 ms |
30144 KB |
Output is correct |
38 |
Correct |
453 ms |
38840 KB |
Output is correct |
39 |
Correct |
590 ms |
29740 KB |
Output is correct |
40 |
Correct |
452 ms |
38584 KB |
Output is correct |
41 |
Correct |
587 ms |
29552 KB |
Output is correct |
42 |
Correct |
453 ms |
38588 KB |
Output is correct |
43 |
Correct |
237 ms |
39476 KB |
Output is correct |
44 |
Correct |
247 ms |
40684 KB |
Output is correct |
45 |
Correct |
102 ms |
25548 KB |
Output is correct |
46 |
Correct |
200 ms |
24780 KB |
Output is correct |
47 |
Correct |
1113 ms |
46592 KB |
Output is correct |
48 |
Correct |
2141 ms |
74932 KB |
Output is correct |
49 |
Correct |
102 ms |
23188 KB |
Output is correct |
50 |
Correct |
106 ms |
23128 KB |
Output is correct |
51 |
Correct |
100 ms |
23300 KB |
Output is correct |
52 |
Correct |
110 ms |
23556 KB |
Output is correct |
53 |
Correct |
101 ms |
23560 KB |
Output is correct |
54 |
Correct |
106 ms |
23744 KB |
Output is correct |