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 the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0.
Vitya quickly understood all tasks of the teacher, but can you do the same?
You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps:
Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x.
Find mex of the resulting array.
Note that after each query the array changes.
Input
First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries.
Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array.
Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105).
Output
For each query print the answer on a separate line.
题目大意:
定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex
解题报告:
考场上搞了一个小时,原来看错题了,其实只是简单的Trie树基本操作,
mex数:如果左子树没满直接走左子树,不然就走右子树.
异或操作:如果x的该位为1,交换该节点的左右子树,打上标记即可
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=6e6+10,maxdep=21;
int gi(){
int str=0;char ch=getchar();
while(ch>'9' || ch<'0')ch=getchar();
while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
return str;
}
struct node{
int l,r,s,rev;
}t[N];
int n,root=0,tot=0,w[30],m;
void insert(int &rt,int x,int d){
if(!rt)rt=++tot;
if(d==-1){
t[rt].s=1;return ;
}
if(x&w[d])insert(t[rt].r,x,d-1);
else insert(t[rt].l,x,d-1);
t[rt].s=t[t[rt].l].s&t[t[rt].r].s;
}
void pushdown(int rt,int d){
if(!t[rt].rev)return ;
int k=t[rt].rev;
t[t[rt].l].rev^=k;t[t[rt].r].rev^=k;
if(d>=1 && (k&w[d-1])){
swap(t[t[rt].l].l,t[t[rt].l].r);swap(t[t[rt].r].l,t[t[rt].r].r);
}
t[rt].rev=0;
}
int query(int rt,int d){
if(d==-1)return 0;
pushdown(rt,d);
if(!t[t[rt].l].s)return query(t[rt].l,d-1);
return query(t[rt].r,d-1)+w[d];
}
void work()
{
int x;
n=gi();m=gi();
w[0]=1;for(int i=1;i<=maxdep;i++)w[i]=w[i-1]<<1;
for(int i=1;i<=n;i++){
x=gi();insert(root,x,maxdep);
}
while(m--){
scanf("%d",&x);
t[root].rev^=x;
printf("%d\n",query(root,maxdep));
}
}
int main()
{
work();
return 0;
}
Codeforces Round #430 D. Vitya and Strange Lesson的更多相关文章
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- Codeforces Round #430 (Div. 2) 【A、B、C、D题】
[感谢牛老板对D题的指点OTZ] codeforces 842 A. Kirill And The Game[暴力] 给定a的范围[l,r],b的范围[x,y],问是否存在a/b等于k.直接暴力判断即 ...
- 【Codeforces Round #430 (Div. 2) A C D三个题】
·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意: 给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...
- 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson
[链接]点击打开链接 [题意] 给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数. [题解] 异或的效果是可以累加的,所以不用每次都 ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
随机推荐
- highcharts 具体参数详解
<script type="text/javascript" src="js/jquery.min.js"></script> < ...
- django启动uwsgi报错
查看uwsgi.log *** Starting uWSGI 2.0.17 (64bit) on [Thu Apr 5 17:46:15 2018] *** compiled with version ...
- lodash源码分析之获取数据类型
所有的悲伤,总会留下一丝欢乐的线索,所有的遗憾,总会留下一处完美的角落,我在冰峰的深海,寻找希望的缺口,却在惊醒时,瞥见绝美的阳光! --几米 本文为读 lodash 源码的第十八篇,后续文章会更新到 ...
- 工频相位无线同步模块PSYN5000系列在高压设备状态检测和局部放电故障定位的应用方案
关键词: PSYN5000,无线同步模块,工频相位,局部放电,在线监测,高压设备,设备状态,故障定位. 前言: 在电力监测领域,出于方便和安全考虑,有些系统不得不采用无线通信的方式,在这样一个无线通信 ...
- zTree根据json选中节点,并且设置其他节点不可选
首先,在适配目录树时,使用checkbox形式,配置代码如下: var settingCatalog = { check:{ enable: true }, data:{ simpleData:{ e ...
- html5shiv.js和respond.min.js的作用
html5shiv:解决ie9以下浏览器对html5新增标签的不识别,并导致CSS不起作用的问题. respond.min:让不支持css3 Media Query的浏览器包括IE6-IE8等其他浏览 ...
- CSS揭秘(四)视觉效果
Chapter 4 1. 单侧投影 为元素设置投影可以使用 box-shadow 属性,指定三个长度值(X轴偏移量.Y轴偏移.模糊半径)与一个颜色值 要想只在底部设置投影,需要用到第四个参数:投影的扩 ...
- 参加Java培训到底靠不靠谱?
导读 科技越发展,社会越进步,人们越便利,便衍生出更多的人从事程序员这个高大上的职业,可哈尔滨Java培训学校这么多,到底靠不靠谱,会不会处处是陷阱,爱尚实训帮你擦亮眼 随着时代的发展,越来越多的人对 ...
- ubuntu安装IBM DB2 Express-C
去 官网 下载DB2对应Linux下的安装包和语言包.
- sql serve 数据库游标的使用
什么是游标? 通俗来讲,个人理解,游标是对一个查询结果集,每次取出一条数据进行处理操作. 使用场景: 例如,我们要修改一个表300条数据,且每条数据修改的内容不一样,那么平时用的update ...