HDU 4825 Xor sum
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的更多相关文章
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- hdu 4825 xor sum(字典树+位运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- HDU 4825 Xor Sum(字典树)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...
随机推荐
- [hdu5225][BC#40]Tom and permutation
好久没写题解了..GDKOI被数位DP教做人了一发,现在终于来填数位DP的大坑了>_<. 发现自己以前写的关于数位DP的东西...因为没结合图形+语文水平拙计现在已经完全看不懂了嗯. 看来 ...
- LuceneNet 实现快速大文件大数据查询
做过站内搜索的朋友应该对Lucene.Net不陌生,因为用普通的sql like查询肯定是不行的,太慢了. 首先说明的是--Lucene.Net只是一个全文检索开发包,不是一个成型的搜索引擎, 它的 ...
- HDU 1232 畅通工程(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出 ...
- Tomcat配置虚拟路径访问容器外的硬盘资源
问题: 如果tomcat中上传了很多的图片,会导致tomcat启动的时候会慢,所以应该把图片上传到tomcat容器外部 那么,问题来了: tomcat出于安全考虑,禁止了直接访问外部硬盘资源. 解决: ...
- dede后台登陆后一片空白的解决办法汇总
dede后台登陆后一片空白的第一种解决办法: 找到:include/common.inc.php文件,打开,查找程序代码://error_reporting(E_ALL);error_reportin ...
- Node.js/Vue环境搭配安装
http://blog.sina.com.cn/s/blog_497ff1a70102x0sw.html 第一次接触Node.js,想创建自己的服务就须配置好Node.js环境 安装Node.js 下 ...
- php中urldecode()和urlencode()起什么作用啊
urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%. urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其 ...
- 织梦dedecms中arclist标签下无法嵌套图片
版权声明:本文为博主原创文章,未经博主允许不得转载. 错误代码: {dede:arclist row=10 orderby=click titlelen=35} [field:title/] {/de ...
- 利用xcode生成的app生成可以在iphone和itouch上运行的ipa安装包
在编译好的真机版目录下的.app文件,至于生成真机可以运行的app的方法,有两种方式,一种是交99美元获得一个证书,另外一种是破解的方式,在此不再详述,本文假设你已经生成了真机上可以运行的app包了( ...
- 如何更改MyEclipse中XML文件的字体?
windows>Preferences>General>Appearance>Colors and Fonts>Basic>Text Font