CF1100F Ivan and Burgers(线性基)

解析

做幸运数字的时候逛题解区爬过来的
挺妙的

把所有询问离线下来,按右端点排序
贪心的让最高位更高的向量出现的尽可能向右
实现上可以把线性基里的向量和当前的数swap来实现
代码就非常好写了

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf (n+1)
//#define debug(...) fprintf(stderr,__VA_ARGS__)
const int N=5e5+100;
const int M=2e5+10500;
const double eps=1e-5;
inline ll read(){ll x(0),f(1);char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
	return x*f;
}

int n,m;

struct node{
	int to,nxt;
}p[N<<1];
int fi[N],cnt;
inline void addline(int x,int y){
	p[++cnt]=(node){y,fi[x]};fi[x]=cnt;
	return;
}

ll mi[65];

int v[35],pos[35];
void upd(int x,int id){
	for(int i=20;i>=0;i--){
		if(x&(1<<i)){
			if(!v[i]){
				v[i]=x;pos[i]=id;break;
			}	
			if(pos[i]<id){
				swap(pos[i],id);swap(v[i],x);
			}
			x^=v[i];
		}
	}
}
struct query{
	int l,r,id;
	bool operator < (const query o){return r<o.r;}
}q[N];
int ans[N],a[N];
inline int calc(int l){
	int res(0);
	for(int i=20;i>=0;i--){
		//printf("i=%d v=%d pos=%d\n",i,v[i],pos[i]);
		if((res&(1<<i))==0&&v[i]&&pos[i]>=l) res^=v[i];
	}
	return res;
}
int main(){
#ifndef ONLINE_JUDGE
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
#endif
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	m=read();
	for(int i=1;i<=m;i++){
		q[i].l=read();q[i].r=read();q[i].id=i;
	}
	sort(q+1,q+1+m);
	int pl(0);
	for(int i=1;i<=m;i++){
		while(pl<q[i].r){
			pl++;upd(a[pl],pl);
		}
		ans[q[i].id]=calc(q[i].l);
		//printf("(%d %d) id=%d\n",q[i].l,q[i].r,q[i].id);
	}
	for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}
/*
10
36 32 32 70 101 8 60 25 101 88
1
2 9
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值