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, ...
随机推荐
- CF 360 E Levko and Game —— 贪心+最短路
题目:http://codeforces.com/contest/360/problem/E 首先,每条边不是选 \( l[i] \) 就是选 \( r[i] \): 做法就是先把边权都设成 \( r ...
- Django-进阶 分页,中间件
知识预览 分页 中间件 回到顶部 分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Cr ...
- 【转】Ruby on Rails中select使用方法
在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆.常见的有三个..select, select_tag, collection_select(其余的什么sel ...
- numpy和matlab计算协方差矩阵的不同(matlab是标准的,numpy相当于转置后计算)
matlab是标准的,numpy相当于转置后计算 >> x = [2,0,-1.4;2.2,0.2,-1.5;2.4,0.1,-1;1.9,0,-1.2] x = 2.0000 0 ...
- c语言基础 c和指针
句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...
- js中的Number方法
1.Number.toExponential(fractionDigits) 把number转换成一个指数形式的字符串.可选参数控制其小数点后的数字位数.它必须在0~20之间. 例如: documen ...
- xdu2017校赛F
Problem F Dogs of Qwordance Senior Backend R&D Engineers 问题描述 那年夏天,锘爷和杰师傅漫步在知春公园的小道上.他们的妻子.孩子牵 着 ...
- Unity编辑器的扩展:IMGUI
IMGUI 介绍 所有关于 Editor 的相关 UI,包括 Inspector.Hierarchy.Window.Game 视图上动态创建的那些半透明 UI.还有 Scene 视图上可添加的辅助显示 ...
- 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)
- Entity Framework Code-First(4):Simple Code First Example
Simple Code First Example: Let's assume that we want to create a simple application for XYZ School. ...