【cf842D】Vitya and Strange Lesson(01字典树)
题意
数列里有n个数,m次操作,每次给x,让n个数都异或上x。并输出数列的mex值。
题解
01字典树保存每个节点下面有几个数,然后当前总异或的是sw,则sw为1的位的节点左右孩子交换(不用真的交换)。左孩子的值小于左边总节点数则mex在左子树,否则在右子树。
代码
const int N=531000;//3e5<2^19<N
int sw=0;
struct Trie{
int ch[N*20][2];
int cnt[N*20];
int size;
void Build(int node, int pos){
if(pos<0)return;
rep(i,0,2){
ch[node][i]=++size;
cnt[size]=0;
Build(ch[node][i], pos-1);
}
}
void Init(){
size=0;
Build(0,19);
}
void Insert(int node, int pos, int num){
if(pos<0)cnt[node]=1;
if(pos<0) return;
Insert(ch[node][(num>>pos)&1], pos-1, num);
cnt[node]=cnt[ch[node][0]]+cnt[ch[node][1]];
}
int Query(int node, int pos, int num){
if(pos<0)
return num;
int lson=(sw&(1<<pos))?1:0;
if(cnt[ch[node][lson]]<(1<<pos)){
return Query(ch[node][lson], pos-1, num);
}
return Query(ch[node][!lson], pos-1,num+(1<<pos));
}
}trie;
int main() {
int n,m;
while(~scanf("%d%d",&n,&m)){
trie.Init();
sw=0;
rep(i,0,n){
int x;
scanf("%d",&x);
trie.Insert(0,19,x);
}
while(m--){
int x;
scanf("%d",&x);
sw^=x;
printf("%d\n",trie.Query(0,19,0));
}
puts("");
}
return 0;
}
自从用了cf上偷来的开头模板以后,感觉写代码速度也快了。但是,代码像裙子越短越性感。所以博客上就不放头文件了。
其实可以像线段树一样写,写得更短了哈哈:
const int N=531000;
int sw=0;
struct Trie{
int cnt[N*20];
void Insert(int node, int pos, int num){
if(pos<0){cnt[node]=1;return;}
Insert(node<<1|((num>>pos)&1), pos-1, num);
cnt[node]=cnt[node<<1]+cnt[node<<1|1];
}
int Query(int node, int pos, int num){
if(pos<0) return num;
int cur=(sw>>pos)&1;
cur|=node<<1;
if(cnt[cur]<(1<<pos)) return Query(cur, pos-1, num);
return Query(cur^1, pos-1,num+(1<<pos));
}
}trie;
int main() {
int n,m;
scanf("%d%d",&n,&m);
rep(i,0,n){
int x;
scanf("%d",&x);
trie.Insert(1,19,x);
}
while(m--){
int x;
scanf("%d",&x);
sw^=x;
printf("%d\n",trie.Query(1,19,0));
}
return 0;
}
【cf842D】Vitya and Strange Lesson(01字典树)的更多相关文章
- cf842d Vitya and Strange Lesson
#include <iostream> #include <cstdio> using namespace std; int s[2000005][2], cnt, n, m, ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)
给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...
- cf842D 01字典树|线段树 模板见hdu4825
一般异或问题都可以转换成字典树的问题,,我一开始的想法有点小问题,改一下就好了 下面的代码是逆向建树的,数据量大就不行 /*3 01字典树 根据异或性质,a1!=a2 ==> a1^x1^..^ ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- Chip Factory---hdu5536(异或值最大,01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- hdu5296 01字典树
根据二进制建一棵01字典树,每个节点的答案等于左节点0的个数 * 右节点1的个数 * 2,遍历整棵树就能得到答案. AC代码: #include<cstdio> using namespa ...
随机推荐
- Linux—vim常用命令
vim常用命令: 1. 键入i进入编辑模式2. esc进入命令模式3. a,进入编辑模式3. b,光标移动到单词前,end,光标移动到行尾4. home光标移动到行首5. cc,删除当前行,并进入编辑 ...
- Joyride (spaf)
题目链接:https://codeforces.com/gym/101873/problem/C spaf的复杂度有点迷,按道理来说,一个简单的spaf在这题的复杂度是1e9,所以不敢写,然后用优先队 ...
- 黑客帝国效果赏析(包含ES6的语法)
首先,看看效果吧. 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- socket流程
- JSP 快速入门
目录 生命周期 9大对象 常用指令 基本语法 表达式语言(EL) jstl介绍 常用的jstl标签 生命周期 我们虽然写的是jsp,代码中包含了html.css.js,以及Java代码,但是真正执行的 ...
- MySQL 批量修改某一列的值为另外一个字段的值
mysql> select * from fruit; +----+--------+-------+ | id | name | price | +----+--------+-------+ ...
- Windows和Linux的Jmeter分布式集群压力测试
Windows的Jmeter分布式集群压力测试 原文:https://blog.csdn.net/cyjs1988/article/details/80267475 在使用Jmeter进行性能测试时, ...
- # 【Python3练习题 004】输入某年某月某日,判断这一天是这一年的第几天?
# [Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? # 思路:先判断是否为闰年,这关系到 2 月份的天数.# 之后再根据月份值把前几个月的天数累积加起来,最后再加上个“日 ...
- 使用ThreadLocal管理Mybatis中SqlSession对象
转自http://blog.csdn.net/qq_29227939/article/details/52029065 public class MybatisUtil { private stati ...
- 123. 单词搜索(DFS)
描述 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样例 给出 ...