题意:

  给一个集合,多次询问,每次给一个k,问你集合和k异或结果最大的哪个

题解:

  经典的01字典树问题,学习一哈.

  把一个数字看成32位的01串,然后查找异或的时候不断的沿着^为1的路向下走即可

#include <bits/stdc++.h>
#define endl '\n'
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=1e5+10,maxm=2e6+10;
int casn,n,m,k,num[maxn];
struct Trie{
int next[maxn*32][2],size;
ll val[maxn*32];
void init(){
size=0;
memset(next,0,sizeof next);
memset(val,0,sizeof val);
}
void insert(ll x){
int cur=0;
for(int i=32;i>=0;i--){
int k=(x>>i)&1;
if(!next[cur][k]) next[cur][k]=++size;
cur=next[cur][k];
}
val[cur]=x;
}
ll find(ll x){
int cur=0;
for(int i=32;i>=0;i--){
int k=(x>>i)&1;
if(next[cur][k^1])cur=next[cur][k^1];
else cur=next[cur][k];
}
return val[cur];
}
}trie;
string s[maxn]; int main() {
IO;
cin>>casn;
int __=0;
while(casn--){
cin>>n>>m;
trie.init();
cout<<"Case #"<<++__<<":"<<endl;
while(n--){
ll x;
cin>>x;
trie.insert(x);
}
while(m--){
ll x;
cin>>x;
cout<<trie.find(x)<<endl;
}
}
return 0;
}

acdream 1063 代码

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=1e5+10,maxm=2e6+10;
int casn,n,m,k,num[maxn];
struct Trie{
int next[maxn*32][2],size;
ll val[maxn*32];
void init(){
size=0;
memset(next,0,sizeof next);
memset(val,0,sizeof val);
}
void insert(ll x){
int cur=0;
for(int i=32;i>=0;i--){
int k=(x>>i)&1;
if(!next[cur][k]) next[cur][k]=++size;
cur=next[cur][k];
}
val[cur]=x;
}
ll find(ll x,bool flag){
int cur=0;
for(int i=32;i>=0;i--){
int k=(x>>i)&1;
if(flag){
if(next[cur][k^1]) cur=next[cur][k^1];
else cur=next[cur][k];
}else {
if(next[cur][k])cur=next[cur][k];
else cur=next[cur][k^1];
}
}
return val[cur];
}
}trie; int main() {
IO;
cin>>casn;
while(casn--){
cin>>n;
trie.init();
while(n--){
ll x;
string s;
cin>>s>>x;
if(s=="insert") trie.insert(x);
else if(s=="qmin") cout<<(x^trie.find(x,0))<<endl;
else cout<<(x^trie.find(x,1))<<endl;
}
}
return 0;
}

hdu 4825 && acdream 1063 01字典树异或问题的更多相关文章

  1. HDU 4825 Xor Sum(字典树)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...

  2. [BZOJ4260] Codechef REBXOR (01字典树,异或前缀和)

    Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,-,AN. Output 输出一行包含给定表达式可能的最大值. Sample ...

  3. P4551 最长异或路径 (01字典树,异或前缀和)

    题目描述 给定一棵 n 个点的带权树,结点下标从 1 开始到 N .寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 输入输出格式 输入格式: 第一行一 ...

  4. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  5. hdu 4825 Xor Sum(01字典树模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...

  6. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

  7. Chip Factory---hdu5536(异或值最大,01字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...

  8. BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)

    <题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...

  9. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

随机推荐

  1. Linux记录-salt分析

    salt-master建立分组 如dn: 'L@dnxxx,dyyy' salt -N dn  state.apply  hadoop test=true salt -N dn  cmd.run  ' ...

  2. Java OPC 代码

    这是我项目的代码,还是容易理解的,记录备份一下 1.任务详情 Java实现OPC通信的代码实现 最终实现对西门子的S7-300的PLC变量的值读写 2.实现情况 使用utgard实现通信:Java实现 ...

  3. vue实现简单日历

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. javaScript ES5常考面试题总结

    js的六种原始值 boolean null undefined number string symbol 坑1: 首先原始类型存储的都是值,是没有函数可以调用的,比如 undefined.toStri ...

  5. Linux C 编程

    主题链接地址:https://www.cnblogs.com/kele-dad/category/1194627.html

  6. 最好用的js前端框架、组件、文档在线预览插件

    这里收集的都是个人认为比较好的js框架.组件 js前端ui框架 此处列举出个人认为最好的几个框架(排序即排名),现在好点的框架商用都需要付费,以下几个也不例外,但是由于组件丰富,都可以作为企业应用的完 ...

  7. Debian Security Advisory(Debian安全报告) DSA-4404-1 chromium

    Package : chromium CVE ID : CVE-2019-5786 Clement Lecigne在chromium的文件读取器实现中发现了一个use-after-free(释放后重用 ...

  8. 1.2 认识python(了解)

    一.Python发展背景 Python的作者,Guido von Rossum(吉多·范·罗苏姆,中国Python程序员都叫他 龟叔),荷兰人.1982年,龟叔从阿姆斯特丹大学获得了数学和计算机硕士学 ...

  9. :target方法实现切换

    <!DOCTYPE HTML><html><head> <title>:target切换</title> <meta charset= ...

  10. ubuntu 18.04 安装 Redis

    这篇博客写得不错,直接看这篇博客就OK了. https://wangxin1248.github.io/linux/2018/07/ubuntu18.04-install-redis.html