1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include<bits/stdc++.h> using std::vector; typedef vector<int> vi; typedef long long ll; const int DD=20; template<typename Tp=int>inline Tp read(){Tp x(0);int op(0);char ch=getchar();while(ch<'0'||ch>'9')op|=(ch==45),ch=getchar();while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();return op?-x:x;} struct LinerChicken{ private: vector<int> C,P; public: LinerChicken():C(DD+1),P(DD+1){} void ins(int p,int x){ for(int i=DD;~i;--i)if(x&(1<<i)){ if(!C[i])return C[i]=x,P[i]=p,void(); else if(P[i]<p)std::swap(C[i],x),std::swap(P[i],p); x^=C[i]; } } int qry(int l){ int ans=0; for(int i=DD;~i;--i)if(P[i]>=l)ans=std::max(ans,ans^C[i]); return ans; } }; int main(){ int n=read(); vi a(n+1); for(int i=1;i<=n;++i)a[i]=read(); int Q=read(); vector<std::array<int,3>> q(Q+1); for(int i=1;i<=Q;++i)q[i][0]=read(),q[i][1]=read(),q[i][2]=i; std::sort(q.begin()+1,q.end(),[&](std::array<int,3> x,std::array<int,3> y){return x[1]==y[1]?x[0]<y[0]:x[1]<y[1];}); LinerChicken C; vi ans(Q+1); for(int i=1,j=1;i<=n;++i){ C.ins(i,a[i]); for(;j<=Q&&q[j][1]<=i;++j)ans[q[j][2]]=C.qry(q[j][0]); } for(int i=1;i<=Q;++i)printf("%d\n",ans[i]); return 0; }
|