結果

問題 No.2977 Kth Xor Pair
ユーザー GOTKAKO
提出日時 2024-12-03 21:36:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,942 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 199,824 KB
最終ジャッジ日時 2025-02-26 10:50:29
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5 TLE * 1 -- * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct BinaryTrie{
    struct Node{
        int times = 0,lazy = 0;
        int L = -1,R = -1;
    };
    private:
    int B;
    vector<Node> V;
    void eval(int pos,int i){
        if(V.at(pos).lazy&(1<<i)) swap(V.at(pos).L,V.at(pos).R);
        if(V.at(pos).L != -1) V.at(V.at(pos).L).lazy ^= V.at(pos).lazy;
        if(V.at(pos).R != -1) V.at(V.at(pos).R).lazy ^= V.at(pos).lazy;
        V.at(pos).lazy = 0;
    }
    public:
    BinaryTrie():B(30),V{Node()}{}
    void insert(int x){
        int pos = 0;
        for(int i=B-1; i>=0; i--){
            V.at(pos).times++;
            if(V.at(pos).L == -1){
                V.push_back({}),V.push_back({});
                V.at(pos).L = V.size()-2,V.at(pos).R = V.size()-1;
            }
            eval(pos,i);
            if(x&(1<<i)) pos = V.at(pos).R;
            else pos = V.at(pos).L;
        }
        V.at(pos).times++;
    }
    void Xorall(int x){V.at(0).lazy ^= x;}

    int lowerbound(int x){
        int ret = 0,pos = 0;
        for(int i=B-1; i>=0; i--){
            if(pos == -1) break;
            eval(pos,i);
            if(x&(1<<i)){
                if(V.at(pos).L != -1) ret += V.at(V.at(pos).L).times;
                pos = V.at(pos).R;
            }
            else pos = V.at(pos).L;
        }
        return ret;
    }
};

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

    long long N,K; cin >> N >> K;
    vector<int> A(N);
    for(auto &a : A) cin >> a;

    int low = -1,high = (1<<30)-1;
    while(high-low > 1){
        int mid = (high+low)/2;
        long long now = 0;
        BinaryTrie Z;
        for(int i=0; i<N; i++){
            Z.Xorall(A.at(i));
            int ok = Z.lowerbound(mid+1);
            now += ok;
            Z.Xorall(A.at(i));
            Z.insert(A.at(i));
        }
        if(now < K) low = mid;
        else high = mid;
    }
    cout << high << "\n";
}
0