結果

問題 No.2281 K → K-1 01 Flip
ユーザー jianglyjiangly
提出日時 2023-04-21 22:21:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 3,771 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 206,968 KB
実行使用メモリ 9,064 KB
最終ジャッジ日時 2024-04-24 08:27:56
合計ジャッジ時間 9,418 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 12 ms
7,168 KB
testcase_02 AC 64 ms
6,272 KB
testcase_03 AC 74 ms
8,336 KB
testcase_04 AC 18 ms
5,376 KB
testcase_05 AC 33 ms
6,912 KB
testcase_06 AC 57 ms
6,144 KB
testcase_07 AC 20 ms
8,172 KB
testcase_08 AC 46 ms
8,352 KB
testcase_09 AC 64 ms
5,376 KB
testcase_10 AC 52 ms
5,376 KB
testcase_11 AC 22 ms
5,376 KB
testcase_12 AC 21 ms
7,788 KB
testcase_13 AC 62 ms
5,376 KB
testcase_14 AC 37 ms
7,040 KB
testcase_15 AC 17 ms
5,376 KB
testcase_16 AC 66 ms
6,400 KB
testcase_17 AC 75 ms
7,168 KB
testcase_18 AC 66 ms
5,632 KB
testcase_19 AC 35 ms
7,684 KB
testcase_20 AC 22 ms
5,376 KB
testcase_21 AC 27 ms
6,528 KB
testcase_22 AC 62 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 36 ms
5,376 KB
testcase_25 AC 26 ms
5,376 KB
testcase_26 AC 27 ms
6,784 KB
testcase_27 AC 54 ms
8,152 KB
testcase_28 AC 27 ms
7,708 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 38 ms
5,376 KB
testcase_31 AC 44 ms
5,888 KB
testcase_32 AC 61 ms
6,912 KB
testcase_33 AC 34 ms
6,400 KB
testcase_34 AC 48 ms
5,376 KB
testcase_35 AC 40 ms
7,936 KB
testcase_36 AC 57 ms
5,376 KB
testcase_37 AC 61 ms
5,760 KB
testcase_38 AC 63 ms
5,376 KB
testcase_39 AC 30 ms
5,376 KB
testcase_40 AC 13 ms
6,272 KB
testcase_41 AC 87 ms
7,936 KB
testcase_42 AC 84 ms
8,296 KB
testcase_43 AC 84 ms
8,064 KB
testcase_44 AC 86 ms
9,064 KB
testcase_45 AC 89 ms
8,424 KB
testcase_46 AC 78 ms
7,040 KB
testcase_47 AC 85 ms
8,064 KB
testcase_48 AC 84 ms
7,808 KB
testcase_49 AC 87 ms
8,936 KB
testcase_50 AC 83 ms
7,552 KB
testcase_51 AC 76 ms
6,656 KB
testcase_52 AC 73 ms
6,656 KB
testcase_53 AC 75 ms
6,784 KB
testcase_54 AC 74 ms
6,656 KB
testcase_55 AC 73 ms
6,656 KB
testcase_56 AC 73 ms
6,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

template<class T,
    class Cmp = std::less<T>>
struct RMQ {
    const Cmp cmp = Cmp();
    static constexpr unsigned B = 64;
    using u64 = unsigned long long;
    int n;
    std::vector<std::vector<T>> a;
    std::vector<T> pre, suf, ini;
    std::vector<u64> stk;
    RMQ() {}
    RMQ(const std::vector<T> &v) {
        init(v);
    }
    void init(const std::vector<T> &v) {
        n = v.size();
        pre = suf = ini = v;
        stk.resize(n);
        if (!n) {
            return;
        }
        const int M = (n - 1) / B + 1;
        const int lg = std::__lg(M);
        a.assign(lg + 1, std::vector<T>(M));
        for (int i = 0; i < M; i++) {
            a[0][i] = v[i * B];
            for (int j = 1; j < B && i * B + j < n; j++) {
                a[0][i] = std::min(a[0][i], v[i * B + j], cmp);
            }
        }
        for (int i = 1; i < n; i++) {
            if (i % B) {
                pre[i] = std::min(pre[i], pre[i - 1], cmp);
            }
        }
        for (int i = n - 2; i >= 0; i--) {
            if (i % B != B - 1) {
                suf[i] = std::min(suf[i], suf[i + 1], cmp);
            }
        }
        for (int j = 0; j < lg; j++) {
            for (int i = 0; i + (2 << j) <= M; i++) {
                a[j + 1][i] = std::min(a[j][i], a[j][i + (1 << j)], cmp);
            }
        }
        for (int i = 0; i < M; i++) {
            const int l = i * B;
            const int r = std::min(1U * n, l + B);
            u64 s = 0;
            for (int j = l; j < r; j++) {
                while (s && cmp(v[j], v[std::__lg(s) + l])) {
                    s ^= 1ULL << std::__lg(s);
                }
                s |= 1ULL << (j - l);
                stk[j] = s;
            }
        }
    } 
    T operator()(int l, int r) {
        if (l / B != (r - 1) / B) {
            T ans = std::min(suf[l], pre[r - 1], cmp);
            l = l / B + 1;
            r = r / B;
            if (l < r) {
                int k = std::__lg(r - l);
                ans = std::min({ans, a[k][l], a[k][r - (1 << k)]}, cmp);
            }
            return ans;
        } else {
            int x = B * (l / B);
            return ini[__builtin_ctzll(stk[r - 1] >> (l - x)) + l];
        }
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N, Q;
    std::cin >> N >> Q;
    
    std::string S;
    std::cin >> S;
    
    std::vector<int> l(N), r(N), sum(N+1);
    for (int i = 1; i < N; i++) {
        l[i] = S[i] == S[i-1] ? l[i-1] : i;
    }
    r[N-1] = N-1;
    for (int i = N-2; i >= 0; i--) {
        r[i] = S[i] == S[i+1] ? r[i+1] : i;
    }
    for (int i = 0; i < N; i++) {
        sum[i+1] = sum[i] + (S[i] == '0' ? 1 : -1);
    }
    
    std::vector<int> id(N), len;
    int cnt = 0;
    for (int i = 0; i < N; i = r[i]+1) {
        id[i] = cnt++;
        len.push_back(r[i]-l[i]+1);
    }
    
    RMQ<int, std::greater<>> rmq(len);
    
    while (Q--) {
        int L, R, K;
        std::cin >> L >> R >> K;
        L--, R--;
        
        int maxlen = 0;
        if (r[L] >= R) {
            maxlen = R-L+1;
        } else {
            maxlen = std::max(r[L] - L + 1, R - l[R] + 1);
            if (r[L] + 1 < l[R]) {
                maxlen = std::max(maxlen, rmq(id[r[L]+1], id[l[R]]));
            }
        }
        
        if (maxlen < K) {
            std::cout << R-L+1 << "\n";
        } else {
            int m = 2*K - 1;
            int s = ((sum[R+1] - sum[L]) % m + m) % m;
            int ans;
            if (s < K) {
                ans = 2 * (K-1) - s;
            } else {
                ans = s - 1;
            }
            std::cout << ans << "\n";
        }
    }
    
    return 0;
}
0