結果
| 問題 |
No.2977 Kth Xor Pair
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-12-03 20:49:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,763 bytes |
| コンパイル時間 | 2,421 ms |
| コンパイル使用メモリ | 202,536 KB |
| 最終ジャッジ日時 | 2025-02-26 10:49:35 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 5 MLE * 1 -- * 28 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct BinaryTrie{
//バグあるかもよ.
struct Node{
int times;
long long lazy;
Node *LR[2];
Node():times(0),lazy(0),LR{nullptr,nullptr}{}
};
private:
int B = 30; //2^B未満の数のみ.
Node *root = nullptr;
void eval(Node *p,int &b){
if(p->lazy&(1LL<<b)) swap(p->LR[0],p->LR[1]);
if(p->LR[0] != nullptr) p->LR[0]->lazy ^= p->lazy;
if(p->LR[1] != nullptr) p->LR[1]->lazy ^= p->lazy;
p->lazy = 0;
}
public: void make(int b){B = b;}
private:
Node* insert(Node *p,long long &x,int b){
if(p == nullptr) p = new Node;
p->times++;
if(b < 0) return p;
eval(p,b);
bool one = (x&(1LL<<b))>0;
p->LR[one] = insert(p->LR[one],x,b-1);
return p;
}
public:
void insert(long long x){root = insert(root,x,B-1);}
bool insertset(long long x){
if(find(x) == 0) insert(x);
else return false;
return true;
}
private:
Node* erase(Node *p,long long &x,int b){
assert(p != nullptr);
p->times--;
if(p->times == 0) return nullptr;
if(b < 0) return p;
eval(p,b);
bool one = (x&(1LL<<b))>0;
p->LR[one] = erase(p->LR[one],x,b-1);
return p;
}
public:
void erase(long long x){root = erase(root,x,B-1);}
bool eraseset(long long x){
if(find(x) != 0) erase(x);
else return false;
return true;
}
private:
long long getkth(Node *p,int k,int b){ //0-indexed.
if(b < 0) return 0;
eval(p,b);
int L = 0;
if(p->LR[0] != nullptr) L = p->LR[0]->times;
return k<L?getkth(p->LR[0],k,b-1):getkth(p->LR[1],k-L,b-1)|(1LL<<b);
}
public:
long long getkth(int k){
if(!(0 <= k && k < size())){
cout << size() << endl;
}
assert(0 <= k && k < size());
return getkth(root,k,B-1);
}
long long getmin(long long x){
Xorall(x);
long long ret = getkth(0);
Xorall(x); return ret;
}
long long getmax(long long x){
Xorall(x);
long long ret = getkth(size()-1);
Xorall(x); return ret;
}
private:
int lowerbound(Node *p,long long &x,int b){
if(p == nullptr || b < 0) return 0;
eval(p,b);
bool one = (x&(1LL<<b))>0;
if(one && p->LR[0] != nullptr) return p->LR[0]->times+lowerbound(p->LR[one],x,b-1);
return lowerbound(p->LR[one],x,b-1);
}
public:
int lower_bound(long long x){return lowerbound(root,x,B-1);}
int upper_bound(long long x){x++; return lowerbound(root,x,B-1);}
public:
int size(){
if(root != nullptr) return root->times;
else return 0;
}
void Xorall(long long x){if(root != nullptr) root->lazy ^= x;}
int find(long long &x){
if(root == nullptr) return 0;
Node *p = root;
for(int i=B-1; i>=0; i--){
eval(p,i);
p = p->LR[(x>>i)&1];
if(p == nullptr) return 0;
}
return p->times;
}
};
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.upper_bound(mid);
now += ok;
Z.Xorall(A.at(i));
Z.insert(A.at(i));
}
if(now < K) low = mid;
else high = mid;
}
cout << high << "\n";
}