This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/predecessor_problem"
#include "../../../src/template/template.hpp"
#include "../../../src/data_structure/segment_tree.hpp"
int op(int a, int b) {
return a + b;
}
int e() {
return 0;
}
int main(void) {
int n, q;
cin >> n >> q;
string t;
cin >> t;
vector<int> a(n);
rep(i, 0, n) {
a[i] = t[i] - '0';
}
SegmentTree<int, op, e> seg(a);
while(q--) {
int c, k;
cin >> c >> k;
if(c == 0) {
seg.set(k, 1);
} else if(c == 1) {
seg.set(k, 0);
} else if(c == 2) {
cout << seg.get(k) << '\n';
} else if(c == 3) {
auto f = [&](int v) -> bool {
return v == 0;
};
int ans = seg.max_right(k, f);
if(ans == n) ans = -1;
cout << ans << '\n';
} else if(c == 4) {
auto f = [&](int v) -> bool {
return v == 0;
};
int ans = seg.min_left(k + 1, f) - 1;
cout << ans << '\n';
} else {
assert(0);
}
}
}
#line 1 "verify/library_checker/data_structure/predecessor_problem.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/predecessor_problem"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/data_structure/segment_tree.hpp"
template <typename S, auto op, auto e>
struct SegmentTree {
SegmentTree(const int N)
: SegmentTree(vector<S>(N, e())) {}
SegmentTree(const vector<S>& v)
: n((int)v.size()) {
size = bit_ceil((unsigned int)n);
log = countr_zero((unsigned int)size);
data = vector<S>(2 * size, e());
for(int i = 0; i < n; ++i) {
data[size + i] = v[i];
}
for(int i = size - 1; i >= 1; --i) {
update(i);
}
}
void set(int p, const S& x) {
assert(0 <= p and p < n);
p += size;
data[p] = x;
for(int i = 1; i <= log; ++i) {
update(p >> i);
}
}
S get(const int p) const {
assert(0 <= p and p < n);
return data[p + size];
}
S prod(int l, int r) const {
assert(0 <= l and l <= r and r <= n);
S sml = e(), smr = e();
l += size;
r += size;
while(l < r) {
if(l & 1) sml = op(sml, data[l++]);
if(r & 1) smr = op(data[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() const {
return data[1];
}
template <bool (*f)(S)>
int max_right(const int l) const {
return max_right(l, [](const S& x) { return f(x); });
}
template <class F>
int max_right(int l, const F& f) const {
assert(0 <= l and l <= n);
assert(f(e()));
if(l == n) return n;
l += size;
S sm = e();
do {
while(l % 2 == 0) l >>= 1;
if(!f(op(sm, data[l]))) {
while(l < size) {
l = l * 2;
if(f(op(sm, data[l]))) {
sm = op(sm, data[l]);
++l;
}
}
return l - size;
}
sm = op(sm, data[l]);
++l;
} while((l & -l) != l);
return n;
}
template <bool (*f)(S)>
int min_left(const int r) const {
return min_left(r, [](const S& x) { return f(x); });
}
template <class F>
int min_left(int r, const F& f) const {
assert(0 <= r and r <= n);
assert(f(e()));
if(r == 0) return 0;
r += size;
S sm = e();
do {
--r;
while(r > 1 and (r % 2)) r >>= 1;
if(!f(op(data[r], sm))) {
while(r < size) {
r = 2 * r + 1;
if(f(op(data[r], sm))) {
sm = op(data[r], sm);
--r;
}
}
return r + 1 - size;
}
sm = op(data[r], sm);
} while((r & -r) != r);
return 0;
}
private:
int n, size, log;
vector<S> data;
inline void update(const int k) {
data[k] = op(data[2 * k], data[2 * k + 1]);
}
inline unsigned int bit_ceil(const unsigned int n) const {
unsigned int res = 1;
while(res < n) res *= 2;
return res;
}
inline int countr_zero(const unsigned int n) const {
return __builtin_ctz(n);
}
};
#line 4 "verify/library_checker/data_structure/predecessor_problem.test.cpp"
int op(int a, int b) {
return a + b;
}
int e() {
return 0;
}
int main(void) {
int n, q;
cin >> n >> q;
string t;
cin >> t;
vector<int> a(n);
rep(i, 0, n) {
a[i] = t[i] - '0';
}
SegmentTree<int, op, e> seg(a);
while(q--) {
int c, k;
cin >> c >> k;
if(c == 0) {
seg.set(k, 1);
} else if(c == 1) {
seg.set(k, 0);
} else if(c == 2) {
cout << seg.get(k) << '\n';
} else if(c == 3) {
auto f = [&](int v) -> bool {
return v == 0;
};
int ans = seg.max_right(k, f);
if(ans == n) ans = -1;
cout << ans << '\n';
} else if(c == 4) {
auto f = [&](int v) -> bool {
return v == 0;
};
int ans = seg.min_left(k + 1, f) - 1;
cout << ans << '\n';
} else {
assert(0);
}
}
}