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;
}

转载于:https://www.cnblogs.com/Yuzao/p/7455071.html

Codeforces Round #430 D. Vitya and Strange Lesson相关推荐

  1. Codeforces Round #727 (Div. 2) F. Strange Array 线段树 + 区间合并 + 排序优化

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的数组,对每个位置iii求一个最大价值,价值计算方式如下:选择一个包含iii的[l,r][l,r][l,r],让后将其拿出来排序,之后价值 ...

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

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

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

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

  4. Codeforces Round #694 (Div. 2) F. Strange Housing (贪心思维)

    F. Strange Housing 题意 有 nnn 个点和 mmm 条边,对点进行染色.要求一条边的两个点不能都染色,并且删除两端都没有染色的边之后,图连通.请给出一种染色方案. 题解 暴力贪心即 ...

  5. Tutorial of Codeforces Round 729 (Div.2) C. Strange Function

    C. Strange Function 题意 定义一个f(x)为最小的正整数使这个正整数不被x整除,给出一个正整数n,1<=n<=1e16,让求 ∑ i = 1 n f ( i ) \su ...

  6. Codeforces Round #697 (Div. 3) G. Strange Beauty 线性筛变形思维

    原题链接 题意: n个数,问删除多少个数之后剩下的数组中的数满足:对于任意一对i,j满足ai%aj==0 || aj%ai ==0. 思路: 题目给的ai只有2e5,很容易想到从ai入手,算出如果当前 ...

  7. Vitya and Strange Lesson (01字典树)

    题意:给定一组数,然后对所有的数进行异或操作m次异或操作,问你每次操作后,没有出现过的最小的非负整数 题解: 首先需要一个性质(ab)c=a(bc); 所以我们没有必要对于所有的数进行异或操作. 我们 ...

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

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

  9. Codeforces Round #694 (Div. 2) E. Strange Shuffle 交互 + 思维分块

    link 题意: nnn个人围成一圈,一开始每个人都有kkk张卡片,每回合n−1n-1n−1个人会给左边⌊x2⌋\left \lfloor \frac{x}{2} \right \rfloor⌊2x​ ...

最新文章

  1. 点击按钮取GridView当前被操作行的数据
  2. cassandra——可以预料的查询,如果你的查询条件有一个是根据索引查询,那其它非索引非主键字段,可以通过加一个ALLOW FILTERING来过滤实现...
  3. mac photoshop install无法安装_MAC安装应用报错:无法打开或文件损坏的处理方法~...
  4. 5种比较流行的Linux发行版
  5. ReportViewer教程(15)-矩阵报表-1
  6. Docker容器引导完整CentOS
  7. .net Api 接口调用 增删改查
  8. HDU1846 Brave Game【巴什博弈】
  9. oracle sql 导入mysql数据库备份_使用PL/SQL连接oracle数据库,并将数据进行导出备份和导入恢复...
  10. Burp Suite 自带浏览器Burp‘s Browser(Chromium)沙盒sandbox问题的解决
  11. 指针的指针(简单易懂)
  12. Shell脚本字符串大小写转换
  13. 软件工程之高质量代码(编码规范)
  14. OFDM和OFDMA区别笔记
  15. java 字符串长度 ascii_将Unicode转换为ASCII而不更改字符串长度(在Java中)
  16. 【前缀和】蓝桥杯试题 k倍区间
  17. 前端利器CodePen和Scrimba了解一下
  18. 最新中国招标投标公共服务平台JS逆向分析
  19. 独立于计算机系统的是 用户模式,第6章 数据库技术基础习题
  20. Python--使用jieba进行分词并计算词权重

热门文章

  1. 数组 spark_spark——rdd常用的转化和行动操作
  2. java关键字_Java关键字
  3. se 类java_Java SE 9:可选的类改进
  4. cocos bubbles_像Messenger Bubbles这样的Android浮动小部件
  5. 让我们在Ubuntu 18.04上加密SSL证书来保护Nginx
  6. 编译运行一个java程序_如何从另一个Java程序编译和运行Java程序
  7. 深入了解如何学好C++编程
  8. truncate(can)
  9. 自如蛋壳被指推高房租背后:爱公寓资金链断裂先例需警醒
  10. ssl客户端与服务端通信的demo