# [Trie树] BZOJ3689: 异或之

n<=100000

### 题解

#include<cstdio>
#include<queue>
#include<algorithm>
#define Fir first
#define Sec second
using namespace std;
const int maxn=100005;
priority_queue< pair< int,pair<int,int> > > _heap;
int n,K,a[maxn];
struct node{
int sz; node* ch[2];
node(node* son=NULL){ ch[0]=ch[1]=son; sz=0; }
} nil,*null=&nil,*root=null;
typedef node* P_node;
void Insert(P_node &p,int val,int now){
if(p==null) p=new node(null);
p->sz++;
if(now) Insert(p->ch[(val&now)?1:0],val,now>>1);
}
int Kth(P_node p,int k,int val,int now){
if(!now) return 0;
int t=p->ch[(val&now)?1:0]->sz;
if(k<=t) return Kth(p->ch[(val&now)?1:0],k,val,now>>1);
else return now+Kth(p->ch[(val&now)?0:1],k-t,val,now>>1);
}
int main(){
freopen("bzoj3689.in","r",stdin);
freopen("bzoj3689.out","w",stdout);
scanf("%d%d",&n,&K);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
Insert(root,a[i],1<<30);
}
for(int i=1;i<=n;i++) _heap.push(make_pair(-Kth(root,2,a[i],1<<30),make_pair(i,2)));
for(int i=1;i<=2*K;i++){
pair< int,pair<int,int> > x=_heap.top(); _heap.pop();
if(i&1) printf("%d ",-x.Fir);
if(x.Sec.Sec<n) _heap.push(make_pair(-Kth(root,x.Sec.Sec+1,a[x.Sec.Fir],1<<30),make_pair(x.Sec.Fir,x.Sec.Sec+1)));
}
return 0;
}