There is an integer sequence aa of length nn and there are two kinds of operations:
- 0 l r: select some numbers from al...aral...ar so that their xor sum is maximum, and print the maximum value.
- 1 x: append xx to the end of the sequence and let n=n+1n=n+1.
Input
There are multiple test cases. The first line of input contains an integer T(T≤10)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)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 nn integers a1,a2,...,an(0≤ai<230)a1,a2,...,an(0≤ai<230), denoting the initial sequence.
Each of the next mm lines contains one of the operations given above.
It's guaranteed that ∑n≤106,∑m≤106,0≤x<230∑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
一道不错的线性基的题:难点在于O1的查询。
题意:给n个数,m个操作,操作0是求l,r区间任意数组合求异或最大值,操作1是在数列最后再加一个数。
思路:想到线性基是显而易见的,对于每个线性基,将出现位置靠右的数字尽可能地放在高位,也就是说在插入新数字的时候,要同时记录对应位置上数字的出现位置,并且在找到可以插入的位置的时候,如果新数字比位置上原来的数字更靠右,就将该位置上原来的数字向低位推。
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<map>
using namespace std;
#define ll long long
#define maxn 1000006
ll p[maxn][31],pos[maxn][31],a[maxn];
ll t,n,m,x,y;
void add(ll x,ll d){
ll q=d;
for (ll i = 0; i <= 29;i++){
p[d][i]=p[d-1][i];
pos[d][i]=pos[d-1][i];
}
for (ll i = 29; i >= 0;i--){
if(x&(1<<i)){
if(!p[d][i]){
p[d][i]=x;
pos[d][i]=q;
return;
}
if(pos[d][i]<q){
swap(p[d][i],x);
swap(pos[d][i],q);
}
x^=p[d][i];
}
}
}
ll query(ll l,ll r){
ll maxx=0;
for (ll i = 29; i >= 0;i--){
if(pos[r][i]>=l&&(maxx^p[r][i])>maxx){
maxx^=p[r][i];
}
}
return maxx;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
ll la=0,l,r;
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]),add(a[i],i);
while(m--)
{
scanf("%lld",&x);
if(x==0)
{
scanf("%lld%lld",&l,&r);
l=(l^la)%n+1;
r=(r^la)%n+1;
if(l>r) swap(l,r);
la=query(l,r);
printf("%lld\n",la);
}
else
{
scanf("%lld",&y);
a[++n]=(y^la);
add(a[n],n);
}
}
}
return 0;
}
主要是添加和查询操作,自己也没看太懂,打算整理一下,详细可以看连接:传送门。