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, ...
随机推荐
- Android TextView跑马灯
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- C# 自定义颜色
一.需要引用 using System.Windows.Media; 二. 自定义颜色 通过自定义 RGB 的值来达到自定义颜色的目的 Color _Mycolor = Color.FromRgb(5 ...
- css中的定位属性position(转)
css中的定位属性position 同样的也是上课的时候发现学生难以理解的一些问题拿出来记录一下,希望帮助初学者. 在css中定位属性position的运用在页面中是很常用的,特别是一些结合js来 ...
- [原创]SQL表值函数:获取从当月计算起往前自定义月份数
今天我现在发现看一篇博文不能够太长,只要能够描述清楚自己想表达的东西,能够让大家知道你要讲什么就行了.因为我今天看了一些长篇博文,真的觉得知识点太多了, 会让人囫囵吞枣. 这篇博文跟我昨天发表的类 ...
- Learning Python 008 正则表达式-003 search()方法
Python 正则表达式 - search()方法 findall()方法在找到第一个匹配之后,还会继续找下去,findall吗,就是找到所有的匹配的意思.如果你只是想找到第一个匹配的信息后,就不在继 ...
- <c和指针>学习笔记5动态内存分配和预处理器
1 动态内存 比如声明数组得时候,我们需要提前预估数组长度,分配大了浪费,少了就更不好操作了.从而引入动态分配,需要的时候再分配. (1)malloc和free void *malloc(size_t ...
- 剑指offer(65):获取数据流中的中位数
参考 https://blog.csdn.net/u011080472/article/details/51291089 题目描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位 ...
- jquery提供的数据提交方式1
1,ajax提交,部分代码为easyui function editNode(id){ $.ajax({ dataType:'json', async:true, //默认的就是异步提交 type:' ...
- vue 绑定属性(index)
<el-menu-item v-for="item in links" :key="item.id" v-bind:index="item.id ...
- SQL Server事务回滚对自增键的影响
SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...