結果

問題 No.2281 K → K-1 01 Flip
ユーザー rniyarniya
提出日時 2023-04-24 18:05:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 2,379 bytes
コンパイル時間 2,672 ms
コンパイル使用メモリ 200,252 KB
実行使用メモリ 7,624 KB
最終ジャッジ日時 2024-04-26 05:57:44
合計ジャッジ時間 12,870 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 13 ms
6,016 KB
testcase_02 AC 123 ms
5,504 KB
testcase_03 AC 132 ms
7,116 KB
testcase_04 AC 29 ms
5,376 KB
testcase_05 AC 50 ms
5,888 KB
testcase_06 AC 91 ms
5,504 KB
testcase_07 AC 27 ms
7,228 KB
testcase_08 AC 70 ms
7,256 KB
testcase_09 AC 106 ms
5,376 KB
testcase_10 AC 84 ms
5,376 KB
testcase_11 AC 36 ms
5,376 KB
testcase_12 AC 29 ms
7,216 KB
testcase_13 AC 106 ms
5,376 KB
testcase_14 AC 61 ms
6,016 KB
testcase_15 AC 28 ms
5,376 KB
testcase_16 AC 107 ms
5,632 KB
testcase_17 AC 124 ms
5,888 KB
testcase_18 AC 112 ms
5,376 KB
testcase_19 AC 54 ms
7,040 KB
testcase_20 AC 34 ms
5,376 KB
testcase_21 AC 35 ms
7,040 KB
testcase_22 AC 100 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 60 ms
5,376 KB
testcase_25 AC 35 ms
5,376 KB
testcase_26 AC 37 ms
6,912 KB
testcase_27 AC 79 ms
7,220 KB
testcase_28 AC 37 ms
7,244 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 63 ms
5,376 KB
testcase_31 AC 58 ms
5,632 KB
testcase_32 AC 90 ms
6,656 KB
testcase_33 AC 50 ms
5,632 KB
testcase_34 AC 67 ms
5,376 KB
testcase_35 AC 58 ms
7,312 KB
testcase_36 AC 83 ms
5,376 KB
testcase_37 AC 94 ms
5,376 KB
testcase_38 AC 79 ms
5,376 KB
testcase_39 AC 43 ms
5,376 KB
testcase_40 AC 17 ms
5,632 KB
testcase_41 AC 116 ms
7,296 KB
testcase_42 AC 118 ms
7,496 KB
testcase_43 AC 111 ms
7,424 KB
testcase_44 AC 134 ms
7,532 KB
testcase_45 AC 129 ms
7,624 KB
testcase_46 AC 103 ms
7,168 KB
testcase_47 AC 122 ms
7,424 KB
testcase_48 AC 114 ms
7,296 KB
testcase_49 AC 135 ms
7,620 KB
testcase_50 AC 116 ms
7,296 KB
testcase_51 AC 84 ms
7,168 KB
testcase_52 AC 81 ms
7,168 KB
testcase_53 AC 80 ms
7,168 KB
testcase_54 AC 81 ms
7,296 KB
testcase_55 AC 80 ms
7,296 KB
testcase_56 AC 80 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

#include "atcoder/segtree"

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int op(int l, int r) { return max(l, r); }

int e() { return 0; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    string S;
    cin >> S;

    vector<int> sum(N + 1), len(N), diff;
    sum[0] = 0;
    for (int i = 0; i < N; i++) {
        sum[i + 1] = sum[i] + (S[i] == '0' ? 1 : -1);
        if (i == 0 or S[i] != S[i - 1])
            len[i] = 1;
        else
            len[i] = len[i - 1] + 1;
        if (i > 0 and S[i] != S[i - 1]) diff.emplace_back(i);
    }
    diff.emplace_back(N);
    atcoder::segtree<int, op, e> seg(len);

    auto query = [&](int L, int R, int K) -> int {
        bool ok = false;
        int nxt = *lower_bound(diff.begin(), diff.end(), L);
        if (nxt >= R) {
            if (R - L >= K) {
                ok = true;
            }
        } else {
            if (nxt - L >= K) ok = true;
            if (seg.prod(nxt, R) >= K) ok = true;
        }
        if (not ok) return R - L;
        int x = sum[R] - sum[L];
        x = (x % (2 * K - 1) + (2 * K - 1)) % (2 * K - 1);
        x = min(x, 2 * K - 1 - x);
        int ans = 2 * K - 2 - x;
        return ans;
    };

    for (; Q--;) {
        int L, R, K;
        cin >> L >> R >> K;
        cout << query(--L, R, K) << '\n';
    }
    return 0;
}
0