BZOJ 3689: 异或之 可持久化trie+堆
和超级钢琴几乎是同一道题吧...
code:
#include <bits/stdc++.h> #define N 200006 #define ll long long #define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout) using namespace std; char buf[100000],*p1,*p2; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() { int x=0; char s=nc(); while(s<'0') s=nc(); while(s>='0') x=(((x<<2)+x)<<1)+s-'0',s=nc(); return x; } namespace trie { int tot; int cnt[N*30],ch[N*30][2]; int newnode() { return ++tot; } void Insert(int pre,int &x,int v) { int now=x=newnode(),i; for(i=30;i>=0;--i) { int o=((v>>i)&1); ch[now][o^1]=ch[pre][o^1]; ch[now][o]=newnode(); pre=ch[pre][o]; now=ch[now][o]; cnt[now]=cnt[pre]+1; } } int query(int x,int y,int z) { int re=0,i; for(i=30;i>=0;--i) { int o=((z>>i)&1); if(ch[x][o]<ch[y][o]) x=ch[x][o],y=ch[y][o]; else re+=(1<<i),x=ch[x][o^1],y=ch[y][o^1]; } return re; } }; struct node { int o,l,r,val,pos; node(int o=0,int l=0,int r=0,int val=0,int pos=0):o(o),l(l),r(r),val(val),pos(pos){} bool operator<(node b) const { return b.val<val; } }; priority_queue<node>q; int A[N],rt[N],ar[N],id[N]; set<int>S[N]; set<int>::iterator it; int main() { // setIO("input"); int i,j,n,k,ou=0; n=rd(),k=rd(); for(i=1;i<=n;++i) { A[i]=rd(); id[i]=ar[i]=A[i]; trie::Insert(rt[i-1],rt[i],A[i]); } sort(ar+1,ar+1+n); for(i=1;i<=n;++i) id[i]=lower_bound(ar+1,ar+1+n,id[i])-ar; for(i=1;i<=n;++i) S[id[i]].insert(i); for(i=1;i<n;++i) { int l=i+1,r=n; int tmp=trie::query(rt[l-1],rt[r],A[i]); int idx=lower_bound(ar+1,ar+1+n,A[i]^tmp)-ar; int pos=*S[idx].lower_bound(l); q.push(node(i,l,r,tmp,pos)); } while(ou<k) { node e=q.top(); q.pop(); printf("%d ",e.val),++ou; int pos=e.pos; if(pos!=e.l) { int tmp=trie::query(rt[e.l-1],rt[pos-1],A[e.o]); int idx=lower_bound(ar+1,ar+1+n,A[e.o]^tmp)-ar; int t=*S[idx].lower_bound(e.l); q.push(node(e.o,e.l,pos-1,tmp,t)); } if(pos!=e.r) { int tmp=trie::query(rt[pos],rt[e.r],A[e.o]); int idx=lower_bound(ar+1,ar+1+n,A[e.o]^tmp)-ar; int t=*S[idx].lower_bound(pos+1); q.push(node(e.o,pos+1,e.r,tmp,t)); } } return 0; }