19暑假线性基G

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
注意每次操作不管是增加还是询问都要进行在线操作,下面的代码,询问的时候可以直接用一个函数解决,但是额外增加一个数组可以对区间线性基做更多操作,方便修改

#include<bits/stdc++.h>
#define maxn 500005
using namespace std;
int b[maxn][35],pos[maxn][35];
bool insert(int h,int x)
{
	for(int i=0;i<=31;++i)
	b[h][i]=b[h-1][i],pos[h][i]=pos[h-1][i];
	int tmp=h;
	for(int i=31;i>=0;--i)
	if(x&(1<<i))
	{
		if(b[h][i])
		{
			if(pos[h][i]<tmp)
			{
				swap(pos[h][i],tmp);
				swap(b[h][i],x);
			}
			x^=b[h][i];
		}
		else
		{
			b[h][i]=x;
			pos[h][i]=tmp;
			return true;
		}
	}
	return false;
}
int a[35];
int get_max()                 
	{
		int ret = 0;
		for(int i = 32;i >= 0;--i)
			if((ret^a[i]) > ret)
			{
				ret ^= a[i];
			}
				
		return ret;
	}
int main()
{
	int t,n,m,k,temp,op,x;
	scanf("%d",&t);
	while(t--)
	{
		memset(b,0,sizeof(b));
		memset(pos,0,sizeof(pos));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&temp);
			insert(i,temp);
		}
		int ans=0;
		while(m--)
		{
			scanf("%d",&op);
			if(op==0)
			{
				memset(a,0,sizeof(a));
			    int l,r;
			    scanf("%d%d",&l,&r);
			    l=(l^ans)%n+1;
				r=(r^ans)%n+1;	
				if(l>r)
				swap(l,r);	    
				for(int i=31;i>=0;--i)
			    {
				    if(pos[r][i]>=l)
				    {
					    a[i]=b[r][i];
				    }				
			    }
			    ans=get_max();
			    printf("%d\n",ans);
			}
			else
			{
				scanf("%d",&x);
				n++;
				x^=ans;
				insert(n,x);
			}			
		}		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值