#line 1 "..\\Main.cpp" #include #include #include #include #include #include #line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\array\\segment-tree.hpp" #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\array\\segment-tree.hpp" namespace nachia{ template< class S, S op(S l, S r) > struct SegmentTree { private: int N; std::vector A; void mergev(int i){ if(i < N) A[i] = op(A[i*2], A[i*2+1]); } public: SegmentTree(int n, S e){ N = 1; while (N < n) N *= 2; A.assign(N * 2, e); } SegmentTree(const std::vector& a, S e) : SegmentTree(a.size(), e){ for(int i=0; i<(int)a.size(); i++) A[i + N] = a[i]; for(int i=N-1; i>=1; i--) mergev(i); } void set(int p, S x){ p += N; A[p] = x; for(int d=1; (1<>d); } S get(int p){ return A[N+p]; } S prod(int l, int r) const { l += N; r += N; S ql = A[0], qr = A[0]; while(l int minLeft(int r, E cmp, int a = 0, int b = 0, int i = -1){ static S x; if(i == -1){ a=0; b=N; i=1; x=A[0]; } if(r <= a) return a; if(b <= r){ S nx = op(A[i], x); if(cmp(nx)){ x = nx; return a; } } if(b - a == 1) return b; int q = minLeft(r, cmp, (a+b)/2, b, i*2+1); if(q > (a+b)/2) return q; return minLeft(r, cmp, a, (a+b)/2, i*2); } // bool cmp(S) template int maxRight(int l, E cmp, int a = 0, int b = 0, int i = -1){ static S x; if(i == -1){ a=0; b=N; i=1; x=A[0]; } if(b <= l) return b; if(l <= a){ S nx = op(x, A[i]); if(cmp(nx)){ x = nx; return b; } } if(b - a == 1) return a; int q = maxRight(l, cmp, a, (a+b)/2, i*2); if(q < (a+b)/2) return q; return maxRight(l, cmp, (a+b)/2, b, i*2+1); } }; } // namespace nachia #line 8 "..\\Main.cpp" using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; struct Mon { int len = 0; int llen = 0; int rlen = 0; int maxlen = 0; }; Mon op(Mon l, Mon r){ Mon res; res.maxlen = max({ l.maxlen, r.maxlen, l.rlen + r.llen }); res.llen = l.llen; if(l.len != -1) res.llen = l.len + r.llen; res.rlen = r.rlen; if(r.len != -1) res.rlen = r.len + l.rlen; res.len = -1; if(l.len != -1 && r.len != -1) res.len = l.len + r.len; return res; } int main(){ int N, Q; cin >> N >> Q; string S; cin >> S; atcoder::fenwick_tree rangesum(N); rep(i,N) rangesum.add(i, S[i] == '0' ? -1 : 1); nachia::SegmentTree seg(N, Mon()); rep(i,N){ if(i != 0 && S[i-1] != S[i]) seg.set(i, Mon{-1,0,1,1}); else seg.set(i, Mon{1,1,1,1}); } rep(q,Q){ int l, r, k; cin >> l >> r >> k; l--; int smod = k * 2 - 1; int s = ((rangesum.sum(l, r) % smod) + smod) % smod; int maxlen = seg.prod(l, r).maxlen; if(maxlen < k){ cout << (r-l) << '\n'; } else{ if(s >= k) s -= smod; s = abs(s); cout << (k*2-2 - s) << '\n'; } } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;