# [BZOJ4184] shallot

6
1 2 3 4 -2 -3

1
3
3
7
7
5

### 试题分析

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
//#include<cmath>
#include<algorithm>

using namespace std;
int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int INF = 2147483600;
const int MAXN = 500010;

int N;
struct qe{
int x,tim;
}a[MAXN+1],b[MAXN+1];
struct data{
int u,v,val;
}in[MAXN+1];

bool cmp(qe a,qe b){if(a.x!=b.x) return a.x<b.x; return a.tim<b.tim;}
int A,D; vector<int> vec[MAXN<<2]; int sz;
bool vis[MAXN+1];

for(int i=0;i<vec[rt].size();i++) if((x^vec[rt][i])<x) x^=vec[rt][i];
if(x){ vec[rt].push_back(x);
for(int i=vec[rt].size()-1;i;i--) if(vec[rt][i]>vec[rt][i-1]) swap(vec[rt][i],vec[rt][i-1]);
} return ;
}
inline void update(int rt,int s){
}
inline void insert(int rt,int l,int r,int L,int R,int x){
if(L<=mid) insert(rt<<1,l,mid,L,R,x);
if(R>mid) insert(rt<<1|1,mid+1,r,L,R,x);
}
inline void Query(int rt,int l,int r,vector<int> v){
for(int j=0;j<vec[rt].size();j++){
int x=vec[rt][j];
for(int i=0;i<v.size();i++) if((x^v[i])<x) x^=v[i];
if(x){ v.push_back(x);
for(int i=v.size()-1;i;i--) if(v[i]>v[i-1]) swap(v[i],v[i-1]);
}
}
if(l==r){
int ret=0;
for(int j=0;j<v.size();j++) if((ret^v[j])>ret) ret^=v[j];//cout<<ret<<" "<<(ret^c[0][j])<<endl;
printf("%d\n",ret);return ;
}
int mid=(l+r)>>1; Query(rt<<1,l,mid,v);
Query(rt<<1|1,mid+1,r,v);
return ;
}

int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
for(int i=1;i<=N;i++){
if(x>0) a[++A].x=x,a[A].tim=i;
else b[++D].x=-x,b[D].tim=i;
} sort(a+1,a+A+1,cmp); sort(b+1,b+D+1,cmp);
for(int i=1;i<=D;i++){
while(a[j].x<b[i].x) ++j;
in[++sz].u=a[j].tim; in[sz].val=a[j].x;
in[sz].v=b[i].tim; vis[j]=true; ++j;
} for(int i=1;i<=A;i++)
if(!vis[i]) in[++sz].u=a[i].tim,in[sz].v=N+1,in[sz].val=a[i].x;
for(int i=1;i<=sz;i++)
insert(1,1,N,in[i].u,in[i].v-1,in[i].val);
vector<int> v;
Query(1,1,N,v);
return 0;
}


posted @ 2018-08-27 11:24  wxjor  阅读(161)  评论(0编辑  收藏  举报