結果

問題 No.2281 K → K-1 01 Flip
ユーザー rniya
提出日時 2023-04-24 18:05:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 2,379 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 198,508 KB
最終ジャッジ日時 2025-02-12 13:51:06
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

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