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
2 2
1 3
1
3
1
0
4 3
0 1 5 6
1
2
4
2
0
0
5 4
0 1 5 6 7
1
1
4
5
2
2
0
2
题意:
找出来这n个数中没有的最小自然数
之后有m次对数组的处理(设m行每行输入的数为x),让数组里面的所有数都和x异或。然后输出这n个数中没有的最小自然数
注释+代码:
1 /*
2 将所有的数字以二进制的方式插入到 trie 树中,然后我们便可以很方便的求出一个序列的 mex 值。
3 假如要全局异或一个数 x ,且 x 的二进制从高到低第 i 位是 1 ,则 trie 树中的第 i 层所有节点都要翻转左右孩子。
4
5 为什么遇到1要翻转,遇到0不需要?
6 如果是0的话,那么原来数是0,异或之后结果还是0;如果原来数是1,异或之后结果还是1
7
8 并且x^y^z=x^(y^z)
9 */
10 #include <iostream>
11 #include <cstdio>
12 #include <cstring>
13 #include <cstdlib>
14 #include <algorithm>
15 using namespace std;
16 typedef long long ll;
17 const int maxn=20;
18 const int mod=998244353;
19 const int N=3e5+10;
20 typedef struct Trie* TrieNode;
21 int v[N];
22 struct Trie
23 {
24 int val,sum;
25 TrieNode next[2];
26 Trie()
27 {
28 val=0;
29 sum=0;
30 memset(next,NULL,sizeof(next));
31 }
32 };
33 void inserts(TrieNode root,int x)
34 {
35 TrieNode p = root;
36 for(int i=maxn;i>=0;--i)
37 {
38 int temp=(x>>i)&1;
39 if(p->next[temp]==NULL) p->next[temp]=new struct Trie();//printf("*%d*",temp);
40 //p->next[temp]->sum+=1;
41 p=p->next[temp];
42 p->sum+=1;
43 }
44 }
45 void update(TrieNode p,int x)
46 {
47 for(int i=0;i<2;++i)
48 if(p->next[i]!=NULL)
49 p->next[i]->val^=p->val; //这个p->next[i]->val为什么要异或?
50 if((p->val>>x)&1) //因为我们每一次只能走字典树的一个方向,所以另一个方向是没有异或过这个x的,所以报
51 swap(p->next[0],p->next[1]); //保留下来,和线段树的懒惰标记差不多
52 p->val=0;
53 }
54 void query(TrieNode root,int x)
55 {
56 int ans=0;
57 TrieNode p=root;
58 for(int i=maxn;i>=0;i--)
59 {
60 update(p,i);
61 if(p->next[0]==NULL || p->next[0]->sum!=(1<<i)) //(1<<i)代表另一个树枝上所有数都存在时的个数
62 p=p->next[0]; //因为咱们时从二进制数的高位枚举到低位,所以遇到能走的零就要走
63 else p=p->next[1],ans|=1<<i;
64 if(p==NULL) break;
65 }
66 printf("%d\n",ans);
67 }
68 void Del(TrieNode root)
69 {
70 for(ll i=0 ; i<2 ; ++i)
71 {
72 if(root->next[i])Del(root->next[i]);
73 }
74 delete(root);
75 }
76 int main()
77 {
78 int n,m;
79 scanf("%d%d",&n,&m);
80 for(int i=0;i<n;++i)
81 scanf("%d",&v[i]);
82 sort(v,v+n);
83 TrieNode root=new struct Trie();
84 inserts(root,v[0]);
85 for(int i=1;i<n;++i)
86 {
87 if(v[i]!=v[i-1])
88 inserts(root,v[i]);
89 }
90 while(m--)
91 {
92 int x;
93 scanf("%d",&x);
94 root->val=x; //每一次都要
95 query(root,x);
96 }
97 Del(root);
98 return 0;
99 }
Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点的更多相关文章
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)
给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...
- 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson
[链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...
- E - Petya and Exam CodeForces - 832B 字典树+搜索
E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...
- Watto and Mechanism CodeForces - 514C (字典树,哈希)
大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...
随机推荐
- (二)React Ant Design Pro + .Net5 WebApi:前端环境搭建
首先,你需要先装一个Nodejs,这是基础哦.如果没有这方面知识的小伙伴可以在园子里搜索cnpm yarn等关键字,内容繁多,此不赘述,参考链接 一. 简介 1. Ant Design Pro v5 ...
- Payment Spring Boot 1.0.4.RELEASE 发布,最易用的微信支付 V3 实现
Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 欢迎ISSUE,欢迎 ...
- Springboot之文件监控
背景:在实际环境部署构成中,由于特殊网络环境因素,有很多服务器之间的网络都是单向的,不能互相访问的,只有通过特定技术手段做到文件的单项摆渡,这就需要在两台服务器上分别写序列化程序和反序列化程序,这里不 ...
- postgresql中权限介绍
postgresql权限分为实例的权限,数据库的权限,模式的权限,对象的权限,表空间的权限 实例的权限:由pg_hba.conf文件控制,控制那些用户那些IP以哪种方式连接数据库 数据库的权限:是否允 ...
- 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限
目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ...
- 1.5V转3V电源芯片,1.5V转3V稳压芯片
1.5V干电池的供电电压一般是0.9V-1.6V左右,因为供电电压不稳,所以需要1.5V转3V的稳压电源芯片,当0.9V-1.6V输入电压时,输出电压能稳定3V输出,给模块供电,MCU供电,LED灯供 ...
- 都知道Base64,Base32你能实现吗?
很长时间没有更新个人博客了,因为前一段时间在换工作,入职了一家新的公司,刚开始需要适应一下新公司的节奏,开始阶段也比较忙.新公司还是有一定的技术气氛的,每周都会有技术分享,而且还会给大家留一些思考题, ...
- JMETER-正则表达式提取与查看变量是否提取正确
一.应用场景说明: 在一个线程组中,B请求需要使用A请求返回的数据,也就是常说的关联,将上一个请求的响应结果作为下一个请求的参数,则需要对A请求的响应报文使用后置处理器,其中最方便最常用的就是正则表达 ...
- 【Soul网关探秘】http数据同步-Admin通知前处理
引言 本篇开始研究 Soul 网关 http 数据同步,将分为三篇进行分析: <Admin通知前处理> <变更通知机制> <Bootstrap处理变更通知> 希望三 ...
- Promise用法
1.概述 Promise是一步编程的一种解决方案,从语法上讲,promise是一个对象,从它可以获取异步的问题 Promise的优点: 可以避免多次异步调用嵌套导致的回调地域 提供了简洁的api,使得 ...