Trie/Xor
/*有一个数组a1,a2,a3……an。找到一个连续子段[l,r],使得al ^ al+1 ^……^ ar达到最大。
一般思路:维护前缀异或+暴力;
for(int i=1;i<=n;i++)
a[i]^=a[i-1];
for(int i=1;i<=n;i++)
for(int j=1;j<i;i++)
ans=max(ans,a[i]^a[j]);
数据量很大,暴力不行。
维护前缀异或+Trie优化;
*/
//指针版本
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000000+5;
const int si=23;
int a[maxn];
int n;
struct node
{
node *chi[2];
void init()
{
chi[0]=NULL;
chi[1]=NULL;
}
};
void Insert(int k,node * root)
{
for(int i=si;i>=0;i--)
{
if((k&(1<<i)))
{
if(root->chi[1]==NULL)
{
node *t=(node *)malloc(sizeof(node));
t->init();
root->chi[1]=t;
}
root=root->chi[1];
}
else
{
if(root->chi[0]==NULL)
{
node *t=(node *)malloc(sizeof(node));
t->init();
root->chi[0]=t;
}
root=root->chi[0];
}
}
}
int query(int k,node *root)
{
int ret=0;
for(int i=si;i>=0;i--)
{
if(k&(1<<i))//找0
{
if(root->chi[0]!=NULL)
{
root=root->chi[0];
}
else
{
root=root->chi[1];
ret+=(1<<i);
}
}
else//找1
{
if(root->chi[1]!=NULL)
{
root=root->chi[1];
ret+=(1<<i);
}
else
{
root=root->chi[0];
}
}
}
return k^ret;
}
void rease(node * root)
{
if(root->chi[0]!=NULL)
rease(root->chi[0]);
if(root->chi[1]!=NULL)
rease(root->chi[1]);
delete root;
}
int main ()
{
while(~scanf("%d",&n))
{
node *root=(node *)malloc(sizeof(node));
root->init();
Insert(0,root);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-1];
Insert(a[i], root);
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,query(a[i], root));
}
printf("%d\n",ans);
rease(root);
}
return 0;
}
//数组版本
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000000+5;
const int si=23;
int a[maxn];
int tree[maxn*3][2];
int cnt=0;
int n;
void Insert(int k)
{
int root=0;
for(int i=si;i>=0;i--)
{
if((k&(1<<i)))
{
if(tree[root][1]==0)
{
cnt++;
tree[root][1]=cnt;
}
root=tree[root][1];
}
else
{
if(tree[root][0]==0)
{
cnt++;
tree[root][0]=cnt;
}
root=tree[root][0];
}
}
}
int query(int k)
{
int root=0;
int ret=0;
for(int i=si;i>=0;i--)
{
if(k&(1<<i))//找0
{
if(tree[root][0]!=0)
{
root=tree[root][0];
}
else
{
root=tree[root][1];
ret+=(1<<i);
}
}
else//找1
{
if(tree[root][1]!=0)
{
root=tree[root][1];
ret+=(1<<i);
}
else
{
root=tree[root][0];
}
}
}
return k^ret;
}
int main ()
{
while(~scanf("%d",&n))
{
memset(tree,0,sizeof(tree));
cnt=0;
Insert(0);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]^=a[i-1];
Insert(a[i]);
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,query(a[i]));
}
printf("%d\n",ans);
}
return 0;
}
Trie/Xor的更多相关文章
- HDU4776 Ants(Trie && xor)
之前mark下来的一道题,今天填一下坑. 题意是这样子的.给你一棵边上有权的树.然后有树上两点(u,v)的路径有n*(n-1)条,路径(u,v)的权值是边权的xor. 然后下面有m个询问,询问你n*( ...
- Codeforces.842D.Vitya and Strange Lesson(Trie xor)
题目链接 /* 异或只有两种情况,可以将序列放到01Tire树上做 在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归:否则向1 (0位置上的数都满了 ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- [USACO]6.1.3 cow xor(二进制+Trie)
题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- 51nod1295 XOR key(可持久化trie)
1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
- 【xsy1147】 异或(xor) 可持久化trie
我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...
随机推荐
- AOE 网络
1.定义 如果在无向环的带权有向图中 - 用有向边表示一个工程中的活动 - 用边上的权值表示活动的持续时间 - 用顶点表示事件 则这样的有向图叫做用边表示活动的网络,简称AOE网络 AOE在工程方面非 ...
- Infix to postfix without '(' and ')'
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- 2016NEFU集训第n+5场 A - Chinese Girls' Amusement
Description You must have heard that the Chinese culture is quite different from that of Europ ...
- erlang-string
string:len("abcdef"). 结果为 6 求字符串的长度 string:equal("abc","abc"). 结果为 tru ...
- Oracle Day04 子查询
1.子查询解决什么问题: 当一个简单的查询查询不到结果的时候,可以使用子查询来丰富查询的条件以达到显示结果的目的. 子查询的格式: 用一个小括号包含,然后在里面写sql语句2.子查询的注意事项: 1) ...
- RSA 公私钥 互换问题
关于 RSA,我的理解是: 按定义的话,公私钥是可以互换的 问题是常见的实现里面,保存“私钥”的那个数据结构或者文件,里面除了私钥所必需的那一对数之外,还有额外的信息(足以算出公钥来),所以绝对不能把 ...
- 图片(img标签)大小自适应
$(function(){ var myimg,oldwidth,oldheight; var maxwidth=249; var maxheight=187; var imgs = document ...
- git远程分支--remote
查看所有远程引用: $ git ls-remote From ssh://someone@example/testgit ebf3ef7551603cd57a699e80db0bfab36d1aa7b ...
- word转pdf swf 在线预览
来源:http://www.cnblogs.com/wuhenke/archive/2010/08/01/1789750.html 之前在项目中研究使用了一套word转PDF,然后将PDF转成SWF的 ...
- ubuntu 连接VPN 命令
1 注册MXVPN 2 启动VPN pptpsetup --create mxvpn1 --server xx.xx.xx.xx --username *** --password *** --e ...