题目链接

/*
异或只有两种情况,可以将序列放到01Tire树上做
在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归;否则向1
(0位置上的数都满了 即 其子树叶子节点都有值)
异或情况下 若x在当前位有1,则反转0/1继续走
由于异或具有结合率,异或一次求mex和异或多个数求原数列mex是一样的
故每次不需要修改原数列,las^=opt即可
注意需要去重 因为在判断某位置rt下的区间中的数都有时,需要num[rt],相同的数显然不能算(画个图)
*/
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define gc() getchar()
const int N=3e5+5,B=20; int n,m,A[N],bit[36];
struct Node
{
int val;
Node *nxt[2];
Node() {val=0, memset(nxt,NULL,sizeof nxt);}
}*rt,pool[N*19];
Node *new_Node()
{
static int cnt=0;
return &pool[cnt++];
}
Node *root=new_Node();
struct Trie
{
void Insert(int n)
{
rt=root;
for(int i=B; i; --i)
{
// printf("I i:%d bit[i]:%d n:%d %d %d\n",i,bit[i],n,n&bit[i],rt->val);
bool id=n&bit[i];
if(!rt->nxt[id])
rt->nxt[id]=new_Node();
++rt->val, rt=rt->nxt[id];
}
++rt->val;//..!
}
inline Node *to(Node *rt,bool p)
{
if(!rt->nxt[p]) rt->nxt[p]=new_Node();
return rt->nxt[p];
}
int Query_Mex(int x)
{
int res=0; rt=root;
for(int i=B; i; --i)
{
if(x&bit[i])
if(!rt->nxt[1]) return res;//后面都没有过
else if(rt->nxt[1]->val < 1<<i-1) rt=rt->nxt[1];//,puts("C");
else rt=to(rt,0), res+=(1<<i-1);//,puts("A");
else
if(!rt->nxt[0]) return res;
else if(rt->nxt[0]->val < 1<<i-1) rt=rt->nxt[0];//,puts("D");
else rt=to(rt,1), res+=(1<<i-1);//,puts("B");
// printf("Q %d:%d %d %d %d\n",x,i,res,bit[i],x&bit[i]);
}
return res;
}
}t;
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("842.in","r",stdin);
#endif for(int i=1; i<=B; ++i) bit[i] = 1<<i-1;
n=read(),m=read();
for(int i=1; i<=n; ++i) A[i]=read();
std::sort(A+1,A+1+n);
int cnt=1;
for(int i=2; i<=n; ++i)
if(A[i]!=A[i-1]) A[++cnt]=A[i];
n=cnt;
for(int i=1; i<=n; ++i) t.Insert(A[i]);
int x=0;
while(m--)
x^=read(), printf("%d\n",t.Query_Mex(x)); return 0;
}

Codeforces.842D.Vitya and Strange Lesson(Trie xor)的更多相关文章

  1. codeforces 842D Vitya and Strange Lesson

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

  2. CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)

    给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...

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

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

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

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

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

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

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

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

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

  8. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  9. 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson

    [链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...

随机推荐

  1. js实现页面遮罩层,并且阻止页面body滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Delphi中的动态包,有详细建立包的步骤(答案很简单:因为包的功能强大)

    为什么要使用包? 答案很简单:因为包的功能强大.设计期包(design-time package)简化了自定义组件的发布和安装:而运行期包(run-time package)则更是给传统的程序设计注入 ...

  3. python调用win32com.client的GetObject查找进程信息及服务信息

    为何不用wmi呢?因为执行很慢,为啥不用winreg?因为winreg在批量获取及遍历服务方面很不方便,于是采用这方法 该方法同命令行下的wmic执行 获取服务信息 #coding=utf8 from ...

  4. WPF通过DynamicResource的用法

    1.先在资源类库中编写:style.xaml,如下: <ResourceDictionary   xmlns="http://schemas.microsoft.com/winfx/2 ...

  5. 最大流算法-最高标号预流推进(HLPP)

    昨天我们学习了ISAP算法,它属于增广路算法的大类.今天学习的算法是预流推进算法中很高效的一类--最高标号预流推进(HLPP). 预流推进 预流推进是一种很直观的网络流算法.如果给到一个网络流让你手算 ...

  6. 织梦dedeCMS数据库结构字段说明-简略说明

    dede_addonarticle 附加文章表 aid int(11) 文章编号typeid int(11) 分类栏目编号body mediumtext 文章内容dede_addonflash 附加F ...

  7. 002_HTTP每日分析及翻译

    一.Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng ...

  8. certificate expired

    最近在测试802.1x,测试过程中,radius服务器端一直显示如下错误: (5)   authenticate {(5) eap: Expiring EAP session with state 0 ...

  9. Qt5 json 数据处理

    QT4中使用第三方库QJson解析JSON文件. QT5新增加了处理JSON的类,类均以QJson开头,包含在QtCore模块中. 用到的头文件 #include <QJsonArray> ...

  10. Go语言规格说明书 之 通道 发送语句(send) 和 接收操作符(receive)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...