#include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct SparseTable { using BinOp = std::function; SparseTable() = default; explicit SparseTable(const std::vector& a, const BinOp bin_op) { init(a, bin_op); } void init(const std::vector& a, const BinOp bin_op_) { bin_op = bin_op_; const int n = a.size(); assert(n > 0); lg.assign(n + 1, 0); for (int i = 2; i <= n; ++i) { lg[i] = lg[i >> 1] + 1; } const int table_h = std::countr_zero(std::bit_floor(a.size())) + 1; data.assign(table_h, std::vector(n)); std::copy(a.begin(), a.end(), data.front().begin()); for (int i = 1; i < table_h; ++i) { for (int j = 0; j + (1 << i) <= n; ++j) { data[i][j] = bin_op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]); } } } Band query(const int left, const int right) const { assert(left < right); const int h = lg[right - left]; return bin_op(data[h][left], data[h][right - (1 << h)]); } private: BinOp bin_op; std::vector lg; std::vector> data; }; // https://twitter.com/maspy_stars/status/1649418845071835136 int main() { int n, q; string s; cin >> n >> q >> s; array, 2> num{}; REP(ch, 2) num[ch].assign(n + 1, 0); REP(i, n) ++num[s[i] - '0'][i + 1]; REP(ch, 2) REP(i, n) num[ch][i + 1] += num[ch][i]; vector a(n, 0); for (int i = 0; i < n;) { int j = i + 1; while (j < n && s[j] == s[i]) ++j; for (; i < j; ++i) { a[i] = j - i; } } // REP(i, n) cout << a[i] << " \n"[i + 1 == n]; SparseTable sparse_table(a, [](const int l, const int r) -> int { return max(l, r); }); while (q--) { int l, r, k; cin >> l >> r >> k; --l; --r; if (sparse_table.query(l, r - k + 2) < k) { cout << r - l + 1 << '\n'; } else { const int ans = (((num[0][r + 1] - num[0][l]) - (num[1][r + 1] - num[1][l])) % (2 * k - 1) + 2 * k - 1) % (2 * k - 1); if (ans < k) { cout << k - 1 + (k - 1 - ans) << '\n'; // [0] k-1 } else { cout << (ans - k) + k - 1 << '\n'; // [1] k-1 } } } return 0; }