记录编号 | 487653 | 评测结果 | AAAAAAAAAA | ||
---|---|---|---|---|---|
题目名称 | 异或 | 最终得分 | 100 | ||
用户昵称 | 是否通过 | 通过 | |||
代码语言 | C++ | 运行时间 | 2.747 s | ||
提交时间 | 2018-02-11 15:45:14 | 内存使用 | 17.31 MiB | ||
#include<bits/stdc++.h> #define LL long long using namespace std; const int inf=1e5+10; int a[inf],n,k,siz,rt; struct node{ int ls,rs,siz; }t[inf*21]; int tmp[22]; void insert(int x){ for(int i=1;i<=21;i++)tmp[i]=x%2,x/=2; int now=rt; for(int i=21;i>=1;i--){ if(tmp[i]){ if(t[now].rs)now=t[now].rs; else now=t[now].rs=++siz; } else { if(t[now].ls)now=t[now].ls; else now=t[now].ls=++siz; } t[now].siz++; } } int tmp2[22]; int query(int x,int dep){ if(!x)return 0; if(!dep)return t[x].siz; if(tmp[dep]){ if(tmp2[dep])return t[t[x].rs].siz+query(t[x].ls,dep-1); else return query(t[x].rs,dep-1); } else { if(tmp2[dep])return t[t[x].ls].siz+query(t[x].rs,dep-1); else return query(t[x].ls,dep-1); } } LL check(int x){ LL ans=0; for(int i=1;i<=21;i++)tmp2[i]=x%2,x/=2; for(int i=1;i<=n;i++){ int u=a[i]; for(int j=1;j<=21;j++)tmp[j]=u%2,u/=2; ans+=query(rt,21); ans--; } return ans/2; } int main() { freopen("xorxor.in","r",stdin); freopen("xorxor.out","w",stdout); scanf("%d%d",&n,&k); rt=siz=1; for(int i=1;i<=n;i++)scanf("%d",&a[i]),insert(a[i]); int l=0,r=1<<20; while(l!=r){ int mid=(l+r)>>1; if(check(mid)>=k)r=mid; else l=mid+1; } printf("%d\n",l); return 0; }