首页 技术 正文
技术 2022年11月18日
0 收藏 658 点赞 5,089 浏览 1558 个字

D. Vitya and Strange Lesson

题意

数列里有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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898