結果

問題 No.2977 Kth Xor Pair
ユーザー 👑 Nachia
提出日時 2024-12-02 21:20:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 177 ms / 3,000 ms
コード長 1,728 bytes
コンパイル時間 831 ms
コンパイル使用メモリ 83,624 KB
最終ジャッジ日時 2025-02-26 10:40:25
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
using namespace std;

void testcase(){
    i64 N; cin >> N;
    i64 K; cin >> K;
    K *= 2; K += N;
    vector<i64> A(N); rep(i,N) cin >> A[i];
    sort(A.begin(), A.end());
    struct Range {
        i64 l, r;
        i64 size() const { return r-l; }
    };
    struct RangePair {
        Range x0, x1;
        i64 size() const { return x0.size() * x1.size(); }
    };
    auto split = [&](Range r, i64 b) -> pair<Range, Range> {
        if(A[r.l] & b) return { {r.l,r.l}, r };
        if(!(A[r.r-1] & b)) return { r, {r.r,r.r} };
        i64 a = A[r.r-1];
        i64 m = lower_bound(A.begin(), A.end(), a - (a & (b-1))) - A.begin();
        return { {r.l,m}, {m,r.r} };
    };
    vector<RangePair> B;
    B.push_back({ 0,N,0,N });
    vector<i64> L(N), R(N,N);
    i64 ans = 0;
    for(i64 d=1ll<<29; d>0; d>>=1){
        vector<RangePair> nx0, nx1;
        for(auto b : B){
            auto [y0,y1] = split(b.x0, d);
            auto [z0,z1] = split(b.x1, d);
            if(y0.size() != 0){
                if(z0.size() != 0) nx0.push_back({ y0, z0 });
                if(z1.size() != 0) nx1.push_back({ y0, z1 });
            }
            if(y1.size() != 0){
                if(z0.size() != 0) nx1.push_back({ y1, z0 });
                if(z1.size() != 0) nx0.push_back({ y1, z1 });
            }
        }
        i64 k = 0;
        for(auto& c : nx0) k += c.size();
        if(k < K){ ans += d; K -= k; swap(nx0, nx1); }
        swap(B, nx0);
    }
    cout << ans << '\n';
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}
0