题目链接

/*
异或只有两种情况,可以将序列放到01Tire树上做
在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归;否则向1
(0位置上的数都满了 即 其子树叶子节点都有值)
异或情况下 若x在当前位有1,则反转0/1继续走
由于异或具有结合率,异或一次求mex和异或多个数求原数列mex是一样的
故每次不需要修改原数列,las^=opt即可
注意需要去重 因为在判断某位置rt下的区间中的数都有时,需要num[rt],相同的数显然不能算(画个图)
*/
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define gc() getchar()
const int N=3e5+5,B=20; int n,m,A[N],bit[36];
struct Node
{
int val;
Node *nxt[2];
Node() {val=0, memset(nxt,NULL,sizeof nxt);}
}*rt,pool[N*19];
Node *new_Node()
{
static int cnt=0;
return &pool[cnt++];
}
Node *root=new_Node();
struct Trie
{
void Insert(int n)
{
rt=root;
for(int i=B; i; --i)
{
// printf("I i:%d bit[i]:%d n:%d %d %d\n",i,bit[i],n,n&bit[i],rt->val);
bool id=n&bit[i];
if(!rt->nxt[id])
rt->nxt[id]=new_Node();
++rt->val, rt=rt->nxt[id];
}
++rt->val;//..!
}
inline Node *to(Node *rt,bool p)
{
if(!rt->nxt[p]) rt->nxt[p]=new_Node();
return rt->nxt[p];
}
int Query_Mex(int x)
{
int res=0; rt=root;
for(int i=B; i; --i)
{
if(x&bit[i])
if(!rt->nxt[1]) return res;//后面都没有过
else if(rt->nxt[1]->val < 1<<i-1) rt=rt->nxt[1];//,puts("C");
else rt=to(rt,0), res+=(1<<i-1);//,puts("A");
else
if(!rt->nxt[0]) return res;
else if(rt->nxt[0]->val < 1<<i-1) rt=rt->nxt[0];//,puts("D");
else rt=to(rt,1), res+=(1<<i-1);//,puts("B");
// printf("Q %d:%d %d %d %d\n",x,i,res,bit[i],x&bit[i]);
}
return res;
}
}t;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("842.in","r",stdin);
#endif for(int i=1; i<=B; ++i) bit[i] = 1<<i-1;
n=read(),m=read();
for(int i=1; i<=n; ++i) A[i]=read();
std::sort(A+1,A+1+n);
int cnt=1;
for(int i=2; i<=n; ++i)
if(A[i]!=A[i-1]) A[++cnt]=A[i];
n=cnt;
for(int i=1; i<=n; ++i) t.Insert(A[i]);
int x=0;
while(m--)
x^=read(), printf("%d\n",t.Query_Mex(x)); return 0;
}

Codeforces.842D.Vitya and Strange Lesson(Trie xor)的更多相关文章

  1. codeforces 842D Vitya and Strange Lesson

    题目大意: 定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex Input First line contains two i ...

  2. CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)

    给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...

  3. Codeforces Round #430 (Div. 2) Vitya and Strange Lesson

    D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...

  4. 【cf842D】Vitya and Strange Lesson(01字典树)

    D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...

  5. Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点

    题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...

  6. Codeforces Round #430 D. Vitya and Strange Lesson

    Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of numbers is ...

  7. D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...

  8. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  9. 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson

    [链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...

随机推荐

  1. ubuntu下tensorflow 报错 libcusolver.so.8.0: cannot open shared object file: No such file or directory

    解决方法1. 在终端执行: export LD_LIBRARY_PATH=”$LD_LIBRARY_PATH:/usr/local/cuda/lib64” export CUDA_HOME=/usr/ ...

  2. SCons: 替代 make 和 makefile 及 javac 的极好用的c、c++、java 构建工具

    http://scons.org/ https://www.ibm.com/developerworks/cn/linux/l-cn-scons/index.html 后附:另外,WAF是一个基于sc ...

  3. Innodb ,MyISAM

    1. InnoDB不支持FULLTEXT类型的索引. 2. InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算 ...

  4. 在docker中部署centos7镜像

    本篇文章参考自: https://www.cnblogs.com/linjj/p/5606911.html https://blog.csdn.net/u012767761/article/detai ...

  5. (并发编程)线程 (理论-创建-lock-属性-守护,与进程的对比)

    一.线程理论1.什么是线程   线程指的是一条流水线的工作过程(执行代码)   进程不是执行单位,是资源单位   一个进程内自带一个线程,线程是执行单位 2.进程VS线程    1.同一进程内的线程们 ...

  6. spring整合strus2的Hellowworld

    比较笨,看了三遍才能理解敲对并正确运行: step: 1.建立web工程( Dynamic Web project)一定要勾上创建web.xml 2.导入jar包 这个就比较坑了,我查了有半个小时才查 ...

  7. vue系列之vue-resource

    vue-resource是Vue.js的一款插件,它可以通过XMLHttpRequest或JSONP发起请求并处理响应.也就是说,$.ajax能做的事情,vue-resource插件一样也能做到,而且 ...

  8. OCM_第十七天课程:Section7 —》GI 及 ASM 安装配置 _管理和配置 GRID /实施 ASM 故障组 /创建 ACFS 文件系统

    注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...

  9. 用Python实现Excel的读写

    一.读excel文件的简单示例 #!/usr/bin/env python # -*- coding:utf-8 -*- import xlrd from xlrd.book import Book ...

  10. HRBUST - 1818 石子合并 区间dp入门

    有点理解了进阶指南上说的”阶段,状态和决策“ /* 区间dp的基础题: 以区间长度[2,n]为阶段,枚举该长度的区间,状态dp[l][r]表示合并区间[l,r]的最小费用 状态转移方程dp[l][r] ...