BZOJ 4260: Codechef REBXOR

trie树

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,cnt,A[400005],F[400005],ch[15000005][2],Suml[400005],Sumr[400005];
void insert(int x){
	int now=0;
	for (int i=30; i>=0; i--){
		int X=(x&(1<<i))>>i;
		if (!ch[now][X]) ch[now][X]=++cnt;
		now=ch[now][X];
	}
}
void solve(){
	cnt=0;
	memset(ch,0,sizeof(ch));
	memset(F,0,sizeof(F));
	insert(0);
	for (int i=1; i<=n; i++){
		insert(A[i]);
		int now=0;
		for (int j=30; j>=0; j--){
			int x=(A[i]&(1<<j))>>j;
			if (ch[now][x^1]){
				now=ch[now][x^1];
				F[i]+=(1<<j);
			}
			else now=ch[now][x];
		}
	}
}
int main(){
	scanf("%d",&n);
	for (int i=1; i<=n; i++) scanf("%d",&A[i]);
	for (int i=1; i<=n; i++) A[i]^=A[i-1];
	solve();
	for (int i=1; i<=n; i++) Suml[i]=F[i];
	for (int i=1; i<=n/2; i++) swap(A[i],A[n-i+1]);
	solve();
	for (int i=1; i<=n; i++) Sumr[i]=F[n-i+1];
	for (int i=1; i<=n; i++) Suml[i]=max(Suml[i],Suml[i-1]);
	for (int i=n; i>=1; i--) Sumr[i]=max(Sumr[i],Sumr[i+1]);
	int ans=0;
	for (int i=1; i<=n; i++) ans=max(ans,Suml[i]+Sumr[i]);
	printf("%d\n",ans);
	return 0;
}

  

posted @ 2018-10-28 15:43  ~Silent  阅读(152)  评论(0编辑  收藏  举报
Live2D