結果

問題 No.911 ラッキーソート
ユーザー ゆきのんゆきのん
提出日時 2020-03-10 21:05:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 2,391 bytes
コンパイル時間 1,692 ms
コンパイル使用メモリ 173,364 KB
実行使用メモリ 28,252 KB
最終ジャッジ日時 2024-04-27 18:52:31
合計ジャッジ時間 8,820 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 229 ms
22,744 KB
testcase_14 AC 235 ms
21,848 KB
testcase_15 AC 238 ms
27,640 KB
testcase_16 AC 241 ms
21,424 KB
testcase_17 AC 240 ms
21,220 KB
testcase_18 AC 242 ms
24,172 KB
testcase_19 AC 239 ms
21,084 KB
testcase_20 AC 240 ms
23,520 KB
testcase_21 AC 249 ms
27,636 KB
testcase_22 AC 240 ms
21,768 KB
testcase_23 AC 231 ms
20,316 KB
testcase_24 AC 210 ms
16,832 KB
testcase_25 AC 133 ms
18,508 KB
testcase_26 AC 104 ms
9,388 KB
testcase_27 AC 50 ms
6,944 KB
testcase_28 AC 25 ms
6,944 KB
testcase_29 AC 8 ms
6,944 KB
testcase_30 AC 3 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 238 ms
28,252 KB
testcase_39 AC 236 ms
21,524 KB
testcase_40 AC 8 ms
6,944 KB
testcase_41 AC 238 ms
22,404 KB
testcase_42 AC 155 ms
15,200 KB
testcase_43 AC 231 ms
21,176 KB
testcase_44 AC 146 ms
14,172 KB
testcase_45 AC 173 ms
17,520 KB
testcase_46 AC 187 ms
18,912 KB
testcase_47 AC 152 ms
14,280 KB
testcase_48 AC 138 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> v(64,0);

void rec(int k,vector<LL> a){
    if(k < 0) return;
    int n = a.size();
    if(n == 1) return;
    int cnt = 0;
    for (int i = 0; i < n - 1; i++) {
        int t = a[i] >> k & 1, nt = a[i+1] >> k & 1;
        if(t != nt) cnt++;
    }
    if(cnt == 0){
        rec(k-1, a);
    }
    else if(cnt == 1){
        vector<LL> L,R;
        for (int i = 0; i < n; i++) {
            int t = a[i] >> k & 1;
            if(t == 0) L.pb(a[i]);
            else R.pb(a[i]);
        }
        if(a[0] >> k & 1) v[k] |= 2;
        else v[k] |= 1;
        rec(k-1, L);
        rec(k-1, R);
    }
    else{
        v[k] |= 3;
    }
    return ;
}

LL dp[64][2];

LL rec2(int k,LL s,bool f){
    if(k < 0) return 1;
    if(~dp[k][f]) return dp[k][f];
    int x = f ? s >> k & 1 : 1;
    LL ret = 0;
    if(v[k] == 0){
        for (int i = 0; i <= x; i++) {
            ret += rec2(k-1,s,f&(x == i));
        }
    }
    else if(v[k] == 1){
        ret += rec2(k-1,s,f&(x == 0));
    }
    else if(v[k] == 2){
        if(x == 0) return dp[k][f] = 0;
        ret += rec2(k-1,s,f&(x == 1));
    }
    else{
        return dp[k][f] = 0;
    }
    return dp[k][f] = ret;
}

int f(LL n){
    int ret = 0;
    while(n){
        n >>= 1;
        ret++;
    }
    return ret - 1;
}

int main(){
    LL n,l,r;cin >> n >> l >> r;
    l--;
    vector<LL> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    rec(63,a);
    LL ans = 0;
    auto solve = [](LL r){
        for (int i = f(r)+1; i <= 63; i++) {
            if(v[i] > 1) return 0LL;
        }
        memset(dp,-1,sizeof(dp));
        return rec2(f(r), r, 1);
    };
    ans += solve(r);
    if(l >= 0){
        ans -= solve(l);
    }
    cout << ans << endl;
    return 0;
}
0