#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <limits.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
const int MOD = 1e9;
template<class T>
class SegmentTreeMax {
public:
vector<T> val;
SegmentTreeMax (int N) {
N = (1 << ((int)floor(log2(N - 1)) + 1));
this->N = N;
val.assign(2 * N, ID);
}
void update (int x, T y) {
x += N - 1;
val[x] = y;
while (x != 0) {
x = (x - 1)/2;
val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
}
}
T query (int ind, const int l, const int r, int tl, int tr) {
if (tl >= l && tr <= r) {
return val[ind];
}
if (tr < l || tl > r) {
return ID;
}
return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
}
T query (int l, int r) {
return query(0, l, r, 0, N - 1);
}
private:
T ID = 0;
T merge (T x, T y) {
return max(x, y);
}
int N;
};
template<class T>
class SegmentTreeAnd {
public:
vector<T> val;
SegmentTreeAnd (int N) {
N = (1 << ((int)floor(log2(N - 1)) + 1));
this->N = N;
val.assign(2 * N, ID);
}
void update (int x, T y) {
x += N - 1;
val[x] = y;
while (x != 0) {
x = (x - 1)/2;
val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
}
}
T query (int ind, const int l, const int r, int tl, int tr) {
if (tl >= l && tr <= r) {
return val[ind];
}
if (tr < l || tl > r) {
return ID;
}
return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
}
T query (int l, int r) {
return query(0, l, r, 0, N - 1);
}
private:
T ID = 0;
T merge (T x, T y) {
return x & y;
}
int N;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, Q;
cin >> N >> Q;
string s;
cin >> s;
SegmentTreeAnd<bool> sta(N);
SegmentTreeMax<int> stm(N);
for (int i = 0; i < s.length(); i++) {
if (s[i] == '0') {
sta.update(i, false);
} else {
sta.update(i, true);
}
stm.update(i, 0);
}
for (int q = 1; q <= Q; q++) {
string str;
cin >> str;
if (str == "query") {
int x, y; cin >> x >> y; x--, y--;
if (sta.query(x, y) == 0) {
cout << 0 << '\n';
continue;
}
cout << q - stm.query(x, y) << '\n';
} else {
int x; cin >> x; x--;
stm.update(x, q);
if (sta.query(x,x) == 0) {
sta.update(x, true);
} else {
sta.update(x, false);
}
}
}
}