結果

問題 No.911 ラッキーソート
ユーザー fine
提出日時 2019-10-19 00:17:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,634 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 174,648 KB
実行使用メモリ 11,144 KB
最終ジャッジ日時 2024-06-25 20:08:28
合計ジャッジ時間 6,400 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, int>;

int calcBoundary(int bit, int l, int r, vector<ll>& a) {
    ll mask = (1LL << bit);
    int idx = 0;
    bool pf = (a[l] & mask);
    for (int i = l; i < r; i++) {
        bool f = (a[i] & mask);
        if (pf != f) {
            if (idx == 0) {
                pf = f;
                idx = i;
            } else {
                return -1;
            }
        } 
    }
    return idx;
}

ll calc_less(int bit, vector<ll>& a, vector<P>& v) {
    if (bit < 0) return 1;

    ll res = 1;
    for (int i = bit; i >= 0; i--) {
        ll mask = (1LL << i);
        vector<P> nv;
        bool f01 = false, f10 = false;
        for (P& p : v) {
            int b = calcBoundary(i, p.first, p.second, a);
            if (b == -1) return 0;

            if (b == 0) {
                nv.push_back(p);
            } else {
                nv.emplace_back(p.first, b);
                nv.emplace_back(b, p.second);
                if (a[b] & mask) f01 = true;
                else f10 = true;
            }
        }

        if (f01 && f10) return 0;
        if (!f10 && !f01) {
            res <<= 1;
        }
        v = move(nv);
    }
    cerr << res << ": \n";
    return res;
}

ll solve(ll lim, int n, vector<ll>& a) {
    if (lim < 0) return 0;
    vector<P> v;
    v.emplace_back(0, n);
    vector<ll> dp(2, 0);
    dp[true] = 1;
    for (int i = 60; i >= 0; i--) {
        vector<ll> ndp(2, 0);
        ll mask = (1LL << i);
        vector<P> nv;
        bool f01 = false, f10 = false;

        for (P& p : v) {
            int b = calcBoundary(i, p.first, p.second, a);
            if (b == -1) return 0;

            if (b == 0) {
                nv.push_back(p);
            } else {
                nv.emplace_back(p.first, b);
                nv.emplace_back(b, p.second);
                if (a[b] & mask) f01 = true;
                else f10 = true;
            }
        }

        if (f01 && f10) return 0;
        if (!f10) {
            ndp[false] += dp[false];
            if (lim & mask) ndp[false] += dp[true];
            else ndp[true] += dp[true];
        }
        if (!f01) {
            ndp[false] += dp[false];
            if (lim & mask) ndp[true] += dp[true];
        }
        dp = move(ndp);
        v = move(nv);
    }
    return dp[false] + dp[true];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    ll L, R;
    cin >> n >> L >> R;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    cout << solve(R, n, a) - solve(L - 1, n, a) << "\n";
    return 0;
}
0