Codeforces Round #430 (Div. 2) D

5 篇文章 0 订阅
2 篇文章 0 订阅
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





考场上想出来了。。。无奈被C卡了太久,打不完GG。。。。


首先,那个数组变与不变是没有多大区别的。。。因为异或满足结合律,所以我们可以把每一次操作都异或成一个操作,这样来看数组就是不变的。。


因为二进制中如果最高位取1,后面怎么取都不会比最高位取0来得小,所以我们可以考虑贪心一下

对于答案的每一位数尽量地去取0,这样得到的答案就是最优的


这样的操作可以用一个trie优化一把


把所有原序列中没有出现过的数插入到trie中,直接回答询问即可


代码:

#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;

typedef long long LL;
typedef double DL;

const int INF = 2147483647;
const int maxn = 600100;
const int maxtrie = 12001000;

int n,m,x,rt,p[25],a[25];
int siz[maxtrie],ch[maxtrie][2],tot;
bool exist[maxn];

inline LL getint()
{
	LL ret = 0,f = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		ret = ret * 10 + c - '0',c = getchar();
	return ret * f;
}

inline int gcd(int a,int b)
{
	return !b ? a : gcd(b,a % b);
}

inline void insert(int &o,int *T,int i)
{
	if (!o) o = ++tot;
	siz[o]++;
	if (i == 20) return;
	insert(ch[o][T[i + 1]],T,i + 1);
}

inline void find(int o,int *T,int i)
{
	if (i == 20) return;
	if (T[i + 1])
	{
		if (siz[ch[o][1]]) 
		{
			a[i + 1] = 0;
			find(ch[o][1],T,i + 1);
		}
		else
		{
			a[i + 1] = 1;
			find(ch[o][0],T,i + 1);
		}
	}
	else
	{
		if (siz[ch[o][0]]) 
		{
			a[i + 1] = 0;
			find(ch[o][0],T,i + 1);
		}
		else
		{
			a[i + 1] = 1;
			find(ch[o][1],T,i + 1);
		}
	}
}

int main()
{
	n = getint(); m = getint();
	for (int i = 1; i <= n; i++) exist[getint()] = 1;
	for (int i = 0; i <= 600000; i++)
	{
		if (exist[i]) continue;
		int x = i;
		for (int j = 20; j >= 1; j--)
		{
			a[j] = x & 1;
			x >>= 1;
		}
		insert(rt,a,0);
	}
	x = 0;
	for (int i = 1; i <= m; i++)
	{
		x = getint() ^ x;
		int test;
		
		int s = x;
		for (int j = 20; j >= 1; j--)
		{
			p[j] = s & 1;
			s >>= 1;
		}
		find(rt,p,0);
		int ret = 0;
		for (int j = 20; j >= 1; j--)
			ret += a[j] ? 1 << (20 - j + 1 - 1) : 0;
		printf("%d\n",ret);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值