Codeforces.842D.Vitya and Strange Lesson(Trie xor)
/*
异或只有两种情况,可以将序列放到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)的更多相关文章
- codeforces 842D Vitya and Strange Lesson
题目大意: 定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex Input First line contains two i ...
- CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)
给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- 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 ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson
[链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...
随机推荐
- phantomjs 中如何使用xpath
function getNodeInfo(inputcsvPath) { var htmlnodeInfo = page.evaluate(function () { //_Ltg var XPATH ...
- linux下混杂模式
混杂模式介绍: 混杂模式就是接收所有经过网卡的数据包,包括不是发给本机的包,默认情况下网卡只把发给本机的包(包括广播包)传递给上层程序,其它的包一律丢弃:简单的讲,混杂模式就是指网卡能接受所有通过它的 ...
- 解决maven编译Java中的使用了未经检查或不安全的操作
eclipse编译器找到右侧黄色点击可以定位到对应代码块位置 解决方法:鼠标悬浮在上方点击add即可 @SuppressWarnings("unchecked");给出的解决方案 ...
- Android 自定义View二(深入了解自定义属性attrs.xml)
1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...
- 移动端适配js
第一种.参考网易的 (我现在用这个多) https://www.cnblogs.com/well-nice/p/5509589.html var deviceWidth = document.docu ...
- vue系列之概念
一.模板引擎 通常出现在应用层,即服务器端(MVC层中的view) 客户端HTTP请求->应用层的控制器(Controller)->应用层的服务层(Service,访问数据库)->封 ...
- 纯 Java 开发 WebService 调用测试工具(wsCaller.jar)
注:本文来自hacpai.com:Tanken的<纯 Java 开发 WebService 调用测试工具(wsCaller.jar)>的文章 基于 Java 开发的 WebService ...
- 测试开发之前端——No7.HTML5中的鼠标事件
鼠标事件 由鼠标或相似的用户动作触发的事件. 适用于所有 HTML 5 元素: 属性 值 描述 onclick script 当单击鼠标时运行脚本 ondblclick script 当双击鼠标时运行 ...
- LeetCode(27): 移除元素
Easy! 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1 ...
- html----常见的标签
HTML标签: <strike>为文字加上一条中线</strike> <em>: 文字变成斜体.</em> 2<sup>2</sup& ...