【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 ...
随机推荐
- 007-迅雷定时重启AutoHotkey脚本-20190411
;; 定时重启迅雷.ahk,;;~ 2019年04月11日;#SingleInstance,forceSetWorkingDir,%A_ScriptDir%DetectHiddenWindows,On ...
- c++入门之 深入cin
cin 表示输入流,但是究其本质,又能认识到什么呢?先上代码: #include "iostream" };//c++11中使用{}进行重新命名 int main() { usin ...
- 《梦断代码》Scott Rosenberg著(三)
开放与封闭之论: 程序源代码是商业软件公司最重要的资产,所以软件公司售卖二进制文件.这样也就意味着如果微软的软件产品出了问题,即便你是一个程序大牛也无法修复它.你只能等着微软来修正问题,因为只有微软程 ...
- ES使用C#添加和更新文档
ElasticSearch 使用C#添加和更新文档 这是ElasticSearch 2.4 版本系列的第四篇: 第一篇:ES1:Windows下安装ElasticSearch 第二篇:ES2:Elas ...
- 快速失败/报错机制 - fail-fast
一.快速报错机制(fail-fast) 这是<Java编程思想>中关于快速报错机制的描述 Java容器有一种保护机制,能够防止多个进程同时修改同一个容器的内容.如果在你迭代遍历容器的过程中 ...
- Tomcat Cluster
Tomcat群集配置| Tomcat集群| MuleSofthttps://www.mulesoft.com/tcat/tomcat-cluster Tomcat Clustering - A Ste ...
- jquery中ajax使用
JQuery的Ajax操作,对JavaScript底层Ajax操作进行了封装, <script type="text/javascript"> $.ajax({ url ...
- php单元测试
https://blog.csdn.net/gaisidewangzhan1/article/details/80347008
- [转帖]Windows NT 之父 - David Cutler
Windows NT 之父 - David Cutler https://www.cnblogs.com/wangwust/p/6826200.html 曾经下过 夺路狂奔的电子书 但是还没看完.. ...
- flutter 动画双指放大图片
class GridAnimation extends StatefulWidget { @override State<StatefulWidget> createState() { r ...