【Codeforces Round #430 (Div. 2) D】 D. Vitya and Strange Lesson ("带lazy" 的字典树)

D. Vitya and Strange Lesson
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today at the lesson Vitya learned a very interesting function — mexMex 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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 19;
int ch[maxn * 2][2], tot, cnt[maxn];
void insert(int x){
	int k = 0, y;
	for(int i = 22; i >= 0; --i){
		y = (x >> i) & 1;
		if(ch[k][y] == 0){
			ch[k][y] = ++tot;
		}
		k = ch[k][y];
	}
}
int query(int x){
	int k = 0, y, ans = 0;
	for(int i = 22; i >= 0; --i){
		y = (x >> i) & 1;
		if(y == 0){
			if(ch[k][0] == 0){
				y = 1;
				ans |= 1 << i;
			}
		}
		else{
			if(ch[k][1] == 0){
				y = 0;
				ans |= 1 << i;
			}
		}
		k = ch[k][y];
	}
	return ans;
}
int main(){
	int n, m, x;
	scanf("%d %d", &n, &m);
	memset(cnt, 0, sizeof(cnt));
	memset(ch, 0, sizeof(ch));
	tot = 0;
	for(int i = 1; i <= n; ++i){
		scanf("%d", &x);
		cnt[x] = 1;
	}
	for(int i = 0; i < maxn; ++i){
		if(cnt[i] == 0){
			insert(i);
		}
	}
	int lazy = 0;
	for(int i = 1; i <= m; ++i){
		scanf("%d", &x);
		lazy ^= x;
		printf("%d\n", query(lazy));
	}
}

/*
题意:定义mex是一个序列中最小未出现的非负整数。对于一个长度为
3e5的序列,一共3e5次操作,每次给定一个数x,让序列中所有数异或x,
然后求新序列的mex。

思路:对原数列中未出现的数按数位从高到低建Trie,在Trie上找最小的
数是很方便的。对于异或操作,实际上就是对这棵Trie上的所有点的值
进行异或,0为1,1为0。但是这样复杂度太高,实际上我们不需要对整棵树
做异或操作,我们用一个变量维护这个值,遍历Trie时,如果该值的某位是1,
说明结点在此处进行了异或操作,每次操作我们对该变量进行异或即可。
那么我们在用Trie求mex时,如果某个结点所在位的下一位没有进行异或操作,
我们就优先访问0结点,进行了异或操作我们就优先访问1结点。

注意:在建树的时候,未出现的数的上限要大一点,不然可能会出现某结点
应该有1结点而未建的情况,导致答案错误。
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值