杭电2019多校第一场 HDU——6579 Operation(线性基)

时间限制: 1 Sec  内存限制: 128 MB

[提交] [状态] [命题人:admin]

 

题目描述

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.

 

输入

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.

 

 

输出

For each type 0 operation, please output the maximum xor sum in a single line.

 

样例输入

复制样例数据

1
3 3
0 1 2
0 1 1
1 3
0 3 4

样例输出

1
3

题意:给n个数,m个操作,操作0是求l,r区间任意数组合求异或最大值,操作1是在数列最后再加一个数,别忘了两个操作的加密操作,注意:加密操作ans初始化为0(题目说的)~~~

题解:看到数异或最大值,首先想到线性基~~

上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 5e5+10;
int a[MAX];
int p[MAX][31],pos[MAX][31];
int n,m;
void add(int x,int id){
	int cao=id;
	for (int i = 0; i <= 29;i++){
		p[id][i]=p[id-1][i];
		pos[id][i]=pos[id-1][i];
	}
	for (int i = 29; i >= 0;i--){
		if(x&(1<<i)){
			if(!p[id][i]){
				p[id][i]=x;
				pos[id][i]=cao;
				return;
			}
			if(pos[id][i]<cao){
				swap(p[id][i],x);
				swap(pos[id][i],cao);
			}
			x^=p[id][i];
		}
	}
}
int query(int l,int r){
	int maxx=0;
	for (int i = 29; i >= 0;i--){
		if(pos[r][i]>=l&&(maxx^p[r][i])>maxx){
			maxx^=p[r][i];
		}	
	}
	return maxx;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int ans=0;
		scanf("%d%d",&n,&m);
		for (int i = 1; i <= n;i++) {
			scanf("%d",&a[i]);
			add(a[i],i);
		}
		while(m--){
			int op;
			scanf("%d",&op);
			if(op==1){
				scanf("%d",&a[++n]);
				a[n]^=ans;
				add(a[n],n);
			}
			else{
				int l,r;
				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);
			}
		}
	}
	return 0;
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值