結果

問題 No.911 ラッキーソート
ユーザー MisterMister
提出日時 2020-08-04 18:45:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 659 ms / 2,000 ms
コード長 2,141 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 98,196 KB
実行使用メモリ 10,308 KB
最終ジャッジ日時 2023-10-12 21:29:54
合計ジャッジ時間 18,375 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 659 ms
7,876 KB
testcase_14 AC 648 ms
7,660 KB
testcase_15 AC 620 ms
9,176 KB
testcase_16 AC 624 ms
7,416 KB
testcase_17 AC 642 ms
7,588 KB
testcase_18 AC 619 ms
8,628 KB
testcase_19 AC 625 ms
7,432 KB
testcase_20 AC 570 ms
8,196 KB
testcase_21 AC 493 ms
10,308 KB
testcase_22 AC 441 ms
8,652 KB
testcase_23 AC 426 ms
7,640 KB
testcase_24 AC 379 ms
6,896 KB
testcase_25 AC 208 ms
8,140 KB
testcase_26 AC 197 ms
4,728 KB
testcase_27 AC 73 ms
4,416 KB
testcase_28 AC 38 ms
4,352 KB
testcase_29 AC 15 ms
4,348 KB
testcase_30 AC 4 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 641 ms
9,564 KB
testcase_39 AC 440 ms
8,292 KB
testcase_40 AC 14 ms
4,348 KB
testcase_41 AC 576 ms
8,312 KB
testcase_42 AC 316 ms
6,672 KB
testcase_43 AC 645 ms
7,532 KB
testcase_44 AC 64 ms
7,008 KB
testcase_45 AC 203 ms
6,976 KB
testcase_46 AC 300 ms
7,016 KB
testcase_47 AC 98 ms
7,012 KB
testcase_48 AC 40 ms
5,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>

template <class It>
std::vector<std::pair<typename It::value_type, int>> runlength(
    It begin, It end) {
    using T = typename It::value_type;

    std::vector<std::pair<T, int>> res;
    while (begin != end) {
        const T& c = *(begin++);
        if (res.empty() || c != res.back().first) {
            res.emplace_back(c, 1);
        } else {
            ++res.back().second;
        }
    }

    return res;
}

constexpr int K = 61;

using lint = long long;

void solve() {
    int n;
    lint xl, xr;
    std::cin >> n >> xl >> xr;

    std::vector<lint> xs(n);
    for (auto& x : xs) std::cin >> x;

    std::vector<int> ts(K, 3);
    std::function<void(int, int, int)> dfs =
        [&](int l, int r, int k) -> void {
        if (k < 0) return;

        std::vector<int> bs;
        for (int i = l; i < r; ++i) {
            bs.push_back((xs[i] >> k) & 1);
        }
        auto ps = runlength(bs.begin(), bs.end());

        int t;
        if (ps.size() == 1) {
            t = 3;
        } else if (ps.size() == 2) {
            t = 1 << bs[0];
        } else {
            t = 0;
        }
        ts[k] &= t;

        if (ps.size() == 1) {
            dfs(l, r, k - 1);
        } else if (ps.size() == 2) {
            int m = l + ps.front().second;
            dfs(l, m, k - 1);
            dfs(m, r, k - 1);
        }
    };
    dfs(0, n, K - 1);

    auto calc = [&](lint m) -> lint {
        if (m < 0) return 0;

        std::vector<lint> dp(2, 1);
        auto ndp = dp;
        for (int k = 0; k < K; ++k) {
            std::fill(ndp.begin(), ndp.end(), 0);

            for (int d = 0; d < 2; ++d) {
                if (((ts[k] >> d) & 1) == 0) continue;

                ndp[0] += dp[0];
                if (d < ((m >> k) & 1)) ndp[1] += dp[0];
                if (d == ((m >> k) & 1)) ndp[1] += dp[1];
            }

            std::swap(dp, ndp);
        }
        return dp[1];
    };

    std::cout << calc(xr) - calc(xl - 1) << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0