XOR
Consider an array A with n elements. Each of its element is A[i] (1 ≤ i ≤ n). Then gives two integers
Q, K, and Q queries follow. Each query, give you L, R, you can get Z by the following rules.
To get Z, at first you need to choose some elements from A[L] to A[R], we call them A[i1], A[i2],
. . . , A[it], Then you can get number Z = K or (A[i1], A[i2], . . . , A[it]).
Please calculate the maximum Z for each query .
Input
Several test cases.
First line an integer T (1 ≤ T ≤ 10). Indicates the number of test cases.
Then T test cases follows. Each test case begins with three integer N, Q, K (1 ≤ N ≤ 10000,1 ≤
Q ≤ 100000, 0 ≤ K ≤ 100000). The next line has N integers indicate A[1] to A[N] (0 ≤ A[i] ≤ 108).
Then Q lines, each line two integer L, R (1 ≤ L ≤ R ≤ N).
Output
For each query, print the answer in a single line.
Sample Input
1
5 3 0
1 2 3 4 5
1 3
2 4
3 5
Sample Output
3
7
7
区间询问最大大异或值
要求的是区间中若干个数和k或运算之后的最大值
首先,插入线性基的时候把每个数字与k异或,将k中1位置a中为1时置0,而为0时置1无所谓,不在最大位,最终和k或的时候,k为1的位置还是1。
#include<bits/stdc++.h>
#define maxn 10005
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,q,k,temp;
scanf("%d",&t);
while(t--)
{
memset(b,0,sizeof(b));
memset(pos,0,sizeof(pos));
scanf("%d%d%d",&n,&q,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&temp);
insert(i,temp^k);
}
while(q--)
{
memset(a,0,sizeof(a));
int l,r;
scanf("%d%d",&l,&r);
for(int i=31;i>=0;--i)
{
if(pos[r][i]>=l)
{
a[i]=b[r][i];
}
}
printf("%d\n",k|get_max());
}
}
}