您的位置:首页 > 其它

bzoj3261 最大异或和(可持久化字典树)

2016-09-08 19:47 316 查看
题目链接:点这里!!

题意:

给定一个非负整数序列 {a},初始长度为 N。       

有M个操作,有以下两种操作类型:

1 、A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1。

2 、Q l r x:询问操作,你需要找到一个位置 p,满足 l<=p<=r,使得:

a[p] xor a[p+1] xor ... xor a
xor x 最大,输出最大是多少。

(N,M<=300000)

题解:

可持久化字典树第一题,基础的可持久化字典树。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
typedef long long LL;
const LL MOD = 1000000007;
const int N = 6e5+15;
const int maxn = 1e6+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,q,a
;
int bin[30],ch[N*25][2],sum[N*25],root
,cnt;
int trie_insert(int x,int val){
int tmp,y;
tmp=y=++cnt;
for(int i=23;i>=0;i--){
ch[y][0]=ch[x][0],ch[y][1]=ch[x][1];
sum[y]=sum[x]+1;
int t=val&bin[i];
t>>=i;
x=ch[x][t];
ch[y][t]=++cnt;
y=ch[y][t];
}
sum[y]=sum[x]+1;
return tmp;
}
int trie_query(int l,int r,int val){
int tmp=0;
for(int i=23;i>=0;i--){
int t=val&bin[i];
t>>=i;
if(sum[ch[r][t^1]]-sum[ch[l][t^1]]>0)
tmp+=bin[i],r=ch[r][t^1],l=ch[l][t^1];
else r=ch[r][t],l=ch[l][t];
}
return tmp;
}
int main(){
bin[0]=1;
for(int i=1;i<30;i++) bin[i]=bin[i-1]<<1;
scanf("%d%d",&n,&q);
n++;
for(int i=2;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<=n;i++) a[i]^=a[i-1];
for(int i=1;i<=n;i++)
root[i]=trie_insert(root[i-1],a[i]);
char s[5];
int l,r,x;
while(q--){
scanf("%s",s);
if(s[0]=='A'){
scanf("%d",&a[++n]),a
^=a[n-1];
root
=trie_insert(root[n-1],a
);
}
else {
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",trie_query(root[l-1],root[r],a
^x));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  可持久化字典树