http://acm.hdu.edu.cn/showproblem.php?pid=6579
OperationTime Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2840 Accepted Submission(s): 867 Problem Description There is an integer sequence a of length n and there are two kinds of operations:
Input There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
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
|
//cnt_val[i][j]保存的是[1,i]区间第j位线性基的值
//cnt_pos[i][j]保存的是[1,i]区间第j位线性基的位置
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int a[maxn],d[32],pos[32];
int cnt_pos[maxn][32],cnt_val[maxn][32];
void Insert(int x,int id)
{
for(int i=30;i>=0;i--)
{
if(x&(1<<i))
{
if(!d[i])
{
d[i]=x;
pos[i]=id;
break;
}
if(pos[i]<id)
{
swap(pos[i],id);
swap(d[i],x);
}
x^=d[i];
}
}
}
int query(int l,int r)
{
int ret=0;
for(int i=30;i>=0;i--)
{
if(cnt_pos[r][i]>=l)
{
ret=max(ret,ret^cnt_val[r][i]);
}
}
return ret;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(cnt_pos,0,sizeof(cnt_pos));
memset(cnt_val,0,sizeof(cnt_val));
memset(d,0,sizeof(d));
memset(pos,0,sizeof(pos));
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Insert(a[i],i);
for(int j=30;j>=0;j--)
{
cnt_pos[i][j]=pos[j];
cnt_val[i][j]=d[j];
}
}
int ans=0;
while(m--)
{
int op,l,r,x;
scanf("%d",&op);
if(op==0)
{
scanf("%d%d",&l,&r);
l=(l^ans)%n+1,r=(r^ans)%n+1;
if(l>r) swap(l,r);
ans=query(l,r);
printf("%d\n",ans);
}
else
{
scanf("%d",&a[++n]);
Insert(a[n]^ans,n);
for(int j=30;j>=0;j--)
{
cnt_pos[n][j]=pos[j];
cnt_val[n][j]=d[j];
}
}
}
}
return 0;
}