Codeforces.842D.Vitya and Strange Lesson(Trie xor)
/*
异或只有两种情况,可以将序列放到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)的更多相关文章
- codeforces 842D Vitya and Strange Lesson
题目大意: 定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex Input First line contains two i ...
- CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)
给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- 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 D. Vitya and Strange Lesson
Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of numbers is ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson
[链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...
随机推荐
- Linux MMC framework2:基本组件之host
声明:本文很多内容和思路参考了http://www.wowotech.net/comm/mmc_host_driver.html,对原作者表示感谢! 1.前言 本文是Linux MMC framewo ...
- 董事局主席董事长总裁首席执行官CEO总裁董事监事区别
董事长是公司的最大股东:董事长是董事会的主席,一般是企业的所有者,掌握企业的股权并且决定企业的发展策略. 董事局主席通常是在大财团中才会出现,董事局主席管数个董事长,一个大财团涉及很多方面的业务,因此 ...
- [bzoj3123][洛谷P3302] [SDOI2013]森林(树上主席树+启发式合并)
传送门 突然发现好像没有那么难……https://blog.csdn.net/stone41123/article/details/78167288 首先有两个操作,一个查询,一个连接 查询的话,直接 ...
- scn 时间
Scn转换成时间: select to_char(scn_to_timestamp(3998591352171),'YYYY-MM-DD HH24:MI:SS') from dual: 时间转换成sc ...
- Day4--------------对文件的权限管理
一.文件权限 ls -l 显示当前文件详细信息 例: -rw-r--rwx.1 root root 1415 11月 9 20:21 anaconda-ks.cfg 依次顺序为:权限信 ...
- expdp和impdp 使用注意事项
使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...
- 【ES】学习5-全文搜索
全文搜索两个最重要的方面是:相关性, 分析. 一旦谈论相关性或分析这两个方面的问题时,我们所处的语境是关于查询的而不是过滤. match:单个词查询 GET /my_index/my_type/_se ...
- 在 laravel 的 DB::transaction 中,为外部变量赋值
例如,我想在 laravel 的事务中,对某个外部变量赋值,然后在后续的逻辑中判断该变量的属性 $user = null; // init DB::transaction(function() use ...
- laravel 接口跨域
最方便的方法,新建一个middleWare,把这个middleware加入到全局中间件,所有的请求,都会经过这个中间件的过滤. php artisan make:middleware CrossHtt ...
- Python 定值类
1.__str__和__repr__ 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__(self, nam ...