BZOJ3261 最大异或和

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

计$B_i$为$A_i$异或前缀和

$\max_{l\leq p\leq r}B_{p-1}\oplus B_N\oplus x$

维护一个可持久化trie

因为是$p-1$所以要查询$(l-2,r-1)$

l-2可能小于0

可以在数列最前面加一个0

然后查询$(l-1,r)$

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int c[20000001],ch[20000001][2],sum,n,root[1200001],m,pw[51],pos;
 8 char s[21];
 9 void insert(int &rt,int x,int h)
10 {
11   int o=rt;
12   rt=++pos;
13   ch[rt][0]=ch[o][0];
14   ch[rt][1]=ch[o][1];
15   c[rt]=c[o]+1;
16   if (h<0)
17     {
18       return;
19     }
20   int t=(x>>h)&1;
21   insert(ch[rt][t],x,h-1);
22 }
23 int query(int rt1,int rt2,int x,int h)
24 {
25   if (h<0)
26     {
27       return 0;
28     }
29   int t=!((x>>h)&1);
30   if (c[ch[rt1][t]]-c[ch[rt2][t]]>0) return pw[h]|query(ch[rt1][t],ch[rt2][t],x,h-1);
31   return query(ch[rt1][!t],ch[rt2][!t],x,h-1);
32 }
33 int main()
34 {int i,x,l,r;
35   pw[0]=1;
36   for (i=1;i<=25;i++)
37     pw[i]=pw[i-1]*2;
38   cin>>n>>m;
39   sum=0;
40   insert(root[1],0,25);
41   n++;
42   for (i=2;i<=n;i++)
43     {
44       scanf("%d",&x);
45       sum^=x;
46       root[i]=root[i-1];
47       insert(root[i],sum,25);
48     }
49   for (i=1;i<=m;i++)
50     {
51       scanf("%s",s);
52       if (s[0]=='A')
53       {
54         scanf("%d",&x);
55         sum^=x;n++;
56         root[n]=root[n-1];
57         insert(root[n],sum,25);
58       }
59       else
60       {
61         scanf("%d%d%d",&l,&r,&x);
62         x=sum^x;
63         printf("%d\n",query(root[r],root[l-1],x,25));
64       }
65     }
66 }

 

posted @ 2018-03-17 15:51  Z-Y-Y-S  阅读(225)  评论(0编辑  收藏  举报