题面:51nod1295
第一次yy出了可持久化Trie哈哈哈……
这题xor其实就是老套路了,其实就是在Trie树上跑一下就好了
我们按照从高位到低位的顺序找,如果这一个二进制位有不同的,那么就往这边走,xor值加上
2i
即可
这题的关键就是区间的问题了,所以就是一个可持久化Trie就可以了
具体是怎么yy出来的?按照主席树的写法就可以了
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
using namespace std;
inline int read(){
int k=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
return k*f;
}
int nex[3000010][2],t[3000010],root[300010],cnt=0;
inline void build(int i,int l,int x){
int c[40]={0},p=0,nowi=root[i],nowl=root[l];
while(x)c[++p]=x&1,x>>=1;
for(int i=32;i;i--){
if(c[i]){
nex[nowi][0]=nex[nowl][0];
nex[nowi][1]=++cnt;
nowi=cnt;nowl=nex[nowl][1];
}else{
nex[nowi][1]=nex[nowl][1];
nex[nowi][0]=++cnt;
nowi=cnt;nowl=nex[nowl][0];
}
t[nowi]=t[nowl]+1;
}
}
inline int smax(int l,int r,int x){
int c[40]={0},p=0,nowr=root[r],nowl=root[l];
while(x)c[++p]=x&1,x>>=1;
int ans=0;
for(int i=32;i;i--){
if(c[i]){
if(t[nex[nowr][0]]-t[nex[nowl][0]]>0){ans+=1<<(i-1);nowl=nex[nowl][0];nowr=nex[nowr][0];}
else{nowl=nex[nowl][1];nowr=nex[nowr][1];}
}else{
if(t[nex[nowr][1]]-t[nex[nowl][1]]>0){ans+=1<<(i-1);nowl=nex[nowl][1];nowr=nex[nowr][1];}
else{nowl=nex[nowl][0];nowr=nex[nowr][0];}
}
}
return ans;
}
int main()
{
int n=read(),m=read();
for(int i=1;i<=n;i++){
root[i]=++cnt;int x=read();
build(i,i-1,x);
}
for(int i=1;i<=m;i++){
int x=read(),l=read()+1,r=read()+1;
printf("%d\n",smax(l-1,r,x));
}
return 0;
}