题目链接:http://codeforces.com/contest/842/problem/D

题意:定义Mex为一个序列中最小的未出现的正整数,给定一个长度为n的序列,然后有m个询问,每个询问给定一个数x,先对序列每个数与x进行异或运算(^),之后输出当前序列修改完之后的Mex。

思路:因为对于原序列和x异或后,得到的新序列再与y异或相当于原序列与z(z=x^y)进行异或,所以问题就可以转化为对于一个数val,原序列和val进行异或后得到新序列的Mex是多少? 考虑01字典树,先把序列的n个数插入字典树(相同的数只插一次),考虑现在的val=0,那么如果左子树(边权为0的边)没有满,说明Mex在左子树,否则在右子树,直到最后到叶子结点为止,那么答案就是该叶子所代表的权值。  现在考虑val为任意数,对于val的二进制为0的位,按照上面的分析即可,但是对于二进制为1的位,优先走右子树(边权为1的边,因为1^1=0, 1^0=1,所以对于右子树相当于异或后的左子树, 左子树相当于异或后的右子树), 一直到叶子结点为止。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<time.h>
#include<cmath>
#include<sstream>
#include<assert.h>
using namespace std;
typedef long long int LL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = 3e5 + ;
struct Trie{
int val[MAXN << ];
int next[MAXN << ][];
int root, L;
int newnode(){
next[L][] = next[L][] = -; val[L] = ;
return L++;
}
void init(){
L = ; root = newnode(); build(,);
}
void build(int u,int deep){
if (deep == ){
return;
}
next[u][] = newnode();
next[u][] = newnode();
build(next[u][], deep + );
build(next[u][], deep + );
}
void insert(int x){
int now = root;
for (int i = ; i >= ; i--){
int num = ( << i)&x ? : ;
now = next[now][num];
val[now]++;
}
}
int Query(int x){
int now = root, res = ;
for (int i = ; i >= ; i--){
int num = ( << i)&x ? : ;
if (val[next[now][num]] < ( << i)){ //异或后0的边
now = next[now][num];
}
else{ //异或后1的边, 顺便把权值累计
now = next[now][!num];
res |= ( << i);
}
}
return res;
}
}trie;
set<int>se;
int main(){
#ifdef kirito
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int start = clock();
int n, m,val;
while (~scanf("%d%d", &n, &m)){
int prexor = ;
trie.init(); se.clear();
for (int i = ; i <= n; i++){
scanf("%d", &val);
if (!se.count(val)){
trie.insert(val);
}
se.insert(val);
}
for (int i = ; i <= m; i++){
scanf("%d", &val);
prexor ^= val;
printf("%d\n", trie.Query(prexor));
}
}
#ifdef LOCAL_TIME
cout << "[Finished in " << clock() - start << " ms]" << endl;
#endif
return ;
}

Codeforces Round #430 (Div. 2) - D的更多相关文章

  1. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

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

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

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

    地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...

  4. 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.直接暴力判断即 ...

  5. Codeforces Round #430 (Div. 2) - B

    题目链接:http://codeforces.com/contest/842/problem/B 题意:给定一个圆心在原点(0,0)半径为r的大圆和一个圆内的圆环长度d,然后给你n个小圆,问你有多少个 ...

  6. Codeforces Round #430 (Div. 2) - A

    题目链接:http://codeforces.com/contest/842/problem/A 题意:给定l,r,x,y,k.问是否存在a (l<=a<=r) 和b (x<=b&l ...

  7. Codeforces Round #430 (Div. 2)

    A. Kirill And The Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

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

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

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

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

随机推荐

  1. Comparable接口与Comparator接口的比较————Comparator接口详解

    Comparator接口位于:java.util包中. Comparator接口:1. 强行对某个对象的Collection进行整体排序.值得注意的是:Comparator接口可以作为参数传到一些so ...

  2. 理解BFC以及BFC相关布局问题解决

    写页面时会遇到: 子元素float父元素的高度不会撑开; 在布局时,box1and box2,其中box1 float:left,这是box2会在box1下面,(如果文字过多就会形成文字环绕效果),但 ...

  3. 地图服务 纬度、经度对应坐标轴x,y

    记下,供自己参考,中国地区的经纬度,经度大,纬度小 如上海经纬度为:(经度, 纬度)(y, x)(lon, lat) 121.48 31.22 纬度---lat----x轴 经度---lon---y轴

  4. 快速找到oracle的alert日志

    https://jingyan.baidu.com/article/f3ad7d0fe5d31309c3345b9b.html

  5. 一个蒟蒻的解题过程记录——洛谷P1003 铺地毯

    这到题算是我“火线回归”后码的第一道题,病好了心情不错,发篇博客分享一下 目录: ·题目描述 ·题目分析 ·解题思路 ·代码实现 ·总结 ·题目描述: 为了准备一场特殊的颁奖典礼,组织者在会场的一片矩 ...

  6. python-接口开发flask模块(二)全局host配置

    设置全局变量优势很多主要是可以方便修改参数不需要每个代码单独修改,只修改host配置就可以,减少出错率,提高工作效率MYSQL_HOST = 'XXX.XXX.CCC.XXX' MYSQL_PORT ...

  7. Delphi IDE使用的一些主要技巧

    Delphi IDE使用的一些主要技巧 1.查找和替换 (1)<ctrl>+F[1]:选择页“Find”,进行查找,则根据查找方向继续查找.选择页“Findin Files”,则进行该工程 ...

  8. SPA(single page application)

    一.SPA的概述 SPA(single page application)单页面应用程序,在一个完成的应用或者站点中,只有一个完整的html页面,这个页面有一个容器,可以把需要加载的代码片段插入到该容 ...

  9. Servlet生命周期 Servlet获取配置信息 ServletContext

    一.Servlet生命周期 实例化 ——> 初始化 ——>  服务 ——>  销毁 出生:(实例化 然后 初始化)tomcat第一次访问,Servlet就出生(默认情况下) 活着:( ...

  10. 【Linux开发】OpenCV在ARM-linux上的移植过程遇到的问题2---CMAKE配置问题

    实际上这里说的是移植的第一步,下载到源码后,我用的是opencv2.4.9,解压缩,然后可以利用cmake-gui来进行configure配置,这里面需要设置交叉编译的工具链,具体的可以参考[Linu ...