結果

問題 No.911 ラッキーソート
ユーザー damenin27844103damenin27844103
提出日時 2019-10-19 14:34:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 5,875 bytes
コンパイル時間 1,119 ms
コンパイル使用メモリ 122,648 KB
実行使用メモリ 12,056 KB
最終ジャッジ日時 2023-09-09 11:03:20
合計ジャッジ時間 8,738 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 276 ms
11,968 KB
testcase_14 AC 271 ms
12,048 KB
testcase_15 AC 255 ms
11,868 KB
testcase_16 AC 259 ms
12,052 KB
testcase_17 AC 259 ms
11,872 KB
testcase_18 AC 264 ms
12,040 KB
testcase_19 AC 269 ms
11,896 KB
testcase_20 AC 251 ms
11,660 KB
testcase_21 AC 238 ms
11,612 KB
testcase_22 AC 213 ms
11,792 KB
testcase_23 AC 215 ms
10,424 KB
testcase_24 AC 188 ms
9,080 KB
testcase_25 AC 101 ms
7,448 KB
testcase_26 AC 89 ms
6,228 KB
testcase_27 AC 38 ms
4,480 KB
testcase_28 AC 21 ms
4,380 KB
testcase_29 AC 7 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 267 ms
12,056 KB
testcase_39 AC 211 ms
11,316 KB
testcase_40 AC 6 ms
4,380 KB
testcase_41 AC 246 ms
11,672 KB
testcase_42 AC 149 ms
7,908 KB
testcase_43 AC 282 ms
12,052 KB
testcase_44 AC 128 ms
4,836 KB
testcase_45 AC 131 ms
4,888 KB
testcase_46 AC 131 ms
4,984 KB
testcase_47 AC 128 ms
4,892 KB
testcase_48 AC 128 ms
4,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <complex>
#include <valarray>
#include <fstream>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <random>

#define _overload(a, b, c, d, ...) d
#define _rep1(X, A, Y) for (int (X) = (A);(X) <= (Y);++(X))
#define _rep2(X, Y) for (int (X) = 0;(X) < (Y);++(X))
#define rep(...) _overload(__VA_ARGS__, _rep1, _rep2)(__VA_ARGS__)
#define rrep(X, Y) for (int (X) = Y-1;(X) >= 0;--(X))
#define all(X) (X).begin(),(X).end()
#define len(X) ((int)(X).size())
#define mod(n, m) (((n)%(m)+(m))%m)
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
const int INFINT = 1 << 30;                          // 1.07x10^ 9
const ll INFLL = 1LL << 60;                          // 1.15x10^18
const double EPS = 1e-10;
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MAX_N = 200000;
int N;
ll L, R;
ll A[MAX_N];
int bitmap[61];

void set_bitmap();

ll max_x();

ll min_x();

int main() {

    cin >> N >> L >> R;
    rep(i, N) cin >> A[i];

    set_bitmap();

    ll ma = max_x();
    ll mi = min_x();

    if (ma < L) {
        cout << 0 << endl;
    } else if (mi > R) {
        cout << 0 << endl;
    } else {
        ll mi_bit = 0, ma_bit = 0;
        for (int i = 61; i >= 0; --i) {
            if (bitmap[i] == 2) {
                mi_bit <<= 1; ma_bit <<= 1;
                mi_bit |= ((mi>>i)&1);
                ma_bit |= ((ma>>i)&1);
            }
        }
        cout << ma_bit - mi_bit + 1 << endl;
    }

    return 0;
}

ll max_x() {
    ll res = 0;

    for (int i = 61; i >= 0; --i) {
        if (bitmap[i] == 1) {
            res += 1LL << i;
        }
    }

    if (res > R) return -1;

    for (int i = 61; i >= 0; --i) {
        if (bitmap[i] == 2) {
            ll tmp = res;
            tmp += 1LL << i;
            if (tmp <= R) {
                res = tmp;
            }
        }
    }

    return res;
}

ll min_x() {
    ll res = 0;

    for (int i = 61; i >= 0; --i) {
        if (bitmap[i] == 1) {
            res += 1LL << i;
        }
    }

    if (res >= L) return res;

    for (int i = 61; i >= 0; --i) {
        if (bitmap[i] == 2) {
            ll tmp = res;
            tmp += 1LL << i;
            if (tmp < L) res = tmp;
        }
    }

    int first_zero_bit = -1;
    for (int i = 0; i <= 61; ++i) {
        if ((bitmap[i] == 2) && ((res >> i) & 1) == 0) {
            first_zero_bit = i;
            break;
        }
    }

    if (first_zero_bit < 0) return INFLL;

    res += 1ll << first_zero_bit;
    for (int i = 0; i < first_zero_bit; ++i) {
        if (bitmap[i] == 2) {
            res -= 1ll << i;
        }
    }

    return res;
}


void set_bitmap() {
    vector<Pii> section;
    section.emplace_back(0, N);
    for (ll i = 61; i >= 0; --i) {
        vector<Pii> tmp_section;
        bool must_flag = false;
        bool must_not_flag = false;
        for (ll j = 0; j < len(section); ++j) {
            ll lb = section[j].fi;
            ll ub = section[j].sc;

            bool all_zero = true;
            for (ll k = lb; k < ub; ++k) {
                if (((A[k] >> i) & 1) == 1) {
                    all_zero = false;
                    break;
                }
            }
            if (all_zero) {
                tmp_section.emplace_back(Pii(lb, ub));
                continue;
            }

            bool all_one = true;
            for (ll k = lb; k < ub; ++k) {
                if (((A[k] >> i) & 1) == 0) {
                    all_one = false;
                    break;
                }
            }
            if (all_one) {
                tmp_section.emplace_back(Pii(lb, ub));
                continue;
            }

            ll first_one_index = 0;
            if (((A[lb] >> i) & 1) == 0) {
                for (ll k = lb; k < ub; ++k) {
                    if (((A[k] >> i) & 1) == 1) {
                        first_one_index = k;
                        break;
                    }
                }

                bool valid_section = true;
                for (ll k = first_one_index; k < ub; ++k) {
                    if (((A[k] >> i) & 1) == 0) {
                        valid_section = false;
                        break;
                    }
                }

                if (!valid_section) {
                    cout << 0 << endl;
                    exit(0);
                }

                must_not_flag = true;
            } else {
                for (ll k = lb; k < ub; ++k) {
                    if (((A[k] >> i) & 1) == 0) {
                        first_one_index = k;
                        break;
                    }
                }

                bool valid_section = true;
                for (ll k = first_one_index; k < ub; ++k) {
                    if (((A[k] >> i) & 1) == 1) {
                        valid_section = false;
                        break;
                    }
                }

                if (!valid_section) {
                    cout << 0 << endl;
                    exit(0);
                }

                must_flag = true;
            }

            tmp_section.emplace_back(lb, first_one_index);
            tmp_section.emplace_back(first_one_index, ub);
        }

        if (must_not_flag && must_flag) {
            cout << 0 << endl;
            exit(0);
        } else if (must_flag) {
            bitmap[i] = 1;
        } else if (must_not_flag) {
            bitmap[i] = 0;
        } else {
            bitmap[i] = 2;
        }

        section = tmp_section;
    }
}
0