题目描述
给定一个非负整数序列{a},初始长度为N。
有M个操作,有以下两种操作类型:
A x
:添加操作,表示在序列末尾添加一个数(x),序列的长度(N+1)。Q l r x
:询问操作,你需要找到一个位置(p),满足(l le p le r),使得: (a[p] oplus a[p+1] oplus ... oplus a[N] oplus 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
Sample Output
4
5
6
solution
题目问的是后缀和,但是由于新加点是在后面加,这个不好维护。
关于异或有一个很显然的性质:
[aoplus boplus a=b
]
所以我们可以转化成求前缀和:
[a[p]oplus a[p+1]oplus cdots oplus a[n] oplus x=s[n]oplus n oplus s[p-1]
]
其中(s)为前缀和。
所以加点显然可以拿一个可持久化(trie)树来维护,插入和询问都和主席树差不多,很好写。
#include<bits/stdc++.h>
using namespace std;
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('
');}
const int maxn = 3e5+10;
int s,n,m,rt[maxn*10],son[maxn*100][2],sum[maxn*100],tot;
struct _trie {
void insert(int pre,int &p,int x) {
p=++tot;int now=p;sum[p]=sum[pre]+1;
for(int i=0;i<=24;i++) {
int t=x>>(24-i)&1;
son[now][t]=++tot;son[now][t^1]=son[pre][t^1];
now=son[now][t],pre=son[pre][t];
sum[now]=sum[pre]+1;
}
}
int query(int pre,int p,int x) {
int ans=0;
for(int i=0;i<=24;i++) {
int t=x>>(24-i)&1;
if(sum[son[p][t^1]]-sum[son[pre][t^1]]>0) p=son[p][t^1],pre=son[pre][t^1],ans=ans<<1|1;
else p=son[p][t],pre=son[pre][t],ans=ans<<1;
}
return ans;
}
}trie;
int main() {
read(n),read(m);
n++;trie.insert(rt[0],rt[1],0);
for(int x,i=2;i<=n;i++) {
read(x),s^=x;
trie.insert(rt[i-1],rt[i],s);
}
for(int i=1;i<=m;i++) {
char S[3];int x,y,z;scanf("%s",S+1);read(x);
if(S[1]=='A') n++,s^=x,trie.insert(rt[n-1],rt[n],s);
else read(y),read(z),write(trie.query(rt[x-1],rt[y],s^z));
}
return 0;
}