#include using namespace std; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") int main() { cin.tie(0); ios::sync_with_stdio(false); 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; } 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 - RLE_left.at(r) + 1 >= K) ok = true; else { for (int i = l + 1; i <= r - 1; i++) { if (RLE_len[i] >= K) { ok = true; break; } } } if (!ok) { cout << R - L + 1 << '\n'; 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) << '\n'; } }