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,交换该节点的左右子树,打上标记即可

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #define RG register
  8. #define il inline
  9. #define iter iterator
  10. #define Max(a,b) ((a)>(b)?(a):(b))
  11. #define Min(a,b) ((a)<(b)?(a):(b))
  12. using namespace std;
  13. const int N=6e6+10,maxdep=21;
  14. int gi(){
  15. int str=0;char ch=getchar();
  16. while(ch>'9' || ch<'0')ch=getchar();
  17. while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
  18. return str;
  19. }
  20. struct node{
  21. int l,r,s,rev;
  22. }t[N];
  23. int n,root=0,tot=0,w[30],m;
  24. void insert(int &rt,int x,int d){
  25. if(!rt)rt=++tot;
  26. if(d==-1){
  27. t[rt].s=1;return ;
  28. }
  29. if(x&w[d])insert(t[rt].r,x,d-1);
  30. else insert(t[rt].l,x,d-1);
  31. t[rt].s=t[t[rt].l].s&t[t[rt].r].s;
  32. }
  33. void pushdown(int rt,int d){
  34. if(!t[rt].rev)return ;
  35. int k=t[rt].rev;
  36. t[t[rt].l].rev^=k;t[t[rt].r].rev^=k;
  37. if(d>=1 && (k&w[d-1])){
  38. swap(t[t[rt].l].l,t[t[rt].l].r);swap(t[t[rt].r].l,t[t[rt].r].r);
  39. }
  40. t[rt].rev=0;
  41. }
  42. int query(int rt,int d){
  43. if(d==-1)return 0;
  44. pushdown(rt,d);
  45. if(!t[t[rt].l].s)return query(t[rt].l,d-1);
  46. return query(t[rt].r,d-1)+w[d];
  47. }
  48. void work()
  49. {
  50. int x;
  51. n=gi();m=gi();
  52. w[0]=1;for(int i=1;i<=maxdep;i++)w[i]=w[i-1]<<1;
  53. for(int i=1;i<=n;i++){
  54. x=gi();insert(root,x,maxdep);
  55. }
  56. while(m--){
  57. scanf("%d",&x);
  58. t[root].rev^=x;
  59. printf("%d\n",query(root,maxdep));
  60. }
  61. }
  62. int main()
  63. {
  64. work();
  65. return 0;
  66. }

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. 2017-2018-1 我爱学Java 第三周 作业

    Team Presentation 团队展示 队员学号 队名 团队项目描述 队员风采 团队首次合照 团队的特色描述 团队初步合作 前两周合作过程中的优缺点 如何改进 团队选题 确立,建立和初步熟悉团队 ...

  2. 将数组写入Plist文件中

    -(void)writeToPlist:(NSArray *)uploadingfiles  Name:(NSString *)name {                  NSMutableArr ...

  3. java从网络中下载图片到本地

    public class imageDownload { public static void main(String[] args) { String url = "http://loca ...

  4. JVM启动参数

    JVM参数的含义 实例见实例分析 参数名称 含义 默认值   -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,J ...

  5. V7000存储数据恢复_底层结构原理拆解及Mdisk磁盘掉线数据恢复方法

    Storwize V7000(也就是我们常说的V7000)是新推出的一款中端存储系统,这款系统的定位虽然在中端,但是Storwize V7000提供有存储管理功能,这一功能以前只有高端存储才拥有(例如 ...

  6. 使用Google 的 gson方式解析json

    gson支持解析的类型还是比较全面的,包括JavaBean,List<JavaBean>,List<String>,Map等,使用起来也是比较方便,下面根据代码示例给出总结: ...

  7. 九、Python发送QQ邮件(SMTP)

    看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...

  8. Python内置函数(36)——reversed

    英文文档: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() meth ...

  9. 源码解析flask的路由系统

    源码解析flask的路由系统 当我们新建一个flask项目时,pycharm通常已经为项目定义了一个基本路由 @app.route('/') def hello_world(): return 'He ...

  10. final类与final方法

    inal---用于类.方法前. final类---不可被继承. final方法---不可被覆盖. final类不能被继承. 如果我们不希望一个类被继承,我们使用final来修饰这个类.这个类将无法被继 ...