首页 技术 正文
技术 2022年11月23日
0 收藏 304 点赞 5,180 浏览 1030 个字

题目链接:http://codeforces.com/contest/842/problem/D

题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就是比较节点总数和有的数字数比较有限向左边转移。

然后这个异或其实可以利用一个数num与一个一个的x异或然后求异或的mex也是容易的只要判断当前二进制位是1那么左右节点拥有的数字数互换(不用真的互换

只要用转移体现出来就行了)。这里的01字典树写法是类似线段树且利用递归的方法。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e5 + ;
int ch[ * M][] , nodes[ * M];
int node_size , num;
void build(int pos , int node) {
if(pos < ) return;
for(int i = ; i < ; i++) {
ch[node][i] = ++node_size;
nodes[node_size] = ;
build(pos - , ch[node][i]);
}
}
void init() {
node_size = ;
nodes[] = ;
build( , );
}
void Insert(int node , int x , int pos) {
if(pos < ) {
nodes[node] = ;
return ;
}
int id = (x >> pos) & ;
Insert(ch[node][id] , x , pos - );
nodes[node] = nodes[ch[node][id]] + nodes[ch[node][id ^ ]];
}
int query(int node , int x , int pos) {
if(pos < ) return x;
int gg = (num >> pos) & ;
if(nodes[ch[node][gg]] < ( << pos)) return query(ch[node][gg] , x , pos - );
return query(ch[node][gg ^ ] , x | ( << pos) , pos - );
}
int main() {
int n , m , x;
num = ;
init();
scanf("%d%d" , &n , &m);
for(int i = ; i < n ; i++) {
scanf("%d" , &x);
Insert( , x , );
}
for(int i = ; i < m ; i++) {
scanf("%d" , &x);
num ^= x;
printf("%d\n" , query( , , ));
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,966
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783