CodeForces ——1100F Ivan and Burgers(线性基)

Ivan loves burgers and spending money. There are nn burger joints on the street where Ivan lives. Ivan has qq friends, and the ii-th friend suggested to meet at the joint liliand walk to the joint riri (li≤ri)(li≤ri). While strolling with the ii-th friend Ivan can visit all joints xx which satisfy li≤x≤rili≤x≤ri.

For each joint Ivan knows the cost of the most expensive burger in it, it costs ciciburles. Ivan wants to visit some subset of joints on his way, in each of them he will buy the most expensive burger and spend the most money. But there is a small issue: his card broke and instead of charging him for purchases, the amount of money on it changes as follows.

If Ivan had dd burles before the purchase and he spent cc burles at the joint, then after the purchase he would have d⊕cd⊕c burles, where ⊕⊕ denotes the bitwise XOR operation.

Currently Ivan has 22100−122100−1 burles and he wants to go out for a walk. Help him to determine the maximal amount of burles he can spend if he goes for a walk with the friend ii. The amount of burles he spends is defined as the difference between the initial amount on his account and the final account.

Input

The first line contains one integer nn (1≤n≤5000001≤n≤500000) — the number of burger shops.

The next line contains nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤1060≤ci≤106), where cici — the cost of the most expensive burger in the burger joint ii.

The third line contains one integer qq (1≤q≤5000001≤q≤500000) — the number of Ivan's friends.

Each of the next qq lines contain two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — pairs of numbers of burger shops between which Ivan will walk.

Output

Output qq lines, ii-th of which containing the maximum amount of money Ivan can spend with the friend ii.

Examples

Input

4
7 2 3 4
3
1 4
2 3
1 3

Output

7
3
7

Input

5
12 14 23 13 7
15
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5

Output

12
14
27
27
31
14
25
26
30
23
26
29
13
13
7

Note

In the first test, in order to spend the maximum amount of money with the first and third friends, Ivan just needs to go into the first burger. With a second friend, Ivan just go to the third burger.

In the second test for a third friend (who is going to walk from the first to the third burger), there are only 8 options to spend money — 00, 1212, 1414, 2323, 12⊕14=212⊕14=2, 14⊕23=2514⊕23=25, 12⊕23=2712⊕23=27, 12⊕14⊕23=2012⊕14⊕23=20. The maximum amount of money it turns out to spend, if you go to the first and third burger — 12⊕23=2712⊕23=27.

题意:给定n个数,q次询问,求区间任意几个数的最大异或值

题解:看到数异或最大值,首先想到线性基,此题跟  “杭电2019多校第一场 1002——Operation(线性基)” 几乎是一样的!!

上代码:

#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(){
	scanf("%d",&n);
	for (int i = 1; i <= n;i++) {
		scanf("%d",&a[i]);
		add(a[i],i);
	}
	scanf("%d",&m);
	while(m--){
		int l,r;
		scanf("%d%d",&l,&r);
		int 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、付费专栏及课程。

余额充值