codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
2024-09-01 05:17:16
题目链接: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 ;
}
最新文章
- Android Studio 解决更新慢的问题
- winform实现word转换为PDF(.doc)
- 16款最佳HTML5超酷动画演示及源码
- android开发 更新升级安装到一半自动闪退
- makefile 中 $@ $^ %<; 使用
- javascript之事件
- java设计原则:16种原则
- win10 uwp 打开文件管理器选择文件
- java对象引用-要掌握的细节
- hive:框架理解
- JavaScript替换HTML标签
- 云计算openstack共享组件(1)——时间同步服务ntp
- react 在IE9下input标签使用e.target.value取值失败
- 爬虫3 requests基础之下载图片用content(二进制内容)
- idea搭spring boot项目
- 使用pwm进行呼吸灯的设计
- JavaScript中遍历数组和对象的方法
- omnigraffle 的一些总结
- ArcGIS Pro 获得工具的个数
- jenkins构建多个项目执行顺序设置
热门文章
- Could not load NIB in bundle: &#39;NSBundle.....
- maven私服nexus上传第三方jar包以及下载
- UE4中UMG与C++交互 页面文本修改
- Mac环境下升级gcc版本--rocksdb
- python虚拟环境完美部署
- C#开发可播放摄像头及任意格式视频的播放器
- 武林 HDU - 1107
- Dubbo的基本介绍及使用
- 配置Oracle透明网关用以连接 SQLServer经验总结
- Java网络编程 -- BIO 阻塞式网络编程