#include #include #include #include using namespace std; const long long INF = 3LL << 60; // range add/max lazy segment tree class lazy_segtree { private: int sz; vector val; vector lazy; void push(int k) { if (lazy[k] != 0) { val[k] += lazy[k]; if (k < sz) { lazy[k * 2 + 0] += lazy[k]; lazy[k * 2 + 1] += lazy[k]; } lazy[k] = 0; } } void add(int a, int b, int k, int l, int r, long long x) { push(k); if (a <= l && r <= b) { lazy[k] += x; push(k); return; } if (r <= a || b <= l) { return; } add(a, b, k * 2 + 0, l, (l + r) >> 1, x); add(a, b, k * 2 + 1, (l + r) >> 1, r, x); val[k] = max(val[k * 2 + 0], val[k * 2 + 1]); } long long rangemax(int a, int b, int k, int l, int r) { push(k); if (a <= l && r <= b) { return val[k]; } if (r <= a || b <= l) { return -INF; } long long lc = rangemax(a, b, k * 2 + 0, l, (l + r) >> 1); long long rc = rangemax(a, b, k * 2 + 1, (l + r) >> 1, r); return max(lc, rc); } public: lazy_segtree() : sz(0), val(vector()), lazy(vector()) {} lazy_segtree(int n) { sz = 1; while (sz < n) { sz *= 2; } val = vector(sz * 2, 0); lazy = vector(sz * 2, 0); } void add(int l, int r, long long x) { add(l, r, 1, 0, sz, x); } long long rangemax(int l, int r) { return rangemax(l, r, 1, 0, sz); } }; int main() { int N, Q; string S; cin >> N >> Q >> S; vector A = { 0 }; for (int i = 1; i < N; i++) { if (S[i - 1] != S[i]) { A.push_back(i); } } A.push_back(N); vector sum(N + 1); for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + (S[i] == '1' ? 1 : -1); } lazy_segtree seg(N); for (int i = 0; i < int(A.size()) - 1; i++) { seg.add(A[i], A[i + 1], A[i + 1] - A[i]); } for (int i = 0; i < Q; i++) { int L, R, K; cin >> L >> R >> K; L -= 1; int p1 = lower_bound(A.begin(), A.end(), L + 1) - A.begin(); int p2 = lower_bound(A.begin(), A.end(), R) - A.begin(); int maxwidth = 0; if (p1 == p2) { maxwidth = R - L; } else if (p2 - p1 == 1) { maxwidth = max(A[p1] - L, R - A[p2 - 1]); } else { maxwidth = max(A[p1] - L, R - A[p2 - 1]); maxwidth = max(maxwidth, (int)seg.rangemax(p1, p2 - 1)); } if (maxwidth < K) { cout << R - L << '\n'; } else { int x = sum[R] - sum[L]; x = (x >= 0 ? x % (2 * K - 1) : ((2 * K - 1) - (-x) % (2 * K - 1)) % (2 * K - 1)); cout << max(x - 1, (2 * K - 1) - x - 1) << '\n'; } } return 0; }