【链接】点击打开链接


【题意】


给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数。

【题解】


异或的效果是可以累加的,所以不用每次都算出来每一次的a是什么。而只要把前i个询问的x取一下异或和now,然后用异或和对每个ai异或就可以了。
对于这道题.我们需要用字典树来做.
对于一开始的a数组中没有出现的数字,都把它按照位加入到字典树中,最多19位。
原数组a,然后没出现的数字组成数组A.
结论:则我们对原数组进行一次异或操作即a xor 了x,则异或之后的a数组的mex值就是A数组也xor x之后的最小值。
感性的证明:
假设进行了一次异或操作.
则,相当于把每个数字的二进制形式中的一些1翻转成了0,而0翻转成了1,就是x的二进制中为1的那些位置都进行了翻转操作,那么假如数字t没有出现过,则进行了一次异或操作之后,t可能就出现了,而t xor x可能就没有出现了,则我们就直接认为t出现了,而t xor x变成没有出现的了.
可以这么做是因为,如果t xor x也是没有出现的话,它也会对x进行异或操作,则也会认为t是没有出现的,这样之前认为t出现过的错误结论就能得到订正,这样就不会造成错解了,t 和 t xor x还是都没出现的
而如果没有出现的数字里面没有t xor x的话,那就说明原数组里面有t xor x这个数字,则进行异或操作之后还是会有t这个数字,则之前的结论也就对了,而进行了异或操作之后
t xor x也就变成没有出现过的了
也就是说,A里面的数字,xor之后还是没有出现的.且只有这些数字是没有出现的
这样就感性地解释了那个结论.
那么现在的问题就变为,给你一个数字X,然后让你在A里面找一个数字,然后Ai和X的异或值最小.
则我们从高位开始,尽量让Ai的位和X的对应位一样(异或结果就变成0),这样高位就尽量小了。
字典树的作用就在此。

【错的次数】


1

【反思】


反面去考虑问题,从中得到结论.
二进制的问题好多和字典树有关呢。
那个结论挺难想的。

【代码】

  1. /*
  2.  
  3. */
  4. #include <cstdio>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstring>
  8. #include <vector>
  9. #include <map>
  10. #include <queue>
  11. #include <iomanip>
  12. #include <set>
  13. #include <cstdlib>
  14. #include <cmath>
  15. using namespace std;
  16. #define lson l,m,rt<<1
  17. #define rson m+1,r,rt<<1|1
  18. #define LL long long
  19. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  20. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  21. #define mp make_pair
  22. #define pb emplace_back
  23. #define fi first
  24. #define se second
  25. #define ld long double
  26. #define ms(x,y) memset(x,y,sizeof x)
  27. #define ri(x) scanf("%d",&x)
  28. #define rl(x) scanf("%lld",&x)
  29. #define rs(x) scanf("%s",x)
  30. #define rf(x) scnaf("%lf",&x)
  31. #define oi(x) printf("%d",x)
  32. #define ol(x) printf("%lld",x)
  33. #define oc putchar(' ')
  34. #define os(x) printf(x)
  35. #define all(x) x.begin(),x.end()
  36. #define Open() freopen("F:\\rush.txt","r",stdin)
  37. #define Close() ios::sync_with_stdio(0)
  38. #define sz(x) ((int) x.size())
  39. #define ld long double
  40.  
  41. typedef pair<int, int> pii;
  42. typedef pair<LL, LL> pll;
  43.  
  44. //mt19937 myrand(time(0));
  45. //int get_rand(int n){return myrand()%n + 1;}
  46. const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
  47. const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
  48. const double pi = acos(-1.0);
  49. const int N = 19;
  50.  
  51. struct node {
  52. node *ch[2];
  53. int val;
  54. node() { ch[0] = ch[1] = NULL; val = 0; }
  55. } *root;
  56.  
  57. int n, m, a[N + 10];
  58. bool bo[1 << N];
  59.  
  60. void insert(int x) {
  61. node *p = root;
  62. rep2(i, (N-1), 0) {
  63. int temp = (x >> i) & 1;
  64. if (p->ch[temp] == NULL) p->ch[temp] = new node();
  65. p = p->ch[temp];
  66. }
  67. p->val = x;
  68. }
  69.  
  70. int query(int x) {
  71. node *p = root;
  72. rep2(i, (N-1), 0) {
  73. int temp = (x >> i) & 1;
  74. if (p->ch[temp] == NULL)
  75. p = p->ch[temp ^ 1];
  76. else
  77. p = p->ch[temp];
  78. }
  79. return p->val;
  80. }
  81.  
  82. int main() {
  83. //Open();
  84. //Close();
  85. root = new node();
  86. ri(n), ri(m);
  87. rep1(i, 1, n) {
  88. int x;
  89. ri(x);
  90. bo[x] = 1;
  91. }
  92. rep1(i, 0, (1 << N)-1)
  93. if (!bo[i])
  94. insert(i);
  95. int now = 0;
  96. rep1(i, 1, m) {
  97. int x;
  98. ri(x);
  99. now ^= x;
  100. oi(now^query(now)); puts("");
  101. }
  102. return 0;
  103. }

