【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 ...
随机推荐
- Yii1.1框架实现PHP极光推送消息通知
一.下载极光推送PHP SDK,解压后放在/protected/components/目录下,如下图所示: 二.完善修改下官方的demo例子,我这里复制一份demo,改为NotifyPush.php, ...
- python模块详解
什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...
- Vector源码分析
Vector与ArrayList底层实现基本类似,底层都是用数组实现的,最大的不同是Vector是线程安全的.ArrayList源码分析请参考ArrayList源码分析 一.源码分析 基于jdk1.7 ...
- beego 各种形式的路由实例
基本路由 基本路由就是和http.Handle和http.HandleFunc一样都是绑定固定的路径,比如绑定了4个路由映射: 定义的4个控制器中,匹配哪一个路由,就输出对应的控制名. beego.R ...
- 牛客OI周赛8-普及组
https://ac.nowcoder.com/acm/contest/543#question A. 代码: #include <bits/stdc++.h> using namespa ...
- 文件传输协议FTP、SFTP和SCP
网络通信协议分层 应用层: HTTP(Hypertext Transfer Protocol 超文本传输协议,显示网页) DNS(Domain Name System) FTP(File Transf ...
- react-redux异步数据操作
import React, { Component } from 'react'; import './App.css'; import {connect} from 'react-redux'; i ...
- embed标签的flash层级太高问题
因为客户要求,项目得兼容IE的兼容模式 页面到了flash都会遮挡底部悬浮的导航. 改变浮动窗口和embed的层级还是不可以.应该不是层级的关系. 最后百度解决方案:在embed标签内添加了wmode ...
- .Net在操作mysql查询的时候出现“: Unknown column 'UserName' in 'where clause'”错误
今天使用.Net操作mysql查询的时候,如果加上条件查询的时候就会出现 Unknown column 'UserName' in 'where clause'这个错,不加条件直接select * f ...
- VS code常用快捷方式—转载
http://www.cnblogs.com/weihe-xunwu/p/6687000.html