codeforces 842D Trie 树异或不存在的最小值
来源:互联网 发布:photoshop mac安装 编辑:程序博客网 时间:2024/04/29 21:23
D. Vitya and Strange Lesson
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
题意:n个数,m个询问,问异或后不存在的最小的数
逆向思维
每一个数异异或一个数的值都是不一样的,第一种想法是把不存在的数加到字典树上,然后找异或到的最小值,得开 21*(1<<20)的内存有点多了。。
#include <bits/stdc++.h>using namespace std;const int N = (1<<20);const int BB=22;struct node{ int nxt[2];}T[N*BB+1];int mp[(1<<20)];typedef long long ll;int sz;void insert(int d){ int p=0; for(int i=21;i>=0;i--) { int dd=(d>>i)&1; if(!T[p].nxt[dd]) T[p].nxt[dd]=++sz; p=T[p].nxt[dd]; }}int go[32];int ans;void ask(){ int p=0; for(int i=21;i>=0;i--) { int dd=go[i]; if(T[p].nxt[dd]) p=T[p].nxt[dd]; else { p=T[p].nxt[1-dd],ans|=(1<<i); } }}int main(){ memset(T,0,sizeof(T)); int n,m; scanf("%d%d",&n,&m); sz=0; for(int i=1;i<=n;i++) { int d; scanf("%d",&d); mp[d]=1; } for(int i=0;i<=(1<<20);i++) { if(!mp[i]) insert(i); } int t=0; while(m--){ int x; scanf("%d",&x); t^=x; ans=0; for(int j=21;j>=0;j--) { int dd=(t>>j)&1; go[j]=dd; } ask(); printf("%d\n",ans); }}
第二种方法是判断子树是不是满的
往不满的子树走,异或同方向的优先
重复的点要删除
#include <bits/stdc++.h>using namespace std;const int N = 300010;const int BB=22;struct node{ int nxt[2];}T[N*BB+1];int size[N*BB+1];typedef long long ll;int sz;int go[32];int ask(){ int p=0,ans=0; for(int i=21;i>=0;i--) { int dd=go[i]; if(!T[p].nxt[dd]) return ans; if(size[T[p].nxt[dd]]<(1<<i)) p=T[p].nxt[dd]; else { ans|=(1<<i); p=T[p].nxt[1-dd]; if(!p) return ans; } } return ans;}void insert(int d){ int p=0; for(int i=21;i>=0;i--) { int dd=(d>>i)&1; if(!T[p].nxt[dd]) T[p].nxt[dd]=++sz; p=T[p].nxt[dd]; size[p]++; } if(size[p]>1) { p=0; for(int i=21;i>=0;i--) { int dd=(d>>i)&1; p=T[p].nxt[dd]; size[p]--; } }}int main(){ memset(T,0,sizeof(T)); memset(size,0,sizeof(size)); int n,m; scanf("%d%d",&n,&m); sz=0; for(int i=1;i<=n;i++) { int d; scanf("%d",&d); insert(d); } int tt=0; while(m--){ int x; scanf("%d",&x); tt^=x; for(int j=21;j>=0;j--) { int dd=(tt>>j)&1; go[j]=dd; } printf("%d\n",ask()); }}
- codeforces 842D Trie 树异或不存在的最小值
- codeforces 706D trie树 或 muliset
- Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】
- Codeforces 706D Trie树/multiset
- Codeforces 842 D. Vitya and Strange Lesson (trie)
- Codeforces Round #367 (Div. 2) D Trie树
- CodeForces 367D Vasiliy's Multiset Trie树
- Codeforces 706D-Vasiliy's Multiset(Trie树)
- 异或树 异或+二进制Trie
- [Codeforces 842D Vitya and Strange Lesson]异或字典树
- 【JDFZOJ】最富有的人 Trie树+异或性质
- Codeforces 271D Good Substrings 暴力+Trie
- codeforces 76 D. Plus and xor相加或异或
- 【bzoj3689】【异或之】【trie树+堆】
- [Trie树] BZOJ3689: 异或之
- SQL1051N 路径 "D:" 不存在或无效。 db2 备份镜像文件异机备份与恢复
- codeforces D. Mishka and Interesting sum 求区间内不同数的异或值
- 论可持久化Trie树贪心在区间最大最小值的运用
- Java 线程池ThreadPoolExecutor(基于jdk1.8)(二)
- WEB_02_JSP/Servlet及相关技术详解
- Linux下实现mysql主从数据库读写分离
- FreeType使用的总结
- 修改网页的alert弹框样式让你的页面更美观(js封装之路(1.1))
- codeforces 842D Trie 树异或不存在的最小值
- java强引用、软引用、弱引用、虚引用
- C# 实践1——与数据库相关的窗体
- 构建我的第一个react项目
- 超星尔雅生态文明撑起美丽中国梦答案
- 校验手机号正则表达式
- opengl官网
- C语言中的system函数
- 关于mysql数据库手机号替换*