1. 程式人生 > >BZOJ 3689: 異或之

BZOJ 3689: 異或之

這題還不錯? 我好菜啊
po姐說的很清楚啊QAQ 直接去看就好了
程式碼打著打著就發現打得好短(躺

不過似乎不算很快 沒怎麼看黃學長那種做法(好像很快

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+2,inf=(1ll<<31)-1;
char O[1<<14],*S=O,*T=O;
#define gc (S==T&&(T=(S=O)+fread(O,1,1<<14,stdin),S==T)?-1:*S++)
inline int read(){
    int
x=0,f=1; char ch=gc; while(ch<'0' || ch>'9'){if(ch=='-')f=-1; if(ch<0)return 0; ch=gc;} while(ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=gc;} return x*f; } int t[N*30][2],s[N*30],tot=0; int a[N],p[N],n,b[31]; void ins(int x){ int u=0; for(int i=30;~i;--i){ int
y=(x&b[i])>0; if(!t[u][y])t[u][y]=++tot; u=t[u][y],++s[u]; } } int get(int x,int k){ if(k>n)return inf; int u=0,w=0; for(int i=30;~i;--i){ int y=(x&b[i])>0,o=s[ t[u][y] ]; if(o>=k) u=t[u][y]; else k-=o,u=t[u][y^1],w+=b[i]; } return
w; } struct P{int u,x;}; bool operator <(const P &x,const P &y){ return x.u>y.u; } priority_queue<P>q; int main(){ n=read(); int m=read()<<1,i; b[0]=1; for(i=1;i<31;++i)b[i]=b[i-1]<<1; for(i=1;i<=n;++i) a[i]=read(),ins(a[i]),p[i]=2; for(i=1;i<=n;++i) q.push( (P){get(a[i],2),i} ); while(m--){ P o=q.top(); q.pop(); if(m&1)printf("%d ",o.u); o.u=get(a[o.x],++p[o.x]); q.push(o); } return 0; }