BZOJ 4184: shallot 线性基+线段树分治
复习一下线性基 ~
code:
#include <cmath> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #define N 500008 #define ll long long #define lson now<<1 #define rson now<<1|1 using namespace std; namespace IO { void setIO(string s) { string in=s+".in"; string out=s+".out"; freopen(in.c_str(),"r",stdin); freopen(out.c_str(),"w",stdout); } }; int bin[32],a[N],S[N],n,tot,edges; int hd[N],to[N],nex[N]; struct node { int l,r,x; node(int l=0,int r=0,int x=0):l(l),r(r),x(x){} }p[N]; vector<node>sg[N<<2]; struct Base { int p[32],nm; void insert(int x) { for(int i=30;i>=0;--i) { if(x&bin[i]) { if(p[i]) x^=p[i]; else { p[i]=x; break; } } } } int query(int x) { for(int i=30;i>=0;--i) if((x^p[i])>x) x^=p[i]; return x; } }tmp; void update(int l,int r,int now,int L,int R,node e) { if(l>=L&&r<=R) { sg[now].push_back(e); return; } int mid=(l+r)>>1; if(L<=mid) update(l,mid,lson,L,R,e); if(R>mid) update(mid+1,r,rson,L,R,e); } void dfs(int l,int r,int now,Base G) { for(int i=0;i<sg[now].size();++i) G.insert(sg[now][i].x); if(l==r) { printf("%d\n",G.query(0)); return; } int mid=(l+r)>>1; dfs(l,mid,lson,G),dfs(mid+1,r,rson,G); } int main() { // IO::setIO("input"); int i,j,len; scanf("%d",&n); for(i=0;i<=30;++i) bin[i]=1<<i; for(i=1;i<=n;++i) scanf("%d",&a[i]); for(i=1;i<=n;++i) S[i]=abs(a[i]); sort(S+1,S+1+n); for(i=1;i<=n;++i) { if(a[i]>0) { int x=lower_bound(S+1,S+1+n,a[i])-S; nex[++edges]=hd[x],hd[x]=edges,p[edges]=node(i,n,a[i]); } else { int x=lower_bound(S+1,S+1+n,-a[i])-S; p[hd[x]].r=i-1,hd[x]=nex[hd[x]]; } } for(i=1;i<=edges;++i) update(1,n,1,p[i].l,p[i].r,p[i]); dfs(1,n,1,tmp); return 0; }