https://www.luogu.com.cn/problem/CF1100F
求一个区间的异或最大值。
固定右端点后,相当于求一个后缀的最大值。我们可以对现在的线性基进行一些处理。
线性基是肯定要记的,但是哪些线性基是有用的呢?我们对于线性基的每个主元加一个 p o s i pos_i posi,表示这个主元是哪个位置贡献的,那么只有 p o s i ≥ l pos_i\ge l posi≥l 的主元是可用的。
至于维护 p o s i pos_i posi,我们可以贪心。对于正常线性基,如果这个主元已经有别人填了,我们是直接异或后跳过的。而现在,我们显然是直接替换最优,直接swap即可。
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define debag(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 500010
int n, m, i, j, k, T;
struct Ji {
int p[31], pos[31];
void insert(int j, int x) {
for(int i = 30, val = x; i >= 0; --i) if(val & (1 << i)) {
if(!p[i]) { p[i] = val, pos[i] = j; return ; }
if(pos[i] < j) { swap(pos[i], j); swap(val, p[i]); /*continue;*/ }
val ^= p[i];
}
}
int qry(int l) {
int ans = 0;
// for(int i = 30; i >= 0; --i)
// if(p[i]) debug("%d : %d(%d)\n", i, pos[i], p[i]);
for(int i = 30; i >= 0; --i)
if(p[i] && pos[i] >= l) ans = max(ans, ans ^ p[i]);
return ans;
}
}a[N];
signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T = read();
// while(T--) {
//
// }
int l, r, x, q, ans;
n = read();
for(i = 1; i <= n; ++i) {
x = read(); a[i] = a[i - 1]; a[i].insert(i, x);
}
q = read();
while(q--) {
l = read(); r = read(); ans = a[r].qry(l);
printf("%d\n", ans);
}
return 0;
}