BZOJ4260: Codechef REBXOR (01Tire树)

题意

题目链接

Sol

首先维护出前缀xor和后缀xor

对每个位置的元素插入到Trie树里面,每次找到和该前缀xor起来最大的元素

正反各做一遍,取最大。

记得要开log倍空间qwq。。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 10;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-')f =- 1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, a[MAXN], s[MAXN], pmx[MAXN], smx[MAXN], ch[MAXN][2], tot;
void insert(int x) {
	int now = 0;
	for(int i = 0; i <= 30; i++) {
		int nxt = (x >> i & 1);
		if(!ch[now][nxt]) ch[now][nxt] = ++tot;
		now = ch[now][nxt];
	}
}
int Query(int x) {
	int now = 0, ans = 0;
	for(int i = 0; i <= 30; i++) {
		int nxt = (x >> i & 1);
		if(ch[now][nxt ^ 1]) ans += 1 << i, now = ch[now][nxt ^ 1];
		else now = ch[now][nxt];
	}
	return ans;
}
void solve(int *s, int *mx) {
	for(int i = 1; i <= N; i++) {
		s[i] = s[i - 1] ^ a[i];
		insert(s[i]);
		mx[i] = Query(s[i]);
		mx[i] = max(mx[i - 1], mx[i]);
	}
}
int main() {
	//freopen("3.in", "r", stdin); 
	N = read();
	for(int i = 1; i <= N; i++) a[i] = read();
	solve(s, pmx);	
	reverse(a + 1, a + N + 1);
	solve(s, smx);
	reverse(smx + 1, smx + N + 1);
	int ans = 0;
	for(int i = 1; i <= N; i++) ans = max(ans, pmx[i] + smx[i + 1]);
	cout << ans;
}
posted @ 2018-09-30 19:28  自为风月马前卒  阅读(389)  评论(0编辑  收藏  举报

Contact with me