若想要深入学习可持久化0-1Trie树,传送门


Description:

给定数列 \(\{a_n\}\) ,支持两种操作:

  • 在数列尾添加一个数 \(x\) ,数列长度变成 \(n+1\) ;
  • 给定闭区间 \([l,r]\) 和一个数 \(x\) ,求:

\[\max_{i=l}^{r}\left \{\left(\bigoplus_{j=i}^{n}a_j \right)\bigoplus x\right \}
\]

Method:

定义 \(Xorsum_i\) 为 \(\bigoplus_{i=1}^{n}a_i\) ,即前缀异或和。我们显然可以得到

\[\left(\bigoplus_{i=pos}^{n}a_i\right)\bigoplus x=Xorsum_{pos-1}\bigoplus Xorsum_n \bigoplus x
\]

:\(x\bigoplus x=0\) , \(x \bigoplus 0=x\)

我们发现 \(Xorsum_n\bigoplus x\) 是一个定值,我们只需要维护 \(Xorsum_{pos-1}\) 即可。

考虑用可持久化0-1Trie树维护。与主席树思路相同 ,我们建立 \(n+1\) 个版本的0-1Trie树,查询的时候运用贪心的思路即可。

可持久化线段树同样支持“前缀和”的思想,我们最后只需要在第 \(r\) 个版本的0-1Trie树上查找 \(l\) 位置即可。

本题毒瘤卡常,本人人丑常数大,用了fread等各种卡常操作才通过。并且由于luogu评测姬的原因(大雾,已经通过的代码又会T掉woc。卡不过的话,开o2吧。

Code:

#include<bits/stdc++.h>
#define Maxn 600010
#define Maxdep 23
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline void read(int &x)
{
int f=1;x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,m;
int sum[Maxn];
struct trie
{
trie *chd[2];
int symbl;
trie()
{
for(int i=0;i<2;i++) chd[i]=NULL;
symbl=0;
}
}*root[Maxn],tree[Maxn<<5],*tail;
void Init(){tail=tree;}
void build(trie *&p,int dep)
{
p=new (tail++)trie();
if(dep<0) return ;
build(p->chd[0],dep-1);
}
void update(trie *&p,trie *flag,int dep,int i)
{
p=new (tail++)trie();
if(flag) *p=*flag;
if(dep<0) return (void)(p->symbl=i);
int tmp=(sum[i]>>dep)&1;//判断是1还是0
if(!tmp) update(p->chd[0],flag?flag->chd[0]:NULL,dep-1,i);
else update(p->chd[1],flag?flag->chd[1]:NULL,dep-1,i);
if(p->chd[0]) p->symbl=std::max(p->symbl,p->chd[0]->symbl);
if(p->chd[1]) p->symbl=std::max(p->symbl,p->chd[1]->symbl);
}
int query(trie *p,int x,int dep,int limit)
{
if(dep<0) return sum[p->symbl]^x;
int tmp=(x>>dep)&1;
if(p->chd[tmp^1]&&p->chd[tmp^1]->symbl>=limit) return query(p->chd[tmp^1],x,dep-1,limit);
return query(p->chd[tmp],x,dep-1,limit);
}
signed main()
{
Init();
read(n),read(m);
build(root[0],Maxdep);
for(int i=1,x;i<=n;i++)
{
read(x);
sum[i]=sum[i-1]^x;
update(root[i],root[i-1],Maxdep,i);
}
for(int i=1;i<=m;i++)
{
char ch=getchar();
while(ch!='A'&&ch!='Q') ch=getchar();
if(ch=='A')
{
int x;
read(x);
n++;
sum[n]=sum[n-1]^x;
update(root[n],root[n-1],Maxdep,n);
continue;
}
if(ch=='Q')
{
int l,r,x;
read(l),read(r),read(x);
int ans=query(root[r-1],sum[n]^x,Maxdep,l-1);
printf("%d\n",ans);
continue;
}
}
return 0;
}

最新文章

  1. IOS编程思想
  2. Android本地服务
  3. mysql 插入汉字出现问号 解决方法
  4. python中字典dict pop方法
  5. PHP中0、空、null和false的总结
  6. bootstrap Tooltip换行问题
  7. 解决java.sql.SQLException: Parameter number X is not an OUT parameter--转
  8. Cocos2d-x程序Windows下VC中文乱码的解决(用MultiByteToWideChar进行转换,VC2010有非常厉害的execution_character_set)
  9. JS中的函数、Bom、DOM及JS事件
  10. Tomcat 笔记-配置虚拟目录
  11. Python3+Selenium2完整的自动化测试实现之旅(六):Python单元测试模块Unittest运用
  12. 网络最大流算法—最高标号预流推进HLPP
  13. JAVA之复制数组
  14. rsa加密解密, 非对称加密
  15. Python - requests https请求的坑
  16. [C]语法, 知识点总结(二. 结构体, 类别名, static, const)
  17. svn提交遇到冲突解决方法
  18. MongoDB——更新操作(Update)c#实现
  19. PG里如何查看表,索引,表空间,数据库大小
  20. 感谢CSDN赠送的图书和杂志(5月份)

热门文章

  1. vue/react/angular开发的css架构思考
  2. centos从零开始安装elasticSearch
  3. 换个语言学一下 Golang(14) ——fmt包
  4. UIPath RPA 自动化脚本 机器人从入门到精通
  5. vue前端实战注意事项
  6. Java 流程控制语句 之 顺序结构
  7. Js编程实践
  8. nano命令
  9. memcpy函数的实现
  10. Windows Server 2008 R2 忘记密码的处理方法