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, ...
随机推荐
- [转]升级Flash Builder 4.6中的Flash Player版本
Adobe自发布Flash Builder 4.6后,就暂停了Flash Builder新版本的发布.但AIR和FlashPlayer版本仍然保持不断的更新.在下载新的AIRSDK并覆盖到Flash ...
- VIJOS:P1706(舞会)
描述 Arthur公司是一个等级森严的公司,它们有着严格的上司与下属的关系,公司以总裁为最高职位,他有若干个下属,他的下属又有若干个下属,他的下属的下属又有若干个下属……现接近年尾,公司组织团拜活动, ...
- Java应用中使用ShutdownHook友好地清理现场、退出JVM的2种方法
Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jv ...
- altium designer 中的top/bottom solder和top/bottom paste mask
转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/51751936 1.top solder为助焊层,说白一点就是说,有这个层的地方就没 ...
- 十二:JAVA I/O
输入模式 输出模式 ⑴字节输入流:InputStream ⑵字节输出流:Output ...
- 0009_if控制语句
1.if 条件: (判断相等一定注意要用 == 而不是 =) 代码块 else: 代码块 2.if 条件一: 代码块 elif 条件二: 代码块 elif 条件三 ...
- Centos6.5_64位系统下安装Oracle 11g
一.硬件要求 1.内存与Sweap:内存2G(以上),Sweap 2G(以上) 内存: 1-2G 2-16G 16G以上 Sweap: 1.5倍内存 1倍内存 16G 检查:# grep MemTot ...
- Packet for query is too large
数据库:mysql5.6 framework: play framework 1.2.4 近日处理批量数据的insert,update,涉及的保存更新sql大概有18w.我的操作如下: 1)每次取10 ...
- 菜鸟攻城狮3(Holle World)
1.创建一个HolleWorld.java文本文件 2.代码:public class HolleWorld { public static void main(String[] args) { Sy ...
- error C2512: “HelloWorld”: 没有合适的默认构造函数可用
error C2512: "HelloWorld": 没有合适的默认构造函数可用 c++ newbie error C2512: no appropriate default co ...