Description

在某时刻加入或删除一个点,问每个时刻的集合中能异或出来的最大值是多少.

Sol

线段树+按时间分治+线性基.

按时间分治可以用 \(logn\) 的时间来换取不进行删除的操作.

把一个数字的存在时间挂在线段树的区间上,不超过 \(logn\) 个区间,所以总和不超过 \(nlogn\) 个节点信息.

然后从上往下走遍历整个线段树,每次到根节点统计一下答案,这里跟线性基有些不同,线性基转置矩阵就是普通的高斯消元,这时候维护线性基,每次插入一个数,更新的贡献,统计答案的时候从上往下贪心,选一个最大值,而不是回带...

Code

/**************************************************************
Problem: 4184
User: BeiYu
Language: C++
Result: Accepted
Time:11256 ms
Memory:37624 kb
****************************************************************/ #include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int N = 5e5+50;
const int M = 35; int n;
map< int,int > mp;
LL pow2[M],ans[N]; struct Py {
LL b[M];
Py() { memset(b,0,sizeof(b)); }
void insert(int x) {
for(int i=M-1;~i;i--) if(x&pow2[i]) {
if(!b[i]) { b[i]=x;break; }
else x^=b[i];
}
}
LL GetAns() {
LL ans=0;
for(int i=M-1;~i;i--) if((ans^b[i])>ans) ans^=b[i];
return ans;
}
}piyan;
struct SegMentTree {
vector< int > d[N<<2];
#define lc (o<<1)
#define rc (o<<1|1)
#define mid ((l+r)>>1) void insert(int o,int l,int r,int L,int R,int x) {
if(L<=l && r<=R) return void(d[o].push_back(x));
if(L<=mid) insert(lc,l,mid,L,R,x);
if(R>mid) insert(rc,mid+1,r,L,R,x);
}
void DFS(int o,int l,int r,Py py) {
for(vector< int > ::iterator i=d[o].begin();i!=d[o].end();i++) py.insert(*i);
if(l==r) return void(ans[l]=py.GetAns());
DFS(lc,l,mid,py),DFS(rc,mid+1,r,py);
}
}seg; inline int in(int x=0,char ch=getchar(),int v=1) {
while(ch>'9' || ch<'0') v=ch=='-' ? -1 : v,ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x*v;
}
int main() {
n=in();
for(int i=1;i<=n;i++) {
int x=in();
if(x>=0) mp[x]=i;
else x=-x,seg.insert(1,1,n,mp[x],i-1,x),mp.erase(x);
}
for(map< int,int > ::iterator i=mp.begin();i!=mp.end();i++)
if((*i).second) seg.insert(1,1,n,(*i).second,n,(*i).first);
pow2[0]=1;for(int i=1;i<M;i++) pow2[i]=pow2[i-1]<<1;
seg.DFS(1,1,n,piyan);
for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
return 0;
}

BZOJ 4184: shallot的更多相关文章

  1. BZOJ.4184.shallot(线段树分治 线性基)

    BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...

  2. bzoj 4184 shallot——线段树分治+线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4184 本来想了可持久化trie,不过空间是 nlogn (出一个节点的时候把 tot 复原就 ...

  3. bzoj 4184: shallot (线段树维护线性基)

    题面 \(solution:\) 这一题绝对算的上是一道经典的例题,它向我们诠释了一种新的线段树维护方式(神犇可以跳过了).像这一类需要加入又需要维护删除的问题,我们曾经是遇到过的像莫对,线段树... ...

  4. 「bzoj 4184: shallot」

    权限题 线段树分治加线性基 首先这个题要求删除线性基肯定是没法处理的 于是我们套上一个线段树分治 线段树分治就是一种能够避免删除的神仙操作 我们发现询问是对一个时间的单点询问,而每一个数存在的时间却是 ...

  5. BZOJ 4184 shallot 线性基+分治

    Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且让小葱从 ...

  6. bzoj 4184 shallot 时间线建线段树+vector+线性基

    题目大意 n个时间点 每个时间点可以插入一个权值或删除一个权值 求每个时间点结束后异或最大值 分析 异或最大值用线性基 但是线性基并不支持删除操作 我们可以对时间线建一棵线段树 离线搞出每个权值出现的 ...

  7. bzoj 4184: shallot【线性基+时间线段树】

    学到了线段树新姿势! 先离线读入,根据时间建一棵线段树,每个节点上开一个vector存这个区间内存在的数(使用map来记录每个数出现的一段时间),然后在线段树上dfs,到叶子节点就计算答案. 注意!! ...

  8. BZOJ 4184 线段树+高斯消元

    思路: 线段树表示的是时间 每回最多log个段 区间覆盖 一直到叶子 的线性基 xor 一下 就是答案 一开始没有思路 看了这篇题解 豁然开朗 http://www.cnblogs.com/joyou ...

  9. LOJ 2312(洛谷 3733) 「HAOI2017」八纵八横——线段树分治+线性基+bitset

    题目:https://loj.ac/problem/2312 https://www.luogu.org/problemnew/show/P3733 原本以为要线段树分治+LCT,查了查发现环上的值直 ...

随机推荐

  1. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  2. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. go channel

    channel 是go语言中不同goroutine之间通信一种方法 //定义一个channel c := make(chan bool) //向channel中写入 c <- true //读取 ...

  4. Binding笔记

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...

  5. spriing boot 实战

    一.spring基础 1. 依赖注入 我们常说的控制翻转(Inversion of Control -IOC)和依赖注入(dependency injection-DI)在Spring环境下是等同的概 ...

  6. delphi 取硬盘号

    function GetVolumeID: string; var vVolumeNameBuffer: ..] of Char; vVolumeSerialNumber: DWORD; vMaxim ...

  7. 【白话设计模式四】单例模式(Singleton)

    转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...

  8. centos 安装lnmp

    1:查看环境 cat /etc/redhat-release 2:关闭防火墙 chkconfig iptables off 3:配置CentOS 6.0 第三方yum源(CentOS默认的标准源里没有 ...

  9. zlib-1.2.7/libpng-1.5.9 instead of zlib-1.2.8/libpng-1.6.6

    The reason for the failure apparently appears to be version incompatibility, partly may be due to li ...

  10. Linux 建立文件夹的链接

    linux下的软链接类似于windows下的快捷方式 建立软链接 ln -s a b a 就是源文件,b是链接文件名,其作用是当进入b目录,实际上是链接进入了a目录 example:ln -s /ho ...