#include using namespace std; template struct SegTree { const T INF = numeric_limits::max(); int n; vector dat; SegTree(int n_) : dat(n_ * 4, INF) { int x = 1; while (n_ > x) { x *= 2; } n = x; } void init(T x) { for (int i = 0; i < 2 * n - 1; i++) dat[i] = x; } void update(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int a, int b) { return query_sub(a, b, 0, 0, n); } T query_sub(int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return INF; } else if (a <= l && r <= b) { return dat[k]; } else { T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); return max(vl, vr); } } }; int main() { long long N, Q; cin >> N >> Q; string S; cin >> S; vector> rle; vector a(N); vector left; for (int i = 0; i < N; i++) { if (i == 0) { if (S[i] == '0') rle.push_back(make_pair(0, 1)); if (S[i] == '1') rle.push_back(make_pair(1, 1)); } else { if (rle[rle.size() - 1].first == 0 && S[i] == '0') rle[rle.size() - 1].second++; else if (rle[rle.size() - 1].first == 1 && S[i] == '0') rle.push_back(make_pair(0, 1)); else if (rle[rle.size() - 1].first == 0 && S[i] == '1') rle.push_back(make_pair(1, 1)); else if (rle[rle.size() - 1].first == 1 && S[i] == '1') rle[rle.size() - 1].second++; } a[i] = rle.size() - 1; if (rle[rle.size() - 1].second == 1) left.push_back(i); } SegTree seg(rle.size()); for (int i = 0; i < rle.size(); i++) { seg.update(i, rle[i].second); } vector rui_l(N, 0), rui_r(N, 0); for (int i = 0; i < N; i++) { if (i != 0) rui_l[i] = rui_l[i - 1]; if (S[i] == '0') rui_l[i] += 1; else rui_l[i] -= 1; if (i != 0) rui_r[N - i - 1] = rui_r[N - i]; if (S[N - i - 1] == '0') rui_r[N - i - 1] += 1; else rui_r[N - i - 1] -= 1; } for (int i = 0; i < Q; i++) { long long L, R, K; cin >> L >> R >> K; long long max_ = 0; if (a[R - 1] > a[L - 1] + 1) max_ = seg.query(a[L - 1] + 1, a[R - 1]); if (a[L - 1] + 1 < rle.size()) max_ = max(max_, left[a[L - 1] + 1] - (L - 1)); max_ = max(max_, (R - 1) - left[a[R - 1]] + 1); if (max_ < K) cout << R - L + 1 << endl; else { long long sum = rui_l[N - 1]; if (L != 1) sum -= rui_l[L - 2]; if (R != N) sum -= rui_r[R]; long long amari = (sum + 1000000 * (2 * K - 1)) % (2 * K - 1); if (amari == 0) cout << 2 * (K - 1) << endl; else if (amari < K) cout << 2 * K - 2 - amari << endl; else cout << amari - 1 << endl; } } }