Codeforces 842 D. Vitya and Strange Lesson (trie)
来源:互联网 发布:12海里知乎 编辑:程序博客网 时间:2024/06/13 23:46
Description
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·10^5) — number of elements in array and number of queries.
Next line contains n integer numbers ai (0 ≤ ai ≤ 3·10^5) — elements of then array.
Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·10^5).
Output
For each query print the answer on a separate line.
Sample Input
5 40 1 5 6 71145
Sample Output
2202
题意
给出长度为 n 的非负整数序列,求该序列异或 x 以后的 mex 值。
思路
将所有的数字以二进制的方式插入到 trie 树中,然后我们便可以很方便的求出一个序列的 mex 值。
假如要全局异或一个数 x ,且 x 的二进制从高到低第 i 位是 1 ,则 trie 树中的第 i 层所有节点都要翻转左右孩子。
AC 代码
#include <bits/stdc++.h>using namespace std;const int maxn = 1e7+10;const int maxm = 20;int n,m,tot;int a[maxn];struct node{ int cnt,res; node *next[2];} pn[maxn],*root;void insert(int x) //trie树构建{ node *p=root; p->cnt++; for(int i=maxm; ~i; i--) { int k=(x>>i)&1; if(!p->next[k])p->next[k]=&pn[tot++]; p=p->next[k]; p->cnt++; }}void push_down(node *p,int d) //异或时反转左右孩子{ for(int i=0; i<2; i++) if(p->next[i]) p->next[i]->res^=p->res; if((p->res>>d)&1) swap(p->next[0],p->next[1]); p->res=0;}void solve(){ int ans=0; node *p=root; for(int i=maxm; ~i; i--) { push_down(p,i); if((p->next[0]?p->next[0]->cnt:0)!=(1<<i)) //寻找mex p=p->next[0]; else p=p->next[1],ans|=1<<i; if(!p)break; } cout<<ans<<endl;}int main(){ cin>>n>>m; for(int i=0; i<n; i++) cin>>a[i]; sort(a,a+n); n = unique(a,a+n)-a; root = &pn[tot++]; for(int i=0; i<n; i++) insert(a[i]); for(int i=0; i<m; i++) { int x; cin>>x; root->res^=x; solve(); } return 0;}
- Codeforces 842 D. Vitya and Strange Lesson (trie)
- codeforces 842D. Vitya and Strange Lesson
- Codeforces 842D Vitya and Strange Lesson
- Codeforces 842 D Vitya and Strange Lesson
- CodeForces 842D Vitya and Strange Lesson
- CF 842D Vitya and Strange Lesson 01Trie(mex)
- Codeforces 842 D Vitya and Strange Lesson 线段树
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson [trie]
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson(01Trie)
- Codeforce#430D.Vitya and Strange Lesson(01Trie)
- Codeforces 842 D Vitya and Strange Lesson 线段树 (未理解透)
- D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- [Codeforces 842D Vitya and Strange Lesson]异或字典树
- Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】
- 线性结构
- spring02 ioc
- TLS回调函数
- SVN 将文件还原到之前的指定版本
- android 内存泄露
- Codeforces 842 D. Vitya and Strange Lesson (trie)
- 关于网络(同步、异步、阻塞、非阻塞,select/poll/epoll,rpc/msgqueue,tcpip常见面试题)
- 中文字节长度引起的数据丢失
- CentOS7中如何设置网络自启动
- 健康生活小常识
- Netty 实现 WebSocket 聊天功能
- D37 python基础
- 奋战聊天机器人(二)语料和词汇资源
- Git生成公钥命令