【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson的更多相关文章

  1. 【 Codeforces Round #430 (Div. 2) A 】 Kirill And The Game

    [链接]点击打开链接 [题意] 水题 [题解] 枚举b从x..y看看k*i是不是在l..r之间就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <cstdio> ...

  2. 【Codeforces Round #430 (Div. 2) B】Gleb And Pizza

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 根据圆心到原点的距离这个东西判断一下圆在不在那个环里面就好 [错的次数] 0 [反思] 在这了写反思 [代码] #include <cst ...

  3. 【Codeforces Round #430 (Div. 2) C】Ilya And The Tree

    [链接]点击打开链接 [题意] 给你一棵n个点的树,每个点的美丽值定义为根节点到这个点的路径上的所有权值的gcd. 现在,假设对于每一个点,在计算美丽值的时候,你可以将某一个点的权值置为0的话. 问你 ...

  4. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

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

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

  6. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  7. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  8. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  9. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

随机推荐

  1. shell脚本实现读取一个文件中的某一列,并进行循环处理

    shell脚本实现读取一个文件中的某一列,并进行循环处理 1) for循环 #!bin/bash if [ ! -f "userlist.txt" ]; then echo &qu ...

  2. Angularjs 1 中使用指令绑定点击事件

    项目中,模板中的菜单是jQuery控制的,在Angularjs中就运行不到了,因为菜单项是ng-repeat之后的. 如html <ul id="main-menu"> ...

  3. Css if hack条件语法

    Css if hack条件语法  <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> ...

  4. linux学习(一)-----vm、centos安装

    安装vm和Centos 1)先安装 virtual machine ,vm12 2)再安装 Linux (CentOS 6.8) 3)原理示意图,这里我们画图说明一下 VM 和 CentOS 的关系. ...

  5. springboot核心技术(一)-----入门、配置

    Hello World 1.创建一个maven工程:(jar) 2.导入spring boot相关的依赖 <parent> <groupId>org.springframewo ...

  6. Scrapy下载图片及自定义分类下载路径

    配置下载图片的流程如下 在items中定义两个属性,image_urls 和images .image_urls是用来存储需要下载的图片url链接,列表类型: 当文件下载完成后会把相关下载信息存入im ...

  7. docker和宿主机之间复制文件

    从主机复制到容器sudo docker cp host_path containerID:container_path 从容器复制到主机sudo docker cp containerID:conta ...

  8. videojs使用的常见问题

    1.报错The play() request was interrupted by a new load request 我在动态更换video的url时会报这个错.修改一下原来的代码如下,就正常了 ...

  9. 生成中国地区随机IP

    #随机生成IP 中国区    public function randip($member){        if($member['user_ip']){            if($member ...

  10. chrome屏蔽https广告

    “设置” -> “高级” -> 系统/“打开代理设置” -> “安全” -> “受限站点” -> “添加” 好了