[BZOJ3261]最大异或和(可持久化Trie)

3261: 最大异或和

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 2568  Solved: 1056
[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

HINT

Source

[Submit][Status][Discuss]

可持久化Trie模板题,建树细节要想清楚,注意要留一个b[0]=0就好。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=l; i<=r; i++)
 4 using namespace std;
 5 
 6 const int N=600100,M=300000*2*25;
 7 char ch;
 8 int n,m,nd,x,l,r,b[N],a[N],rt[N],c[M][2],sm[M];
 9 
10 void ins(int y,int &x,int k){
11     int tmp=++nd; x=nd;
12     for (int i=23; ~i; i--){
13         c[x][0]=c[y][0]; c[x][1]=c[y][1]; sm[x]=sm[y]+1;
14         int t=(k>>i)&1; c[x][t]=++nd; x=c[x][t]; y=c[y][t];
15     }
16     sm[x]=sm[y]+1; x=tmp;
17 }
18 
19 int que(int l,int r,int k){
20     int res=0;
21     for (int i=23; ~i; i--){
22         int t=(k>>i)&1;
23         if (sm[c[r][t^1]]-sm[c[l][t^1]]) res+=1<<i,r=c[r][t^1],l=c[l][t^1];
24             else r=c[r][t],l=c[l][t];
25     }
26     return res;
27 }
28 
29 int main(){
30     freopen("bzoj3261.in","r",stdin);
31     freopen("bzoj3261.out","w",stdout);
32     scanf("%d%d",&n,&m); n++; ins(rt[0],rt[1],0);
33     rep(i,2,n) scanf("%d",&a[i]),b[i]=b[i-1]^a[i],ins(rt[i-1],rt[i],b[i]);
34     rep(i,1,m){
35         scanf(" %c",&ch);
36         if (ch=='A') scanf("%d",&x),n++,b[n]=b[n-1]^x,ins(rt[n-1],rt[n],b[n]);
37                     else scanf("%d%d%d",&l,&r,&x),printf("%d\n",que(rt[l-1],rt[r],b[n]^x));
38     }
39     return 0;
40 }

 

posted @ 2018-04-11 17:55  HocRiser  阅读(175)  评论(0编辑  收藏  举报