题目链接: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. Python 使用 PyQt5 开发的关机小工具

    前两天简单认识了一下PyQt5,通过练习开发了一款在Window下自定义关机的小工具,代码如下 import os,sys,time from PyQt5 import QtCore,QtWidget ...

  2. SpringMvc中ModelAndView模型的应用

    /** * 目标方法的返回值可以是 ModelAndView 类型. * 其中可以包含视图和模型信息 * SpringMVC 会把 ModelAndView 的 model 中数据放入到 reques ...

  3. VBA通过C#以API方式调用JS脚本函数

    http://www.cnblogs.com/Charltsing/p/JSDotNetAPI.html 在网页采集中,很多时候需要运行网站下载的某个js文件中的函数,以计算Request参数.VBA ...

  4. MySQL——执行计划

    项目开发中,性能是我们比较关注的问题,特别是数据库的性能:作为一个开发,经常和SQL语句打交道,想要写出合格的SQL语句,我们需要了解SQL语句在数据库中是如何扫描表.如何使用索引的: MySQL提供 ...

  5. 从输入url到页面展现的过程

    先看一幅图:(下面的所有图我都进行拉伸压缩了  如果看不到  可以右键复制图片地址 然后到浏览器粘贴查看  不然显示不全图片) mac没有画图软件  不好意思  xmind做的 1. 输入网址   当 ...

  6. Ubuntu - apt 下载源设置为阿里的源

    # 备份 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 切换为阿里的源 echo "deb http:/ ...

  7. springboot 使用外置tomcat启动

    pom.xml  如下 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  8. JS函数的基础部分

    JS函数的基础部分 JS函数的部分: 先看下一段的代码: window.onload = function(){  function test(){   alert("123"); ...

  9. 003-Django创建模版配置流程

    创建一个页面,简单的 templates/新建home.html,首页展示个人信息,发现我们需要四个参数 <!DOCTYPE html> <html lang="en&qu ...

  10. Lesson 4 The double life of Alfred Bloggs

    There are two type of people in the society. People who do manual works can higher payment than peop ...