D. Vitya and Strange Lesson
http://codeforces.com/contest/842/problem/D
1、整体的数组是不用变的,比如数组a[]经过一次询问x后,然后再询问y,相当于询问x ^ y ^ a[i]后的mex值
2、假设所求的答案是k,询问的数字是x,那么对于每个元素a[i],有a[i] ^ x != k恒成立。因为k是一个a[i]^x后得到的新数组,一个不存在新数组的数。所以若a[i] ^ x = k,则k不会是答案。
3、两个数相异或的结果是唯一的,即z ^ x 是一个确定值。
那么要求答案k,我肯定能找到一个数b,这个数不属于a[],使得别b ^ x = k
所以就相当于找一个数,异或x,得到的值最小,就是答案。
数b的范围是a[]的补集,因为a[]是3e5,所以补集大小开到1e6就够,不然这题用这个方法是做不了的。据说有一个好方法,但是还没想懂。
为什么最小是答案,因为数b[]异或x的结果肯定不会和a[]异或x的结果相同。证明:
若b[i] ^ x == a[i] ^ x,那么同时异或x,就等于b[i] = a[i]矛盾。
所以数b[]异或x的结果,每一个都是a[]异或x的结果中不存在的数。
那么找最小的那个就当然是答案
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e6 + ;
int vis[maxn];
struct Node {
struct Node *pNext[];
} tree[maxn * ];
int t;
struct Node *create() {
struct Node *p = &tree[t++];
for (int i = ; i < ; ++i) p->pNext[i] = NULL;
return p;
}
void toInsert(struct Node **T, int val) {
struct Node *p = *T;
if (p == NULL) p = *T = create();
for (int i = ; i >= ; --i) {
int id = (( << i) & val) > ;
if (p->pNext[id] == NULL) p->pNext[id] = create();
p = p->pNext[id];
}
}
int ask(struct Node *T, int val) {
struct Node *p = T;
LL ans = ;
for (int i = ; i >= ; --i) {
int id = (( << i) & val) > ;
if (p->pNext[id]) p = p->pNext[id];
else {
if (!p->pNext[!id]) return ans + ( << i);
ans += << i;
p = p->pNext[!id];
}
}
return ans;
}
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
int x;
scanf("%d", &x);
vis[x] = true;
}
struct Node *T = NULL;
for (int i = ; i <= maxn - ; ++i) {
if (!vis[i]) toInsert(&T, i);
// printf("%d\n", i);
}
int haha = ;
for (int i = ; i <= m; ++i) {
int val;
scanf("%d", &val);
haha ^= val;
printf("%d\n", ask(T, haha));
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
// printf("%d\n", 1 << 25);
work();
return ;
}
D. Vitya and Strange Lesson的更多相关文章
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- Codeforces Round #430 D. Vitya and Strange Lesson
Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of numbers is ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...
- codeforces 842D Vitya and Strange Lesson
题目大意: 定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex Input First line contains two i ...
- Codeforces.842D.Vitya and Strange Lesson(Trie xor)
题目链接 /* 异或只有两种情况,可以将序列放到01Tire树上做 在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归:否则向1 (0位置上的数都满了 ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- cf842d Vitya and Strange Lesson
#include <iostream> #include <cstdio> using namespace std; int s[2000005][2], cnt, n, m, ...
随机推荐
- java web项目创建
https://www.cnblogs.com/kangjianwei101/p/5621738.html
- LVS实战1
(一).NAT模式:NAT模型:地址转换类型,主要是做地址转换,类似于iptables的DNAT类型,它通过多目标地址转换,来实现负载均衡:特点和要求: 1.LVS(Director)上面需要双网卡: ...
- wpf 可视化树的注意点
将控件的visibility 设置为collapse时,控件的可视化树并未生成,此时并不能查找到控件内的子控件. 若某控件想对用户不可见,且可以生成可视化树,则需将控件的visibility 设置为h ...
- TS学习之函数
定义函数类型(规定函数参数及函数返回值的类型,若函数没有返回值,则返回类型为null) function add(x: number, y: number): number { return x + ...
- sql 查看表结构
sqlserver 查看表结构 exec sp_help @TableName --得到表信息.字段,索引.constraint. exec sp_pkeys @TableName --得到主键. e ...
- 问题:Oracle long 类型l;结果:oracle里long类型的总结
oracle里long类型的总结 1.LONG 数据类型中存储的是可变长字符串,最大长度限制是2GB. 2.对于超出一定长度的文本,基本只能用LONG类型来存储,数据字典中很多对象的定义就是用LONG ...
- fsck磁盘检查修复
fsck 使用方式 : fsck [-sACVRP] [-t fstype] [--] [fsck-options] filesys [...]说明 : 检查与修复 Linux 档案系统,可以同时检查 ...
- Dialog 自定义使用3(回调点击事件)
1 , Dialog布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- JAVA类,变量的赋值一个小细节,以及static标注变量的意义
在JAVA类中方法外部定义的变量,如果定义的时候没有赋值,那么在方法外也不能对其进行赋值,否则报错,只能放入某一个方法内对其赋值.(为何报错待晚些时候深入查找补充) static标注的变量称为静态变量 ...
- 使用JFileChooser保存文件
--------------------siwuxie095 工程名:TestFileChooser 包名:com.siwuxie095 ...