NOJ2026:Keroro侵略地球(01字典树)
传送门
题意
略
分析
将n个数插入字典树中,m次查询,取最大值,复杂度\(O(mlogn)\)
trick
1.注意题目给的空间,开40刚刚够(62852K)
2.作为01字典树的模板保存了
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
//#pragma comment(linker, "/STACK:102400000,102400000")
//inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
const int maxn = 1e5+10;//集合中的数字个数
int ch[40*maxn][2];//节点的边信息
//int num[40*maxn];//记录节点的使用次数,删除时要用
ll val[40*maxn];//节点存储的值
int cnt;//树中节点的个数
inline void init()
{
cnt=1;mem(ch[0],0);//清空树
}
void insert(ll x)//在字典树中插入x,和一般字典树操作相同,将x化成二进制插入到字典树
{
int cur=0;
for(int i=40;i>=0;--i)
{
int idx=(x>>i)&1;
if(!ch[cur][idx])
{
mem(ch[cnt],0);
ch[cur][idx]=cnt;
//num[cnt]=0;
val[cnt++]=0;
}
//printf("ch[%d][%d]=%d\n",cur,idx,ch[cur][idx]);
cur=ch[cur][idx];
//num[cur]++;
}
val[cur]=x;//最后节点插入val
}
void update(ll x,int c)
{
int cur=0;
for(int i=40;i>=0;--i)
{
int idx=(x>>i)&1;
cur=ch[cur][idx];
//num[cur]+=c;
}
}
ll query(ll x)//在字典树(数集)中查找和x异或是最大值的元素y,返回y
{
int cur=0;
for(int i=40;i>=0;--i)
{
int idx=(x>>i)&1;
if(ch[cur][idx^1]) cur=ch[cur][idx^1];else cur=ch[cur][idx];
}
return val[cur]^x;
}
/*
ll query(ll x)//在字典树(数集)中查找和x异或是最大值的元素y,返回异或的最大值
//带删除操作的查询
{
int cur=0;
for(int i=32;i>=0;--i)
{
int idx=(x>>i)&1;
if(ch[cur][idx^1]&&num[ch[cur][idx^1]]) cur=ch[cur][idx^1];else cur=ch[cur][idx];
}
return val[cur]^x;
}
*/
int main()
{
int n,m;
ll x;
while(scanf("%d %d",&n,&m)!=EOF)
{
init();
ll ans=0;
F(i,1,n)
{
scanf("%I64d",&x);
insert(x);
}
//printf("Case #%d:\n",k);
F(i,1,m)
{
scanf("%I64d",&x);
ans=max(ans,query(x));
}
printf("%I64d\n",ans);
}
return 0;
}
NOJ2026:Keroro侵略地球(01字典树)的更多相关文章
- 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 ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- hdu-4825(01字典树)
题意:中文题意 解题思路:01字典树板子题 代码: #include<iostream> #include<algorithm> #include<cstdio> ...
- 【cf842D】Vitya and Strange Lesson(01字典树)
D. Vitya and Strange Lesson 题意 数列里有n个数,m次操作,每次给x,让n个数都异或上x.并输出数列的mex值. 题解 01字典树保存每个节点下面有几个数,然后当前总异或的 ...
- hdu 4825 && acdream 1063 01字典树异或问题
题意: 给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个 题解: 经典的01字典树问题,学习一哈. 把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可 # ...
- cf842D 01字典树|线段树 模板见hdu4825
一般异或问题都可以转换成字典树的问题,,我一开始的想法有点小问题,改一下就好了 下面的代码是逆向建树的,数据量大就不行 /*3 01字典树 根据异或性质,a1!=a2 ==> a1^x1^..^ ...
随机推荐
- Elasticsearch shield权限管理详解
Elasticsearch shield权限管理详解 学习了:https://blog.csdn.net/napoay/article/details/52201558 现在(20180424)改名为 ...
- memcache的学习路线图
memcache学习材料 //memcache自带的github 上的 wiki //席剑飞 Memcache(MC)系列 1~8系列 评注: memcache系统写的最深的一博客,建议一读 ...
- Linux中修改docker镜像源及安装docker
1.首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.re ...
- [Testing] Config jest to test Javascript Application -- Part 3
Run Jest Watch Mode by default locally with is-ci-cli In CI, we don’t want to start the tests in wat ...
- ruby rails
http://www.zhihu.com/question/19552402 作者:陈振宇链接:http://www.zhihu.com/question/19552402/answer/1236 ...
- IO多路复用:select、poll、epoll示例
一.IO多路复用 所谓IO多路复用,就是通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. Linux支持IO多路复用的系统调用有 ...
- 【Mongodb教程 第十八课 】MongoDB常用命令 数据库命令 集合操作命令
面向文档的 NoSQL 数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能. 条件操作符 <, <=, >, >= 这个操作符就不用多 ...
- VUE 之 生命周期
1. Vue实例的生命周期分为8个周期 1.1 beforeCreate:在实例创建前 <div id="app"> {{ name }} <button @cl ...
- C标准库中atoi的一种可能的实现
为避免与标准库中的atoi产生歧义, 我将自己编写的函数命名为strToInt, 以下是示例代码 #include <stdio.h> int strToInt(const char *s ...
- String常量池
http://developer.51cto.com/art/201106/266454.htm