NOJ2026:Keroro侵略地球(01字典树)
2024-10-11 20:43:19
传送门
题意
略
分析
将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;
}
最新文章
- Oracle软件安装目录满的清理方法
- 【QUESTION】
- Windows 10 后台音频
- D3D标注动态避让
- js、jquery、动态添加option项
- DTD简单使用
- C51指针类型和存储区的关系详解
- Canvas Path 绘制柱体
- Eclipse设置内存大小
- 在网页中使用particlesjs实现背景的动态粒子特效
- Lodop打印控件 超文本自动分页
- [原]openstack-kilo--issue(二十三)虚拟机状态错误power_status为shutdonw或者vm_status为error
- ThinkPHP模板的知识
- 经典算法二分查找循环实现Java版
- (简单匹配)Card Game Cheater -- hdu --1528
- 前端开发 - HTML/CSS
- 使用VS2013和git进行代码管理
- metasploit利用漏洞渗透攻击靶机
- 无法正常下载Nuget 包的问题
- win10下安装lxml
热门文章
- MBProgressHUD 显示方向异常
- Lambda 表达式的演示样例-来源(MSDN)
- Missing &;#39;name&;#39; key attribute on element activity at AndroidMan
- NOI 2014简要题解
- find the longest of the shortest (hdu 1595 SPFA+枚举)
- 解压Zip
- NPOI实现Excel导入
- 在安卓6.0(及以上)设备上无法获取无线网卡MAC地址的解决方案
- Android 的assets文件资源与raw文件资源读取
- MFC上显示摄像头JPEG图片数据的两种方法