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表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...
随机推荐
- 第一个FPGA工程—LED流水灯
这一章我们来实现第一个FPGA工程-LED流水灯.我们将通过流水灯例程向大家介绍一次完整的FPGA开发流程,从新建工程,代码设计,综合实现,管脚约束,下载FPGA程序.掌握本章内容,大家就算正式的开始 ...
- Python中__repr__和__str__区别
Python中__repr__和__str__区别 看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): ...
- linux软链接和硬链接的区别
硬链接:ln 3.txt 4 相当于把源文件复制了一份 软连接:ln -s 3.txt 5 相当于快捷方式 改动源文件4.5同时更新,删除3.txt ,5不存在,4存在的.
- SharePoint 2010 使用Install-SPSolution部署wsp包状态一直是”正在部署”
1.服务器场信息如下: 2.使用下面命令部署,状态一直是"正在部署" Install-SPSolution –Identity xxxx.wsp –WebApplication h ...
- ajax模拟获取json
现在工作中我用到获取数据的方式,基本都是ajax.前台获取后端的数据后,需要进行处理,然后把他们放进页面中的相应标签里.下面举一个简单的例子,来模拟数据的获取和摆放. 这里用ng框架获取数据然后处理, ...
- zabbix系列(八)zabbix添加对web页面url的状态监控
通过zabbi做web监控不仅仅可以监控到站点的响应时间,还可以根据站点返回的状态码,或者响应时间做报警 1.对需要监控的主机添加web监控 在configuration—hosts 中打开主机列 ...
- chart学习
效果图: 目录信息 graphic.jsp <%@ page language="java" contentType="text/html; charset=UTF ...
- Mysql复习大全(转)
基础知识: 1.数据库的连接 mysql -u -p -h -u 用户名 -p 密码 -h host主机 2.库级知识 显示数据库: show databases; 选择数据库: use dbname ...
- 560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- Python-JS中的事件详解
目录 fdf!! fefd 一.JS中的事件二.JS中的事件分类: 1.事件初级: 2.事件参数 Event 3.鼠标事件 4.键盘事件 *** 5.表单事件 *** 6.文档事件 * 7.图片事件 ...