2019 Multi-University Training Contest 1 1002 线性基
题意:序列操作1.末尾加数 2.求区间异或最大值。
求区间异或的最大值很容易就想到用线性基去做
比赛的时候试了分块和线段树去维护线性基,结果都TLE了
之前也想到了维护后缀线性基,但是不知道怎么处理区间,没想到是在线性基里维护出现的位置
正解应该是对每一个后缀维护一个线性基,每次插入元素的时候都在最高位选择出现位置偏右的数,查询的时候只要当前位的出现为止在l的右边就代表区间可以产生这个数
#include <map> #include <set> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <vector> #include <string> #include <bitset> #include <cstdio> #include <cstdlib> #include <cstring> #include <sstream> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define For(i, x, y) for(int i=x;i<=y;i++) #define _For(i, x, y) for(int i=x;i>=y;i--) #define Mem(f, x) memset(f,x,sizeof(f)) #define Sca(x) scanf("%d", &x) #define Sca2(x,y) scanf("%d%d",&x,&y) #define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define Scl(x) scanf("%lld",&x) #define Pri(x) printf("%d\n", x) #define Prl(x) printf("%lld\n",x) #define CLR(u) for(int i=0;i<=N;i++)u[i].clear(); #define LL long long #define ULL unsigned long long #define mp make_pair #define PII pair<int,int> #define PIL pair<int,long long> #define PLL pair<long long,long long> #define pb push_back #define fi first #define se second typedef vector<int> VI; int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();} while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;} const double PI = acos(-1.0); const double eps = 1e-9; const int maxn = 1e6 + 10; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; int N,M,K; struct xj{ int a[32],pos[32]; inline void init(){ for(int i = 0; i <= 30; i ++) a[i] = pos[i] = 0; } inline void add(int x,int p){ for(int i = 30; i >= 0 ; i --){ if(!(x & (1 << i))) continue; if(!a[i]){ a[i] = x; pos[i] = p; break; }else{ if(p > pos[i])swap(a[i],x),swap(p,pos[i]); x ^= a[i]; } } } inline int getmax(int l){ int ans = 0; for(int i = 30; i >= 0; i --){ if(pos[i] < l) continue; if(ans < (ans ^ a[i])) ans ^= a[i]; } return ans; } }pre[maxn]; int main(){ int T; Sca(T); while(T--){ Sca2(N,M); for(int i = 1; i <= N ; i ++){ int x = read(); pre[i] = pre[i - 1]; pre[i].add(x,i); } int ans = 0; while(M--){ int op = read(); if(!op){ int l = read() ^ ans,r = read() ^ ans; l = l % N + 1; r = r % N + 1; if(l > r) swap(l,r); ans = pre[r].getmax(l); Pri(ans); }else{ int x = read() ^ ans; N++; pre[N] = pre[N - 1]; pre[N].add(x,N); } } } return 0; }