結果
| 問題 | No.2977 Kth Xor Pair |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-12-12 05:23:00 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,134 ms / 3,000 ms |
| コード長 | 3,049 bytes |
| コンパイル時間 | 6,038 ms |
| コンパイル使用メモリ | 261,072 KB |
| 最終ジャッジ日時 | 2025-02-26 12:14:20 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 34 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
bool deb = 1;
struct Binary_Trie {
ll base = 31; // 桁数
struct Node {
ll ne[2];
ll c, num;
Node(ll c_) : c(c_), num(0) { ne[0] = ne[1] = -1; }
};
vector<Node> nd;
ll rt = 0;
Binary_Trie() { nd.push_back(Node(rt)); }
void insert(const ll &num) {
ll id = 0;
for(ll b = base; b >= 0; b--) {
ll nt = 0;
nt = ((num>>b)&1);
if(nd[id].ne[nt] == -1) {
nd.push_back(Node(nt));
nd[id].ne[nt] = nd.size() - 1;
}
nd[id].num++;
id = nd[id].ne[nt];
}
nd[id].num++;
}
bool search(const ll &num) {
ll id = 0;
bool EX = 1;
for(ll b = base; b >= 0; b--) {
ll nt = 0;
nt = ((num>>b)&1);
if(nd[id].ne[nt] == -1
|| nd[nd[id].ne[nt]].num == 0) {
EX = 0;
break;
}
id = nd[id].ne[nt];
}
return EX;
}
void erase(const ll &num) {
if(!search(num)) return;
ll id = 0;
for(ll b = base; b >= 0; b--) {
ll nt = 0;
nt = ((num>>b)&1);
nd[id].num--;
id = nd[id].ne[nt];
}
nd[id].num--;
}
ll small_num(const ll &p, const ll &x) { // d XOR p < x なる d の個数
ll res = 0;
ll id = 0;
for(ll b = base; b >= 0; b--) {
ll nx = 0, np = 0;
nx = ((x>>b)&1);
np = ((p>>b)&1);
if(nx == 1) {
if(nd[id].ne[np] != -1) {
res += nd[nd[id].ne[np]].num;
}
if(nd[id].ne[1 - np] == -1) { break; }
else {
id = nd[id].ne[1 - np];
continue;
}
}
else {
if(nd[id].ne[np] == -1) { break; }
else {
id = nd[id].ne[np];
continue;
}
}
}
return res;
}
ll min_XOR(ll x) {
ll res = 0, id = 0;
for(ll b = base; b >= 0; b--) {
ll nx = 0;
nx = ((x>>b)&1);
if(nd[id].ne[nx] != -1
&& nd[nd[id].ne[nx]].num > 0) {
id = nd[id].ne[nx];
}
else {
res += (1ll << b);
id = nd[id].ne[1 - nx];
}
}
return res;
}
ll K_thsmall(ll K) { // K番目に小さい値を返す.(0index)
if(nd[0].num <= K) return -1e18;
ll id = 0, res = 0;
for(ll b = base; b >= 0; b--) {
if(nd[id].ne[0] != -1
&& nd[nd[id].ne[0]].num > K) {
id = nd[id].ne[0];
}
else {
res += (1ll << b);
if(nd[id].ne[0] != -1) {
K -= nd[nd[id].ne[0]].num;
}
id = nd[id].ne[1];
}
}
return res;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll N=2e5;
ll K=(N*(N-1))/4;
cin>>N>>K;
vector<ll> A(N);
Binary_Trie B;
for(int i=0;i<N;i++){
cin>>A[i];
// A[i]=rand()%ll(1e9);
B.insert(A[i]);
}
ll L=-1,R=4e9;
while(R-L>1){
ll M=(R+L)/2;
ll cnt=0;
if(M!=0)for(int i=0;i<N;i++){
cnt+=B.small_num(A[i],M)-1;
}
(cnt/2<K?L:R)=M;
}
cout<<L<<endl;
}