Ivan and Burgers(CF-1100F)

Problem Description

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 x which satisfy li≤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⊕c burles, where ⊕ denotes the bitwise XOR operation.

Currently Ivan has 2^(2^100)−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 i. 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 n (1≤n≤500000) — the number of burger shops.

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

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

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

Output

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

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 — 0, 12, 14, 23, 12⊕14=2, 14⊕23=25, 12⊕23=27, 12⊕14⊕23=20. The maximum amount of money it turns out to spend, if you go to the first and third burger — 12⊕23=27.

题意:给出长度为 n 的一个序列,再给出 m 个查询,每组查询给出 l、r 两个数,查询区间 [l,r] 中的异或和最大值

思路:查询异或和最大值可以利用线性基来做,但由于要区间查询,那么可以利用前缀线性基来做

Source Program

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

struct PrefixLinearBasis{
    int d[N][32];//前缀线性基
    int pos[N][32];//最后一个修改i这个位置的数
    int num;
    PrefixLinearBasis(){
        memset(d,0,sizeof(d));
        memset(pos,0,sizeof(pos));
        num=0;
    }
    void add(int x){//向线性基中添加x
        num++;
        for(int i=0; i<32; i++){//复制前num-1个线性基
            d[num][i]=d[num-1][i];
            pos[num][i]=pos[num-1][i];
        }

        int P=num;
        for(int i=31; i>=0; i--){
            if((x>>i)&1){
                if(d[num][i]){//插入失败
                    if(pos[num][i]=0; i--){
            if(pos[r][i]res) 
                res^=d[r][i];
        }
        return res;
    }
    int queryMin(int l,int r) {//查询[l,r]中的最小值
        for(int i=0; i<=60; i++){
            if(pos[r][i]

 

你可能感兴趣的:(#,CodeForces,#,线性代数——线性基与前缀线性基)