题目链接
题意:现在有两种操作:
操作一:输入0 l,r:表示在数组l,r区间中找出任意个数,使这些数的异或和最大。
操作二:输入1 x,表示在数组末尾插入一个数x。
题解:这个题用01字典树做不了,只能用线性基做。对于每个线性基,将出现位置靠右的数字尽可能地放在高位,也就是说在插入新数字的时候,要同时记录对应位置上数字的出现位置,并且在找到可以插入的位置的时候,如果新数字比位置上原来的数字更靠右,就将该位置上原来的数字向低位推,这里记录用两个二维数组记录区间[1,i]第i为线性基位置和值(cnt_d[i][j]保存的是[1,i]区间第j位线性基的值,cnt_pos[i][j]保存的是[1,i]区间第j位线性基的位置),然后在给定[l,r],从最高位开始,找出cnt_pos[r][j]>=l&&(ans^cnt_d[r][j])>ans的值异或起来就是答案。点这里CF 1100F可以做做。
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int mod = 998244353;
const int maxn = 5e5 + 5;
const int inf = 1e9;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int n, a[maxn], q, pos[35], d[35];
int cnt_pos[maxn][32], cnt_d[maxn][32];
void add_xor(int x, int id) {
for (int i = 30; i >= 0; i--) {
if (x & (1 << i)) {
if (!d[i]) {
d[i] = x, pos[i] = id;
break;
}
if (pos[i] < id) {
swap(pos[i], id), swap(d[i], x);
}
x = x ^ d[i];
}
}
}
int query(int l, int r) {
int ans = 0;
for (int i = 30; i >= 0; i--) {
if (cnt_pos[r][i] >= l) {
ans = max(ans, ans ^ cnt_d[r][i]);
}
}
return ans;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
me(d, 0), me(pos, 0);
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
add_xor(a[i], i);
for (int j = 30; j >= 0; j--)
cnt_pos[i][j] = pos[j], cnt_d[i][j] = d[j];
}
int ans = 0;
while (q--) {
int opt, x, l, r;
scanf("%d", &opt);
if (opt) {
scanf("%d", &x);
add_xor(x ^ ans, ++n);
for (int j = 30; j >= 0; j--)
cnt_pos[n][j] = pos[j], cnt_d[n][j] = d[j];
} else {
scanf("%d%d", &l, &r);
l = (l ^ ans) % n + 1;
r = (r ^ ans) % n + 1;
if (l > r)
swap(l, r);
printf("%d\n", ans = query(l, r));
}
}
}
return 0;
}