#include using namespace std; #include using namespace atcoder; const int INF = 1e9; int op(int a, int b) { return max(a, b); } int e() { return -INF; } int main() { int N, Q; string S; cin >> N >> Q >> S; vector cum0(N + 1, 0), cum1(N + 1, 0); for (int i = 0; i < N; i++) { cum0.at(i + 1) = cum0.at(i); cum1.at(i + 1) = cum1.at(i); if (S.at(i) == '0') cum0.at(i + 1)++; else cum1.at(i + 1)++; } vector RLE_left = {0}, RLE_right = {0}, RLE_len = {1}; vector where(N, 0); for (int i = 1; i < N; i++) { if (S.at(i - 1) == S.at(i)) { RLE_right.back()++; RLE_len.back()++; } else { RLE_left.push_back(i); RLE_right.push_back(i); RLE_len.push_back(1); } where.at(i) = (int)RLE_left.size() - 1; } segtree seg(RLE_len); while (Q--) { int L, R, K; cin >> L >> R >> K; L--, R--; int l = where.at(L), r = where.at(R); bool ok = false; if (RLE_right.at(l) - L + 1 >= K) ok = true; else if (R - max(L, RLE_left.at(r)) + 1 >= K) ok = true; else { l++, r--; if (l <= r && seg.prod(l, r + 1) >= K) ok = true; } if (!ok) { cout << R - L + 1 << endl; continue; } int x = cum0.at(R + 1) - cum0.at(L); int y = cum1.at(R + 1) - cum1.at(L); int z = (x - y) % (2 * K - 1); if (z < 0) z += 2 * K - 1; cout << max(z - 1, 2 * K - 2 - z) << endl; } }