题目链接: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. tp32-layuicms项目介绍

    项目结构:  项目截图: 登录页 文章列表 码云仓库:https://gitee.com/lim2018/tp32-layuicms

  2. swagger2简单使用

    1.引入jar <!--引入swagger--> <dependency> <groupId>io.springfox</groupId> <ar ...

  3. 启动Maven项目时报错Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project **-web: Failed to clean project: Failed to delete E:\**\target\tomcat\logs\access_lo

    这类错误 出现这种错误,通常是由于您已启动了另一个tomcat 进程或者运行的javaw.exe进程,导致报错. 解决方法: 1. 鼠标点击 X 进行关闭运行失败的 Console页,(如果运行多次, ...

  4. LeetCode_1114.按顺序打印(多线程)

    LeetCode_1114 LeetCode-1114.按顺序打印 我们提供了一个类: public class Foo { public void one() { print("one&q ...

  5. 【Win32 API】远程工具调用

    前言 有时候,影城报障需要远程过去重现和处理,如果电脑没有安装远程工具的话,还需要营业员下载和安装,然后将账号密码发送过来,这样一来一回操作繁琐也浪费时间,所以我们可以设想一下这种场景,售票员点击在p ...

  6. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_3 Mybatis的CRUD-修改和删除操作

    增加更新操作 更新用户的配置 测试类 删除的操作 这里的parpameterType值可以是:Integer.INTEGER.int.java.lang.Integer 讲到typeAliases标签 ...

  7. Spring 初识

    一.Spring是什么? 首先可以进入Spring官网 https://spring.io/ 看一下相关介绍. Spring为开发者提供了一站式的轻量级应用开发平台.简单来说,Spring为开发者提供 ...

  8. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  9. STM32 USB开发(三) 基于F105RBT6核心板开发的自定义HID收发(FS)

    硬件设计 该核心板的USB插口有两个,一个是用于USB Slave的,可以用来做HID设备,把模拟STM32模拟为U盘等:另一个是USB Host设备,可以对插上的U盘的数据进行读写. 图中J2是Mi ...

  10. python的浅复制,深复制

    1.a = b是将b的id复制给b,然后a与b指向同一个对象 import numpy as np a = np.arange(5) print(a) b = a print(id(a)) print ...