//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];
bool f[4 * N];
pair<int, int> rvs[N];
void build(int s, int l, int r) {
rvs[s] = {l, 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] = 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] = 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 (query(s * 2, l, mid, u, v) & query(s * 2 + 1, mid + 1, r, u, v));
}
vector<int> section;
void put(int s, int l, int r, int u, int v) {
if (v < l || u > r) return;
if (u <= l && r <= v) {
section.push_back(s);
return;
}
int mid = l + r >> 1;
put(s * 2, l, mid, u, v); put(s * 2 + 1, mid + 1, r, u, v);
}
int dfsl(int s, int l, int r) {
if (l == r) return l;
int m = l + r >> 1;
if (!f[s * 2 + 1]) return dfsl(2 * s + 1, m + 1, r);
return dfsl(2 * s, l, m);
}
int dfsr(int s, int l, int r) {
if (l == r) return l;
int m = l + r >> 1;
if (!f[s * 2]) return dfsr(2 * s, l, m);
return dfsr(2 * s + 1, m + 1, r);
}
int get(int x, int w) {
section.clear();
int l = w == 1 ? x : 1, r = w == 1 ? n : x;
put(1, 1, n, l, r);
if (w) {
for (const auto&s : section) {
if (!f[s]) {
return dfsr(s, rvs[s].first, rvs[s].second);
}
}
return n + 1;
} else {
reverse(section.begin(), section.end());
for (const auto&s : section) {
if (!f[s]) {
return dfsl(s, rvs[s].first, rvs[s].second);
}
}
return 0;
}
}
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) + 1 : x), r = (a[x + 1] == 1 ? get(x + 1, 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) + 1 : x), r = (a[x + 1] == 1 ? get(x + 1, 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:23:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'void update(int, int, int, int, int)':
street_lamps.cpp:33:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'int query(int, int, int, int, int)':
street_lamps.cpp:40:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'void put(int, int, int, int, int)':
street_lamps.cpp:50:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'int dfsl(int, int, int)':
street_lamps.cpp:56:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int m = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'int dfsr(int, int, int)':
street_lamps.cpp:62:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int m = l + r >> 1;
| ~~^~~
street_lamps.cpp: In function 'void upd(int, int, int)':
street_lamps.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | 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:109:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen("duck.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:110:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen("duck.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21596 KB |
Output is correct |
2 |
Correct |
6 ms |
21604 KB |
Output is correct |
3 |
Correct |
6 ms |
21368 KB |
Output is correct |
4 |
Correct |
6 ms |
21592 KB |
Output is correct |
5 |
Correct |
6 ms |
21596 KB |
Output is correct |
6 |
Correct |
6 ms |
21596 KB |
Output is correct |
7 |
Correct |
7 ms |
21596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
32964 KB |
Output is correct |
2 |
Correct |
229 ms |
34236 KB |
Output is correct |
3 |
Correct |
384 ms |
35264 KB |
Output is correct |
4 |
Runtime error |
30 ms |
44888 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21596 KB |
Output is correct |
3 |
Correct |
7 ms |
21596 KB |
Output is correct |
4 |
Correct |
7 ms |
21596 KB |
Output is correct |
5 |
Runtime error |
31 ms |
44952 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21604 KB |
Output is correct |
2 |
Correct |
10 ms |
21712 KB |
Output is correct |
3 |
Correct |
7 ms |
21596 KB |
Output is correct |
4 |
Correct |
8 ms |
21596 KB |
Output is correct |
5 |
Runtime error |
29 ms |
44884 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21596 KB |
Output is correct |
2 |
Correct |
6 ms |
21604 KB |
Output is correct |
3 |
Correct |
6 ms |
21368 KB |
Output is correct |
4 |
Correct |
6 ms |
21592 KB |
Output is correct |
5 |
Correct |
6 ms |
21596 KB |
Output is correct |
6 |
Correct |
6 ms |
21596 KB |
Output is correct |
7 |
Correct |
7 ms |
21596 KB |
Output is correct |
8 |
Correct |
189 ms |
32964 KB |
Output is correct |
9 |
Correct |
229 ms |
34236 KB |
Output is correct |
10 |
Correct |
384 ms |
35264 KB |
Output is correct |
11 |
Runtime error |
30 ms |
44888 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |