結果

問題 No.2281 K → K-1 01 Flip
ユーザー 👑 NachiaNachia
提出日時 2023-04-21 22:02:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 3,872 bytes
コンパイル時間 866 ms
コンパイル使用メモリ 85,628 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-04-24 08:11:14
合計ジャッジ時間 15,352 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 32 ms
8,192 KB
testcase_02 AC 113 ms
8,064 KB
testcase_03 AC 132 ms
12,544 KB
testcase_04 AC 34 ms
5,632 KB
testcase_05 AC 61 ms
8,064 KB
testcase_06 AC 100 ms
7,936 KB
testcase_07 AC 53 ms
12,544 KB
testcase_08 AC 93 ms
12,416 KB
testcase_09 AC 108 ms
5,632 KB
testcase_10 AC 88 ms
5,632 KB
testcase_11 AC 41 ms
7,808 KB
testcase_12 AC 56 ms
12,288 KB
testcase_13 AC 106 ms
5,632 KB
testcase_14 AC 72 ms
8,064 KB
testcase_15 AC 31 ms
5,376 KB
testcase_16 AC 114 ms
7,936 KB
testcase_17 AC 129 ms
8,192 KB
testcase_18 AC 114 ms
7,808 KB
testcase_19 AC 75 ms
12,288 KB
testcase_20 AC 36 ms
5,632 KB
testcase_21 AC 60 ms
12,416 KB
testcase_22 AC 108 ms
5,632 KB
testcase_23 AC 8 ms
5,376 KB
testcase_24 AC 61 ms
5,376 KB
testcase_25 AC 42 ms
5,376 KB
testcase_26 AC 59 ms
12,288 KB
testcase_27 AC 105 ms
12,416 KB
testcase_28 AC 61 ms
12,288 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 66 ms
5,376 KB
testcase_31 AC 82 ms
8,192 KB
testcase_32 AC 112 ms
12,288 KB
testcase_33 AC 66 ms
8,064 KB
testcase_34 AC 81 ms
5,376 KB
testcase_35 AC 90 ms
12,416 KB
testcase_36 AC 91 ms
5,376 KB
testcase_37 AC 107 ms
7,936 KB
testcase_38 AC 103 ms
5,376 KB
testcase_39 AC 53 ms
5,632 KB
testcase_40 AC 31 ms
8,064 KB
testcase_41 AC 160 ms
12,672 KB
testcase_42 AC 174 ms
12,672 KB
testcase_43 AC 169 ms
12,672 KB
testcase_44 AC 165 ms
12,672 KB
testcase_45 AC 169 ms
12,544 KB
testcase_46 AC 155 ms
12,672 KB
testcase_47 AC 164 ms
12,672 KB
testcase_48 AC 162 ms
12,672 KB
testcase_49 AC 170 ms
12,672 KB
testcase_50 AC 160 ms
12,544 KB
testcase_51 AC 163 ms
12,544 KB
testcase_52 AC 161 ms
12,672 KB
testcase_53 AC 151 ms
12,544 KB
testcase_54 AC 156 ms
12,544 KB
testcase_55 AC 151 ms
12,672 KB
testcase_56 AC 152 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "..\\Main.cpp"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#include <atcoder/fenwicktree>
#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<S> 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<S>& 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)<=N; d++) mergev(p>>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<r){
            if(l&1) ql = op(ql, A[l++]);
            if(r&1) qr = op(A[--r], qr);
            l /= 2;
            r /= 2;
        }
        return op(ql, qr);
    }

    S allProd() const { return A[1]; }

    // bool cmp(S)
    template<class E>
    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<class E>
    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<int> rangesum(N);
    rep(i,N) rangesum.add(i, S[i] == '0' ? -1 : 1);
    nachia::SegmentTree<Mon, op> 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;

0