[BZOJ4184]shallot(线段树+线性基)
2017-06-11 18:54
288 查看
=== ===
这里放传送门=== ===
题解
线性基不支持删除,但是可以发现每个数字出现的范围是一段区间,那么如果用线段树把这一段区间打上标记,然后就相当于是每次单点查询。也就是搞一个下标是时间的线段树。然后标记永久化,查答案的时候dfs一遍线段树就可以了。这题空间贼卡。。一开始算算理论上界4*n还得加上线性基根本开不下。。那动态开点吧,nlogn还是不行。。最后干脆出几组大数据看看发现最多700000+个节点,然后卡着开数组终于跑过去了。。。
代码
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,a[500010],now[40],cnt,rt,ans[500010],nxt[500010],head[500010],p[500010]; bool ext[500010]; struct segtree{ int l,r,v[32],res; segtree(){l=r=0;res=31;memset(v,0,sizeof(v));} void add(int val){ if (res==0) return; for (int i=30;i>=0;i--) if (val&(1<<i)) if (v[i]==0){v[i]=val;--res;break;} else val^=v[i]; } int calc(){ int ans=0; for (int i=30;i>=0;i--) if ((ans^v[i])>ans) ans^=v[i]; return ans; } void add(segtree now){ if (res==0) return; for (int i=30;i>=0;i--) if (now.v[i]!=0){ if (res==0) break; if (v[i]==0){v[i]=now.v[i];--res;continue;} for (int j=30;j>=0;j--) if ((now.v[i])&(1<<j)) if (v[j]==0){v[j]=now.v[i];--res;break;} else now.v[i]^=v[j]; } } }t[850010],b; int Abs(int x){return (x<0)?-x:x;} int comp(int x,int y){return Abs(a[x])<Abs(a[y]);} void Prepare(){ int cnt=0,v[500010]; for (int i=1;i<=n;i++) p[i]=i; sort(p+1,p+n+1,comp); for (int i=1;i<=n;i++) if (Abs(a[p[i]])==Abs(a[p[i-1]])) if (a[p[i]]<0) v[p[i]]=-cnt; else v[p[i]]=cnt; else{ ++cnt; if (a[p[i]]<0) v[p[i]]=-cnt; else v[p[i]]=cnt; } for (int i=1;i<=cnt;i++) head[i]=n+1; for (int i=n;i>=1;i--) if (v[i]>0){ nxt[i]=head[v[i]]; head[v[i]]=i; } memset(p,0,sizeof(p)); for (int i=1;i<=n;i++) if(v[i]<0){ int val=-v[i]; p[head[val]]=i; head[val]=nxt[head[val]]; } } void add(int &i,int l,int r,int left,int right,int val){ if (i==0){i=++cnt;t[i]=segtree();} if (left<=l&&right>=r){t[i].add(val);return;} int mid=(l+r)>>1; if (left<=mid) add(t[i].l,l,mid,left,right,val); if (right>mid) add(t[i].r,mid+1,r,left,right,val); } void getans(int now,int l,int r,segtree base){ segtree newbase=base; newbase.add(t[now]); if (l==r) {ans[l]=newbase.calc();return;} int mid=(l+r)>>1; getans(t[now].l,l,mid,newbase); getans(t[now].r,mid+1,r,newbase); } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); Prepare(); cnt=rt=0; for (int i=1;i<=n;i++) if (a[i]>0) if (p[i]==0) add(rt,1,n,i,n,a[i]); else add(rt,1,n,i,p[i]-1,a[i]); getans(rt,1,n,b); for (int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
相关文章推荐
- [分治线段树 & 线性基] BZOJ4184: shallot
- [线段树分治][线性基]BZOJ 4184: shallot
- BZOJ_4184_shallot_线段树按时间分治维护线性基
- BZOJ 4184: shallot 线段树分治 线性基
- bzoj4184 shallot(线段树分治+线性基)
- bzoj 4184: shallot【线性基+时间线段树】
- bzoj 4184: shallot 分治+线性基
- [CDQ分治 线性基] BZOJ 4184 shallot
- 【BZOJ4184】shallot 线段树+vector+线性基
- 【BZOJ 4184】shallot 线性基
- 【Bzoj4184】 shallot——时间分治+线性基
- bzoj 4184 shallot 时间线建线段树+vector+线性基
- bzoj4184-shallot
- 4184: shallot 线段树+按时间分治+线性基
- Bzoj 4184 shallot
- [BZOJ 4184]shallot
- BZOJ 4184 shallot
- [线性基 树链剖分 线段树 || ST表 || 点分治] BZOJ 4568 [Scoi2016]幸运数字
- BZOJ 4184 线段树+高斯消元
- BZOJ4184: shallot