題目鏈接
首先記\(sum\)為前綴異或和,那么區間\(s[l,r]=sum[l-1]^{\wedge}sum[r]\)。即一個區間異或和可以轉為求兩個數的異或和。
那么對\([l,r]\)的詢問即求\([l-1,r]\)中某兩個數異或的最大值。
區間中某一個數和已知的一個數異或的最大值可以用可持久化Trie \(O(\log v)\)求出。所以盡量確定一個數,再在區間中求最大值。
而且數據范圍提醒我們可以分塊。
用\(head[i]\)表示第\(i\)塊的開頭位置,\(Max(l,r,x)\)表示\(x\)與\([l,r]\)中某一個數異或的最大值,\(f[i][j]\)表示從第\(i\)塊的開始到位置\(j\),某兩個數異或的最大值是多少。
那么 \(f[i][j] = \max(f[i-1][j-1], Max(head[i], j-1, A[j]))\)。可以在\(O(n\sqrt n\log v)\)時間內預處理。(\(A[]\)是前綴異或和)
查詢的時候,設\(x\)表示\(l\)后面的第一塊,若\(l,r\)在同一塊里,則 \(ans = Max(l, r, A[i]), i\in[l,r]\)。(對啊 和自己異或也沒什么意義)
否則 \(ans = \max(f[x][r], Max(l, r, A[i]))\),\(i\in[l,begin[x]-1]\)。
對\([1,r]\)的詢問,可能會有同上一題一樣的邊界問題(可以異或0)?把\(A[0]=0\)也試一遍就行了。。
詢問復雜度同樣\(O(q\sqrt n\log v)\)。
//11020kb 8232ms
#include <cmath>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define MAXIN 500000//為什么50000WA+TLE啊 QAQ
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define BIT 30
typedef long long LL;
const int N=12005,M=111;int root[N],A[N],bel[N],H[N],f[M][N];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Trie
{#define S N*32int tot,son[S][2],sz[S];void Insert(int x,int y,int v){for(int i=BIT; ~i; --i){int c=v>>i&1;son[x][c]=++tot, son[x][c^1]=son[y][c^1];x=tot, y=son[y][c];sz[x]=sz[y]+1;}}int Query(int x,int y,int v){int res=0;for(int i=BIT; ~i; --i){int c=(v>>i&1)^1;if(sz[son[y][c]]-sz[son[x][c]]>0)x=son[x][c], y=son[y][c], res|=1<<i;elsec^=1, x=son[x][c], y=son[y][c];}return res;}
}T;inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}int main()
{int n=read(),Q=read(),size=sqrt(n);for(int i=1; i<=n; ++i)bel[i]=(i-1)/size+1, T.Insert(root[i]=++T.tot,root[i-1],A[i]=A[i-1]^read());//^不是+ == H[1]=1;for(int i=2,lim=bel[n]; i<=lim; ++i) H[i]=H[i-1]+size;for(int i=1,lim=bel[n]; i<=lim; ++i)for(int j=H[i]+1,rtl=root[H[i]-1]; j<=n; ++j)f[i][j]=std::max(f[i][j-1],T.Query(rtl,root[j-1],A[j]));for(int l,r,x,y,ans=0; Q--; ){x=((LL)read()+ans)%n+1, y=((LL)read()+ans)%n+1;//read()%n+ans%n 都可能爆int。。and LL要在括號里面。。l=std::min(x,y), r=std::max(x,y);--l, ans=0;if(bel[l]==bel[r])for(int i=l,rtl=root[std::max(0,l-1)],rtr=root[r]; i<=r; ++i)ans=std::max(ans,T.Query(rtl,rtr,A[i]));else{ans=f[bel[l]+1][r];for(int i=l,lim=H[bel[l]+1]-1,rtl=root[std::max(0,l-1)],rtr=root[r]; i<=lim; ++i)ans=std::max(ans,T.Query(rtl,rtr,A[i]));}printf("%d\n",ans);}return 0;
}