分成sqrtn块r然后,简历可持久化字典数,然后查询即可。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=32005;
int root[maxn];
int cnt;
LL fa[80];
namespace Trie{
int sum[maxn*65];
int ch[maxn*65][2];
void init(){
memset(ch,0,sizeof(ch));
memset(sum,0,sizeof(sum));
}
void insert(int pre,int no,LL val){
for(int i=63;i>=0;i--){
ch[no][0]=ch[pre][0];
ch[no][1]=ch[pre][1];
sum[no]=sum[pre]+1;
int w=(val>>i)&1;
ch[no][w]=++cnt;
no=ch[no][w];
pre=ch[pre][w];
}
sum[no]=sum[pre]+1;
}
LL query(int pre,int no,LL val){
LL s=0;
for(int i=63;i>=0;i--){
int w=(val>>i)&1;
if(sum[ch[no][w^1]]-sum[ch[pre][w^1]]>0){
s+=fa[i];
w=w^1;
}
no=ch[no][w];
pre=ch[pre][w];
}
return s;
}
}
LL dp[300][300];
LL a[maxn];
int belong[maxn];
int l[300],r[303];
int main()
{
for(int i=0;i<=63;i++) fa[i]=(LL)1<<i;
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
a[i]=a[i-1]^a[i];
root[i]=++cnt;
Trie::insert(root[i-1],root[i],a[i]);
}
int tot=0;
int blo=sqrt(n);
for(int i=1;i<=n;i++){
++tot;
int p=i;
l[tot]=i;
while(p<=n&&p-i<=blo){
belong[p]=tot;
p++;
}
r[tot]=p-1;
i=p-1;
}
for(int i=tot;i>=1;i--){
for(int j=i;j<=tot;j++){
if(j==i){
for(int f=l[i];f<=r[i];f++){
dp[i][j]=max(dp[i][j],Trie::query(root[f-1],root[r[j]],a[f-1]));
}
}
else{
dp[i][j]=dp[i+1][j];
for(int f=l[i];f<=r[i];f++){
dp[i][j]=max(dp[i][j],Trie::query(root[f-1],root[r[j]],a[f-1]));
}
}
}
}
LL ans=0;
while (m--) {
LL x,y;
scanf("%lld%lld",&x,&y);
int l1,r1;
l1=min(((x+ans)%n)+1,((y+ans)%n)+1);
r1=max(((x+ans)%n)+1,((y+ans)%n)+1);
ans=0;
if(belong[l1]+1>=belong[r1]){
for(int i=l1;i<=r1;i++){
ans=max(ans,Trie::query(root[i-1],root[r1],a[i-1]));
}
}
else{
ans=dp[belong[l1]+1][belong[r1]-1];
for(int i=l1;i<=r[belong[l1]];i++){
ans=max(ans,Trie::query(root[i-1],root[r1],a[i-1]));
}
for(int i=l[belong[r1]];i<=r1;i++){
ans=max(ans,Trie::query(root[l1-1],root[i],a[i]));
}
}
printf("%lld\n",ans);
}
}