BZOJ 3166: [Heoi2013]Alo 链表+可持久化trie
链表这个东西非常好用啊 ~
code:
#include <bits/stdc++.h> #define N 50010 #define inf 2000400000 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n,ans,cnt; int a[N],t[N*35],ch[N*35][2],pre[N],nxt[N],rt[N]; pair<int,int>b[N]; void insert(int x,int pos) { int now=rt[pos]=++cnt, last=rt[pos-1]; for(int i=30;i>=0;--i) { int o=(x>>i)&1; ch[now][o^1]=ch[last][o^1]; ch[now][o]=++cnt; t[ch[now][o]]=t[ch[last][o]]+1; now=ch[now][o]; last=ch[last][o]; } } int query(int sum,int l,int r) { --l; int now=rt[l],last=rt[r],ret=0; for(int i=30;i>=0;--i) { int o=(sum>>i)&1; if(t[ch[last][o^1]]>t[ch[now][o^1]]) { ret+=(1<<i); now=ch[now][o^1]; last=ch[last][o^1]; } else { now=ch[now][o]; last=ch[last][o]; } } return ret; } int main() { // setIO("input"); int i,j,n; scanf("%d",&n); int fir=0,last=n+1; a[fir]=a[last]=inf; for(i=1;i<=n;++i) { pre[i]=i-1, nxt[i]=i+1; scanf("%d",&a[i]), b[i]=make_pair(a[i],i), insert(a[i],i); } sort(b+1,b+1+n); for(i=1;i<=n;++i) { int x=b[i].second; int l=pre[x],r=nxt[x]; nxt[l]=r,pre[r]=l; if(l!=fir) ans=max(ans, query(a[x],pre[l]+1,r-1)); if(r!=last) ans=max(ans, query(a[x],l+1,nxt[r]-1)); } printf("%d\n",ans); return 0; }