codeforces 842D Vitya and Strange Lesson
题目大意:
定义mex数为数组中第一个没有出现的非负整数.有m个操作,每个操作有一个x,将数组中所有的元素都异或x,然后询问当前的mex
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).
For each query print the answer on a separate line.
2 2
1 3
1
3
1
0
4 3
0 1 5 6
1
2
4
2
0
0
5 4
0 1 5 6 7
1
1
4
5
2
2
0
2
2
0
0
5 4
0 1 5 6 7
1
1
4
5
2
2
0
2
%%%%yzh大佬,学会了新姿势:http://www.cnblogs.com/Yuzao/default.html?page=1这题正解其实是01Trie
按照理解,Trie用来保存字符串,但也可以通过01分支来保存数
这样我们只要找到树中最靠左的空节点,对应的值即为答案
更新可以用lazy标记,如果有标记,则反转左右子节点
如果左边有空节点,那么就返回向左查找的值
没有则返回右节点查找的值+2^(dep-1)[左节点数量]
然后注意本题是按高位向低位拓展,因为高位分支少,所以会更快
YZD&&HJW&&SAC&&YZH%%%orz大佬
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node
{
int l,r;
}c[];
int seg[],rev[],root,tot,n,m;
void insert(int &rt,int x,int dep)
{
if (!rt) rt=++tot;
if (dep==)
{
seg[rt]=;
return;
}
if (x&(<<dep-)) insert(c[rt].r,x,dep-);
else insert(c[rt].l,x,dep-);
seg[rt]=seg[c[rt].l]&&seg[c[rt].r];
}
void pushdown(int &rt,int dep)
{
if (rev[rt]==) return;
int k=rev[rt];
rev[c[rt].l]^=k;
rev[c[rt].r]^=k;
if (k&(<<dep-))
swap(c[rt].l,c[rt].r);
rev[rt]=;
}
int query(int &rt,int dep)
{
if (dep==) return ;
pushdown(rt,dep);
if (seg[c[rt].l]==) return query(c[rt].l,dep-);
else return query(c[rt].r,dep-)+(<<dep-);
}
int main()
{int i,j,x;
cin>>n>>m;
for (i=;i<=n;i++)
{
scanf("%d",&x);
insert(root,x,);
}
for (i=;i<=m;i++)
{
scanf("%d",&x);
rev[root]^=x;
printf("%d\n",query(root,));
}
}
codeforces 842D Vitya and Strange Lesson的更多相关文章
- Codeforces.842D.Vitya and Strange Lesson(Trie xor)
题目链接 /* 异或只有两种情况,可以将序列放到01Tire树上做 在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归:否则向1 (0位置上的数都满了 ...
- 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
因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...
随机推荐
- alpha冲刺第七天
一.合照 二.项目燃尽图 三.项目进展 问答界面问答内容呈现 设置里的帐号设置呈现 能爬取教务处网站的内容保存到本地数据库 四.明日规划 继续完善各个内容的界面呈现 查找关于如何自动更新爬取内容 搜索 ...
- CoreAnimation注意事项
最近调查的一个bug和内存泄露都和CoreAnimation有关,因此谈一下使用CoreAnimation需要注意的几个问题 CAAnimation的delegate属性是retain的,这个设计确实 ...
- Java如何调取创蓝253短信验证码
基于创蓝253短信服务平台的Java调用短信接口API package com.bcloud.msg.http; import java.io.ByteArrayOutputStream; impor ...
- H5 音频标签自定义样式修改以及添加播放控制事件
说明: 需求要求这个音频标签首先要是可适配移动端浏览器的,音频样式就是参考微信做的. 最终效果如下: 具体实现 思路: H5 的 <audio> 标签是由浏览器负责实现默认样式的.所以不同 ...
- JS的if和switch
var aa=parseInt(prompt("请输入你的年龄")); //定义输入 if(aa<18){ //输出小于18,返回值少年 alert("少年&quo ...
- openssl几个加密算法使用介绍
1.openssl简介 1)openssl概述 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. ...
- Linux入门:usermod - 修改用户帐户信息
一.什么是usermod? usermod 命令通过修改系统帐户文件来修改用户账户信息usermod [options] user_name选项(options)-a|--append ##把用户追加 ...
- redux的使用过程
1.redux是react的状态管理工具,可以用来存放公共数据,因此也可用来作为组件间参数传递的方法. 2.组件传参,需要有一个公共的父组件.在父组件中引入Provider.通过Provider将 ...
- JSON(五)——同步请求中使用JSON格式字符串进行交互(不太常见的用法)
在同步请求中使用JSON格式进行数据交互的场景并不多,同步请求是浏览器直接与服务器进行数据交互的大多是用jsp的标签jstl和el表达式对请求中的数据进行数据的渲染.我也是在一次开发中要从其它服务器提 ...
- Object.defineProperties()和Object.defineProperty()方法
Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象. 语法:Object.defineProperty(obj, pro ...