3261: 最大异或和
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1954 Solved: 810
[ Submit][ Status][ Discuss]
Description
给定一个非负整数序列 {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[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保证没有修改操作。
对于 100% 的数据, 0<=a[i]<=10^7。
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保证没有修改操作。
对于 100% 的数据, 0<=a[i]<=10^7。
Sample Output
4
5
6
5
6
HINT
对于 100% 的数据, 0<=a[i]<=10^7 。
Source
这道题还比2741水一点,不用分块,在序列后面加点操作简直鸡肋,直接insert即可.
详情参考:http://blog.csdn.net/maxmercer/article/details/76163855
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=600005;
const int maxbit=23;
inline const int read(){
register int f=1,x=0;
register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return f*x;
}
char ss[5];
int a[maxn],c[maxn*24][2],sum[maxn*24],tr[maxn],b[maxn],id,Whereever_I_Go,_wanna,ed,n,m;
struct Trie{
void insert(int &rt,int pre,int who,int pos){
rt=++id;
c[rt][0]=c[pre][0];c[rt][1]=c[pre][1];
sum[rt]=sum[pre]+1;
if(pos==-1) return;
Whereever_I_Go=(who>>pos)&1;
insert(c[rt][Whereever_I_Go],c[pre][Whereever_I_Go],who,pos-1);
}
inline int query(int l,int r,int who){
ed=0;
for(int i=maxbit;i>=0;i--){
_wanna=((who>>i)&1)^1;
int p=c[l][_wanna],q=c[r][_wanna];
if(sum[q]>sum[p]) ed|=(1<<i),l=p,r=q;
else l=c[l][_wanna^1],r=c[r][_wanna^1];
}
return ed;
}
}trie;
int main(){
n=read(),m=read();n++;
for(register int i=2;i<=n;i++) a[i]=read(),b[i]=b[i-1]^a[i];
for(register int i=1;i<=n;i++) trie.insert(tr[i],tr[i-1],b[i],maxbit);
while(m--){
scanf("%s",ss);
if(ss[0]=='A'){
n++;a[n]=read();b[n]=b[n-1]^a[n];
trie.insert(tr[n],tr[n-1],b[n],maxbit);
}
else{
int l=read(),r=read(),x=read();
printf("%d\n",trie.query(tr[l-1],tr[r],b[n]^x));
}
}
}