思路:线性基+贪心,保存线性基中每一位的最后一个
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define MIN(a, b) (a)<(b)?(a):(b) #define MAX(a, b) (a)>(b)?(a):(b) #define pdd pair<long double, long double> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 5e5 + 5; int a[N], ans[N], base[20], pos[20]; piii Q[N]; void add(int p) { int x = a[p]; for (int i = 19; i >= 0; i--) { if(x&(1<<i)) { if(!base[i]) { base[i] = x; pos[i] = p; return ; } if(p > pos[i]) swap(base[i], x), swap(pos[i], p); x ^= base[i]; } } } int main() { int n, q; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%d %d", &Q[i].fi.se, &Q[i].fi.fi); Q[i].se = i; } sort(Q+1, Q+1+q); int now = 1; for (int i = 1; i <= q; i++) { int L = Q[i].fi.se, R = Q[i].fi.fi; while(now <= R) add(now++); int res = 0; for (int i = 19; i >= 0; i--) if(pos[i] >= L) res = MAX(res, res^base[i]); ans[Q[i].se] = res; } for (int i = 1; i <= q; i++) printf("%d ", ans[i]); return 0; }