bzoj 3261: 最大异或和

3261: 最大异或和

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 2637  Solved: 1078
[Submit][Status][Discuss]

Description

给定一个非负整数序列{a},初始长度为N。
有M个操作,有以下两种操作类型:
1、Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1。
2、Qlrx:询问操作,你需要找到一个位置p,满足l<=p<=r,使得:
a[p] xor a[p+1] xor ... xor a[N] xor x 最大,输出最大是多少。

Input

第一行包含两个整数 N  ,M,含义如问题描述所示。   
第二行包含 N个非负整数,表示初始的序列 A 。 
接下来 M行,每行描述一个操作,格式如题面所述。   

Output

假设询问操作有 T个,则输出应该有 T行,每行一个整数表示询问的答案。

Sample Input

5 5
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6
对于测试点 1-2,N,M<=5 。
对于测试点 3-7,N,M<=80000 。
对于测试点 8-10,N,M<=300000 。
其中测试点 1, 3, 5, 7, 9保证没有修改操作。
0<=a[i]<=10^7。

Sample Output

4
5
6
 
    后缀查询改成 总的 异或 前缀,然后写一个可持久化trie就好啦。
 
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=300005;
struct node{
	int Sum;
	node *ch[2];
}nil[maxn*73],*rot[maxn*4],*cnt;
int now,Xor,N,L,R,X,M,ci[33],ans;
char C;

node *update(node *u,int x){
	node *ret=++cnt;
	*ret=*u,ret->Sum++;
	if(x<0) return ret;
	
	if(Xor&ci[x]) ret->ch[1]=update(ret->ch[1],x-1);
	else ret->ch[0]=update(ret->ch[0],x-1);
	
	return ret;
}

void query(node *l,node *r,int x){
	if(x<0) return;
	int u=(ci[x]&X)?0:1;
	if(r->ch[u]->Sum-l->ch[u]->Sum) ans+=ci[x],query(l->ch[u],r->ch[u],x-1);
	else query(l->ch[u^1],r->ch[u^1],x-1);
}

inline void solve(){
	while(M--){
		C=getchar();
		while(C!='A'&&C!='Q') C=getchar();
		if(C=='A') N++,scanf("%d",&now),Xor^=now,rot[N]=update(rot[N-1],25);
		else{
			scanf("%d%d%d",&L,&R,&X);
			X^=Xor,ans=0;
			query(rot[L-1],rot[R],25);
			printf("%d
",ans);
		}
	}
}

int main(){
	ci[0]=1;
	for(int i=1;i<=25;i++) ci[i]=ci[i-1]<<1;
	
	scanf("%d%d",&N,&M);
	nil->Sum=0,N++;
	nil->ch[0]=nil->ch[1]=cnt=rot[0]=nil;
	rot[1]=update(rot[0],25);
	
	for(int i=2;i<=N;i++){
		scanf("%d",&now),Xor^=now;
		rot[i]=update(rot[i-1],25);
	}
	
	solve();
	
	return 0;
}

  

原文地址:https://www.cnblogs.com/JYYHH/p/8961328.html