Today at the lesson Vitya learned a very interesting function — mex. Mex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0.

Vitya quickly understood all tasks of the teacher, but can you do the same?

You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps:

Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x.

Find mex of the resulting array.

Note that after each query the array changes.

Input

First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries.

Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array.

Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105).

Output

For each query print the answer on a separate line.

题目大意:

定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex

解题报告:

考场上搞了一个小时,原来看错题了,其实只是简单的Trie树基本操作,

mex数:如果左子树没满直接走左子树,不然就走右子树.

异或操作:如果x的该位为1,交换该节点的左右子树,打上标记即可

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=6e6+10,maxdep=21;
int gi(){
int str=0;char ch=getchar();
while(ch>'9' || ch<'0')ch=getchar();
while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
return str;
}
struct node{
int l,r,s,rev;
}t[N];
int n,root=0,tot=0,w[30],m;
void insert(int &rt,int x,int d){
if(!rt)rt=++tot;
if(d==-1){
t[rt].s=1;return ;
}
if(x&w[d])insert(t[rt].r,x,d-1);
else insert(t[rt].l,x,d-1);
t[rt].s=t[t[rt].l].s&t[t[rt].r].s;
}
void pushdown(int rt,int d){
if(!t[rt].rev)return ;
int k=t[rt].rev;
t[t[rt].l].rev^=k;t[t[rt].r].rev^=k;
if(d>=1 && (k&w[d-1])){
swap(t[t[rt].l].l,t[t[rt].l].r);swap(t[t[rt].r].l,t[t[rt].r].r);
}
t[rt].rev=0;
}
int query(int rt,int d){
if(d==-1)return 0;
pushdown(rt,d);
if(!t[t[rt].l].s)return query(t[rt].l,d-1);
return query(t[rt].r,d-1)+w[d];
}
void work()
{
int x;
n=gi();m=gi();
w[0]=1;for(int i=1;i<=maxdep;i++)w[i]=w[i-1]<<1;
for(int i=1;i<=n;i++){
x=gi();insert(root,x,maxdep);
}
while(m--){
scanf("%d",&x);
t[root].rev^=x;
printf("%d\n",query(root,maxdep));
}
} int main()
{
work();
return 0;
}

Codeforces Round #430 D. Vitya and Strange Lesson的更多相关文章

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

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

  2. Codeforces Round #430 (Div. 2) 【A、B、C、D题】

    [感谢牛老板对D题的指点OTZ] codeforces 842 A. Kirill And The Game[暴力] 给定a的范围[l,r],b的范围[x,y],问是否存在a/b等于k.直接暴力判断即 ...

  3. 【Codeforces Round #430 (Div. 2) A C D三个题】

    ·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意:    给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...

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

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

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

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

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

    因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...

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

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

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

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

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

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

随机推荐

  1. 获取android项目的数据库地址或者数据库名

    你不需要知道该路径.只是使用数据库,你可以将它们删除的列表. for (String databaseName : context.databaseList()) { context.deleteDa ...

  2. Oracle RAC环境下定位并杀掉最终阻塞的会话

    实验环境:Oracle RAC 11.2.0.4 (2节点) 1.模拟故障:会话被级联阻塞 2.常规方法:梳理找出最终阻塞会话 3.改进方法:立即找出最终阻塞会话 之前其实也写过一篇相关文章: 如何定 ...

  3. 分贝块---dBblock

    分贝,用英语来表达的话,是decibel,是量度两个相同单位之数量比例的计量单位,主要用于度量声音强度,常用dB表示. 块,block,在百度百科中,指数据库中的最小存储和处理单位,包含块本身的头信息 ...

  4. 阿里云API网关(16)客户端请求的https支持

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  5. OAuth是什么?

    一.OAuth的概念 1.问题的提出 2.应用场景 3.规范演进 二.OAuth的运行原理 1.参与者 访问私有数据需要用户参与(客户.用户.服务提供者) 访问公共数据不需要用户参与(客户.服务提供者 ...

  6. Python 自动化 第一周

    1.Python简介 1.1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时 ...

  7. windows计划任务

    前段时间写了一个小工具,实现两个数据库的数据同步. 需求:要求每天的某个时间自动同步数据 功能写好之后,发现了windows一个自带的功能不错,可以实现我的需要,不用写定时器触发了 控制面板-> ...

  8. 刨析Maven(对pom.xml配置文件常用标签的解析)

    昨天在阿里云看到了一句话,"当你Learning和Trying之后,如果能尽量把Teaching也做好,会促进我们思考".共勉! 这是关于Maven的第三篇博客,这次我们深入了解p ...

  9. Spark-1.X编译构建及配置安装

    前提条件(环境要求) jdk版本:1.7+ scala版本:1.10.4+ maven版本:3.3.3+ 本博客中使用的软件版本 spark版本:spark-1.6.1.tar.gz(源码) jdk版 ...

  10. 2017年Unity游戏开发视频教程(入门到精通)

    本文是我发布的一个Unity游戏开发的学习目录,以后我会持续发布一系列的游戏开发教程,都会更新在这个页面上,适合人群有下面的几种: 想要做独立游戏的人 想要找游戏开发相关工作的人 对游戏开发感兴趣的人 ...