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

相关文章

Java数组初始化的五种方式

《Java数组初始化的五种方式》数组是Java中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

Python容器类型之列表/字典/元组/集合方式

《Python容器类型之列表/字典/元组/集合方式》:本文主要介绍Python容器类型之列表/字典/元组/集合方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 列表(List) - 有序可变序列1.1 基本特性1.2 核心操作1.3 应用场景2. 字典(D

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu