G - Vitya and Strange Lesson(数组-字典树)

2024-03-20 22:38

本文主要是介绍G - Vitya and Strange Lesson(数组-字典树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

G - Vitya and Strange Lesson

 CodeForces - 842D 

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.

Examples

Input

2 2
1 3
1
3

Output

1
0

Input

4 3
0 1 5 6
1
2
4

Output

2
0
0

Input

5 4
0 1 5 6 7
1
1
4
5

Output

2
2
0
2

 

题意 + 思路:

这两篇博客解释的非常好,我就不多说了

https://blog.csdn.net/brazy/article/details/77841433

 

https://blog.csdn.net/xzzf1024/article/details/79181290

 解释:

  主函数里  再次求MEX的时候 和上次的MEX值异或

            num^=x;           //与上次得到的值异或
            printf("%d\n",num^Find_Trie(num));

这相当于再次输入的X与存在字典树的序列异或。(设存在字典树的序列a,则相当于a^x1^x2);

个人理解+代码:

//MEX求数列中未出现过的最小自然数
//X异或任何一位结果不同
//与存在的数异或后 会再次成为这个N数组的数
//求MEX的值 会出现在 不在这N数组的数。
//再次求MEX的时候 和上次的MEX值异或 就可以了
//
#include <map>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1e6;
int trie[5*maxn][2];
int vis[5*maxn];
int rt,tot;
void init()
{mem(vis,0);mem(trie,0);tot=0;
}
void Build_Trie(int y)
{rt=0;for(int i=31; i>=0; i--){int x=(y>>i)&1;if(!trie[rt][x])trie[rt][x]=++tot;rt=trie[rt][x];}vis[rt]=y;
}
int Find_Trie(int y)
{rt=0;int ans=0;for(int i=31; i>=0; i--){int x=(y>>i)&1;    //找到最小的异或值if(!trie[rt][x])rt=trie[rt][x^1];elsert=trie[rt][x];}return vis[rt];
}
map<int,int>mp;
int main()
{int n,m,y;while(~scanf("%d%d",&n,&m)){init();mp.clear();int x;for(int i=0; i<n; i++){scanf("%d",&x);mp[x]=1;}for(int i=0; i<=600005; i++){if(!mp[i])Build_Trie(i);}int num=0;for(int i=0; i<m; i++){scanf("%d",&x);num^=x;           //与上次得到的值异或printf("%d\n",num^Find_Trie(num));}}return 0;
}

 

这篇关于G - Vitya and Strange Lesson(数组-字典树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830991

相关文章

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

Python 常用数据类型详解之字符串、列表、字典操作方法

《Python常用数据类型详解之字符串、列表、字典操作方法》在Python中,字符串、列表和字典是最常用的数据类型,它们在数据处理、程序设计和算法实现中扮演着重要角色,接下来通过本文给大家介绍这三种... 目录一、字符串(String)(一)创建字符串(二)字符串操作1. 字符串连接2. 字符串重复3. 字

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

Java中数组与栈和堆之间的关系说明

《Java中数组与栈和堆之间的关系说明》文章讲解了Java数组的初始化方式、内存存储机制、引用传递特性及遍历、排序、拷贝技巧,强调引用数据类型方法调用时形参可能修改实参,但需注意引用指向单一对象的特性... 目录Java中数组与栈和堆的关系遍历数组接下来是一些编程小技巧总结Java中数组与栈和堆的关系关于

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat