trie树的异或和问题

本题是一道经典题,使用trie树维护所给出的集合,我们知道等比数列前n项的和比第n+1项小,所以本题可以使用贪心策略,对于每一个询问,我们从高位向低位匹配,寻找最大异或值,向下递归求解。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define LL unsigned long long
using namespace std;
const int MAXN=3200000+5;
LL init(){
LL rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return fh*rv;
}
struct node{
int nxt[2];
}trie[MAXN];
LL T,n,num,nume,m;
void ins(const LL id){
LL u=0;
for(int i=31;i>=0;i--){
bool v=(id>>i)&(LL)1;
if(!trie[u].nxt[v]) trie[u].nxt[v]=++nume;
u=trie[u].nxt[v];
}
}
LL query(const LL id){
LL u=0,ans=0;
for(int i=31;i>=0;i--){
bool v=(id>>i)&(LL)1;
if(trie[u].nxt[!v]){
ans=(ans<<1)+!v;
u=trie[u].nxt[!v];
}else{
ans=(ans<<1)+v;
u=trie[u].nxt[v];
}
}
return ans;
}
int main(){
freopen("in.txt","r",stdin);
T=init();
while(T--){
printf("Case #%d:\n",++num);
n=init();m=init();
for(int i=1;i<=n;i++){
int t=init();
ins(t);
}
for(int i=1;i<=m;i++){
int t=init();
printf("%d\n",query(t));
}
memset(trie,0,sizeof(trie));
nume=0;
}
fclose(stdin);
return 0;
}

HDU 4825 Xor sum的更多相关文章

  1. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  2. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  3. hdu 4825 Xor Sum (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...

  4. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  5. hdu 4825 Xor Sum trie树

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Proble ...

  6. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  7. hdu 4825 xor sum(字典树+位运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

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

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

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

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

  10. HDU 4825 Xor Sum(字典树)

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

随机推荐

  1. [高并发]抢红包设计(使用redis)

    假设一个需求,在某个预告活动中准备了10w个红包,100w人在某个时间点去开抢,每人只能抢1次,如何保证性能和准确性,下面我给出我的一个设计方案,抛砖引玉 分析瓶颈 查询用户是否已参与过活动 获取一个 ...

  2. 基础二 day4 日记

    1.list增删改查 l1 = [1,'alex',True,[1,2,3],(2,3,4),{'name':'alex'}]l1 = ['alex',True,'wusir','ritian','t ...

  3. Web应用的目录结构

    Web应用的目录结构 |- WebRoot :   web应用的根目录 |- 静态资源(html+css+js+image+vedio)|- WEB-INF :固定写法. |-classes: (可选 ...

  4. 从零开始学习前端开发 — 10、HTML5新标签及表单控件属性和属性值

    一.html5新增标签 1.结构性标签 header 定义网页的头部 nav 定义网页的导航 footer 定义网页的底部 section 定义网页的某个区域 article 定义网页中的一篇文章 a ...

  5. HTTP 405 错误 – 方法不被允许 (Method not allowed)【转载】

    介绍 HTTP 协议定义一些方法,以指明为获取客户端(如您的浏览器或我们的 CheckUpDown 机器人)所指定的具体网址资源而需要在 Web 服务器上执行的动作.则这些方法如下: OPTIONS( ...

  6. redis常见命令使用

    这篇经验主要介绍了Redis常见用的一些操作命令.这篇例子是在windows上操作的.linux类似.写的一些基础,大神就别看了. 工具/原料   redis windows 方法/步骤   1 可以 ...

  7. PhpStorm (强大的PHP开发环境)2017.2.4 附注册方法

    http://www.oyksoft.com/soft/40722.html?pc=1 最新版PhpStorm 2017正式版改进了PHP 7支持,改进代码完成功能. PhpStorm 是最好的PHP ...

  8. Python 3 利用机器学习模型 进行手写体数字识别

    0.引言 介绍了如何生成数据,提取特征,利用sklearn的几种机器学习模型建模,进行手写体数字1-9识别. 用到的四种模型: 1. LR回归模型,Logistic Regression 2. SGD ...

  9. [拾 得]pipe和xargs的恩怨情仇

    Photo by Joshua Sortino on Unsplash   坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/  ...

  10. jQuery中foreach的continue和break

    摘录自:http://blog.csdn.net/penginpha/article/details/12159389 1. continue. 可以使用return. $("***&quo ...