Descrpiton
There is an integer sequence a of length n and there are two kinds of operations:
0 l r: select some numbers from al…ar
so that their xor sum is maximum, and print the maximum value.
1 x: append x
to the end of the sequence and let n=n+1
.
Input
There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
For each test case:
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,…,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It’s guaranteed that ∑n≤106,∑m≤106,0≤x<230
.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero:
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.
Output
For each type 0 operation, please output the maximum xor sum in a single line.
Sample Input
1
3 3
0 1 2
0 1 1
1 3
0 3 4
Sample Output
1
3
//类似于CF1100F,多了一点预处理
#include <bits/stdc++.h>
using namespace std;
class LinearBase{
static const int NAXLEN=5e5+5;
static const int MAXBITS=32;
int n; // count from 1
int base[NAXLEN][MAXBITS];
int pos[NAXLEN][MAXBITS];
public:
void insert(int x){
int cur;
n++;
cur=n;
disp();
memcpy(base[n],base[n-1],sizeof(base[0]));
memcpy(pos[n],pos[n-1],sizeof(pos[0]));
for(int i=MAXBITS-1;i>=0;i--){
if(!((x>>i)&1)){
continue;
}
if(!base[n][i]){
base[n][i]=x;
pos[n][i]=cur;
break;
}
else{
if(cur>pos[n][i]){
swap(base[n][i],x);
swap(pos[n][i],cur);
}
x^=base[n][i];
}
}
}
int query(int l,int r){
int ret=0;
for(int i=MAXBITS-1;i>=0;i--){
if(pos[r][i]>=l){
ret=max(ret,ret^base[r][i]);
}
}
return ret;
}
void init(){
n=0;
memset(base[0],0,sizeof(base[0]));
memset(pos[0],0,sizeof(pos[0]));
}
int getN(){
return n;
}
void disp(){
/*cout<<"disp:";
for(int i=0;i<=n;i++){
cout<<base[n][i]<<" ";
}
cout<<"\n";*/
}
};
LinearBase lb;
int main(){
ios::sync_with_stdio(0);
int lastans=0;
int T;
cin>>T;
while(T--){
lastans=0; //就因为它!!!
lb.init();
int n,m,arg_type;
cin>>n>>m;
for(int i=0;i<n;i++){
int t;
cin>>t;
lb.insert(t);
lb.disp();
}
while(m--){
cin>>arg_type;
if(arg_type==0){
int l,r;
cin>>l>>r;
l=(l^lastans)%n+1;
r=(r^lastans)%n+1;
if(l>r) swap(l,r);
lastans=lb.query(l,r);
cout<<lastans<<"\n";
}
else {
int x;
cin>>x;
lb.insert(x^lastans);
n++;//坑!
}
lb.disp();
}
}
return 0;
}