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的更多相关文章

  1. 【cf842D】Vitya and Strange Lesson(01字典树)

    D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...

  2. Codeforces Round #430 (Div. 2) Vitya and Strange Lesson

    D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...

  3. 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 ...

  4. Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点

    题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...

  5. Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson

    因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...

  6. codeforces 842D Vitya and Strange Lesson

    题目大意: 定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex Input First line contains two i ...

  7. Codeforces.842D.Vitya and Strange Lesson(Trie xor)

    题目链接 /* 异或只有两种情况,可以将序列放到01Tire树上做 在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归:否则向1 (0位置上的数都满了 ...

  8. D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...

  9. cf842d Vitya and Strange Lesson

    #include <iostream> #include <cstdio> using namespace std; int s[2000005][2], cnt, n, m, ...

随机推荐

  1. 转:删除redis所有KEY

    转自:http://ssuupv.blog.163.com/blog/static/1461567220135610456193/ 批量删除Key Redis 中有删除单个 Key 的指令 DEL,但 ...

  2. OpenStack、CloudStack、Eucalyptus和vCloud Director四大主流云平台怎么选?

    软件产品开发公司Altoros Systems的IT基础设施设计师Vadim Truksha在美国<网络世界>上发表一篇文章,详细对比了CloudStack.Eucalyptus.vClo ...

  3. 面向对象要点(this关键字)

    package day07; public class ThisKeywords { private String name; private void Foo(String name) { this ...

  4. Windchill

    判断某查询栏位是否为空 错误:if (projComp != null && !projComp.equals("")) 正确:if((projComp != nu ...

  5. [poj1273]Drainage Ditches(最大流)

    解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  6. [matlab]Monte Carlo模拟学习笔记

    理论基础:大数定理,当频数足够多时,频率可以逼近概率,从而依靠概率与$\pi$的关系,求出$\pi$ 所以,rand在Monte Carlo中是必不可少的,必须保证测试数据的随机性. 用蒙特卡洛方法进 ...

  7. JVM优化(未完)

    -XX:+PrintGC 在eclipse控制台打印简单的GC信息,与-verbose:gc一样 -verbose:gc 在eclipse控制台打印简单的GC信息 -XX:+PrintGCDetail ...

  8. 1、linux-wget

    1.常用下载与参数 wget + 空格 + 要下载文件的url路径 例如: # wget linuxsense.org/xxxx/xxx.tar.gz">http://www.linu ...

  9. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP基础知识、jdk动态代理,CGLib动态代理)

  10. 【NLP】中文分词:原理及分词算法

    一.中文分词 词是最小的能够独立活动的有意义的语言成分,英文单词之间是以空格作为自然分界符的,而汉语是以字为基本的书写单位,词语之间没有明显的区分标记,因此,中文词语分析是中文信息处理的基础与关键. ...