# BZOJ 4184 shallot 线性基+分治

6
1 2 3 4 -2 -3

1
3
3
7
7
5

## HINT

N<=500000,Ai<=2^31-1

—————————————————————————————————————————————————————

%%%PoPoQQQ大爷ORZ

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<algorithm>
6 #include<cmath>
7 #include<queue>
8 #include<set>
9 #include<map>
10 #include<vector>
11 #include<cctype>
12 using namespace std;
13 const int maxn=500005;
14
15 int N,ans[maxn];
16 struct data{ int l,r,v; };
17 map<int,int>C,lp;
18 map<int,int>::iterator it;
19 struct Linear_Base{
20     static const int up=31;
21     int b[up];
22     Linear_Base(){ memset(b,0,sizeof(b)); }
23     void ins(int x){
24         for(int i=up-1;i>=0;i--) if((1<<i)&x){
25             if(!b[i]) { b[i]=x; break; }
26             else x^=b[i];
27         }
28     }
29     int query(){
30         int re=0;
31         for(int i=up-1;i>=0;i--)
32             re=max(re,re^b[i]);
33         return re;
34     }
35 }Lb;
36
37 void solve(int L,int R,vector<data>a,Linear_Base bl)
38 {
39     int m=L+R>>1,n=a.size();
40     vector<data>l,r;
41     for(int i=0;i<n;i++){
42         if(a[i].l==L&&a[i].r==R) bl.ins(a[i].v);
43         else if(a[i].r<=m) l.push_back(a[i]);
44         else if(a[i].l>m) r.push_back(a[i]);
45         else{
46             l.push_back((data){a[i].l,m,a[i].v});
47             r.push_back((data){m+1,a[i].r,a[i].v});
48         }
49     }
50     if(L==R){ ans[L]=bl.query(); return; }
51     solve(L,m,l,bl);
52     solve(m+1,R,r,bl);
53 }
54 void work()
55 {
56     scanf("%d",&N);
57     vector<data>a;
58     int x;
59     for(int i=1;i<=N;i++){
60         scanf("%d",&x);
61         if(x<0&&--C[-x]==0) a.push_back((data){lp[-x],i-1,-x}),C.erase(-x),lp.erase(-x);
62         else if(x>0&&++C[x]==1) lp[x]=i;
63     }
64     for(it=C.begin();it!=C.end();it++)
65         a.push_back((data){lp[it->first],N,it->first});
66     C.clear(); lp.clear();
67     solve(1,N,a,Lb);
68     for(int i=1;i<=N;i++) printf("%d\n",ans[i]);
69 }
70 int main()
71 {
72     work();
73     return 0;
74 }

posted @ 2018-03-22 09:19  KKKorange  阅读(162)  评论(0编辑  收藏  举报