題目鏈接
題意
初始時有\(n\)個數,現在有\(q\)次操作:
- 查詢\([l,r]\)內選擇一些數使得異或和最大;
- 在末尾加入一個數。
題目強制在線。
思路
對於\(i\)我們記錄\([1,i]\)每個基底最靠近\(i\)的位置和這個位置的值,然后查詢時看\(r\)這個位置記錄的每個基底的位置是否大於等於\(l\),如果大於等於那么\([l,r]\)內一定有一個位置可以貢獻這個基底,然后比較答案大小即可。
本題和\(cf1100F\)一樣的寫法只是多了個操作而已。
代碼實現如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1000000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int t, n, q, op, l, r, x;
int a[maxn], b[32], pos[32], base[maxn][32], las[maxn][32];
bool add(int val, int pp) {
for (int i = 30; i >= 0; i--) {
if (val & (1ll << i)) {
if (!b[i]) {
pos[i] = pp;
b[i] = val;
break;
}
if(pos[i] < pp) {
swap(b[i], val);
swap(pos[i], pp);
}
val ^= b[i];
}
}
return val > 0;
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &q);
for(int i = 30; i >= 0; --i) b[i] = 0, pos[i] = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
add(a[i], i);
for(int j = 30; j >= 0; --j) base[i][j] = b[j], las[i][j] = pos[j];
}
int lastans = 0;
while(q--) {
scanf("%d", &op);
if(op) {
++n;
scanf("%d", &x);
x ^= lastans;
add(x, n);
for(int i = 30; i >= 0; --i) base[n][i] = b[i], las[n][i] = pos[i];
} else {
scanf("%d%d", &l, &r);
l = (l ^ lastans) % n + 1, r = (r ^ lastans) % n + 1;
if(l > r) swap(l, r);
lastans = 0;
for(int i = 30; i >= 0; --i) {
if(las[r][i] >= l && (lastans ^ base[r][i]) > lastans) {
lastans ^= base[r][i];
}
}
printf("%d\n", lastans);
}
}
}
return 0;
}