题目链接

/*有一个数组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的更多相关文章

  1. HDU4776 Ants(Trie && xor)

    之前mark下来的一道题,今天填一下坑. 题意是这样子的.给你一棵边上有权的树.然后有树上两点(u,v)的路径有n*(n-1)条,路径(u,v)的权值是边权的xor. 然后下面有m个询问,询问你n*( ...

  2. Codeforces.842D.Vitya and Strange Lesson(Trie xor)

    题目链接 /* 异或只有两种情况,可以将序列放到01Tire树上做 在不异或的情况下在Tire上查找序列的mex很容易,从高位到低位 如果0位置上数没有满,则向0递归:否则向1 (0位置上的数都满了 ...

  3. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. [USACO]6.1.3 cow xor(二进制+Trie)

    题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...

  5. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  6. 51nod1295 XOR key(可持久化trie)

    1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...

  7. hdu 4825 Xor Sum (01 Trie)

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

  8. 51nod 1295 XOR key (可持久化Trie树)

    1295 XOR key  题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个长度为N的正整数数组A,再给出Q个查 ...

  9. 【xsy1147】 异或(xor) 可持久化trie

    我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...

随机推荐

  1. webpack-hot-middleware 用于 livereload

    https://github.com/glenjamin/webpack-hot-middleware Webpack hot reloading using only webpack-dev-mid ...

  2. Lucene基础(2)

    上一篇:Lucene基础(1) 一.Lucene术语 Document, Field, Term, Query, Analyzer相信在其中大多数在之前已经理解了...对其中部分概念详细说明 Docu ...

  3. JS学习之路,菜鸟总结的注意事项及错误更正

    JavaScript 是一种面向对象的动态语言,它的语法来源于 Java 和 C,所以这两种语言的许多语法特性同样适 用于 JavaScript.需要注意的一个主要区别是 JavaScript 不支持 ...

  4. HDU 2102 A计划(DFS)

    题目链接 Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主 ...

  5. ESFramework ——可堪重任的网络通信框架

    ESFramework是一套性能卓越.稳定可靠.强大易用的跨平台通信框架,支持应用服务器集群.其内置了消息的收发与自定义处理(支持同步/异步模型).消息广播.P2P通道.文件传送(支持断点续传).心跳 ...

  6. Cross Product

    Cross Product These are two vectors: They can be multiplied using the "Cross Product" (als ...

  7. Oracle Day04 子查询

    1.子查询解决什么问题: 当一个简单的查询查询不到结果的时候,可以使用子查询来丰富查询的条件以达到显示结果的目的. 子查询的格式: 用一个小括号包含,然后在里面写sql语句2.子查询的注意事项: 1) ...

  8. SELinux Policy Macros

    参考:http://selinuxproject.org/page/NB_RefPolicy Directory Macros macro expansion getattr_dir_perms ge ...

  9. PHP实现中文截取无乱码

    字符串的处理是编程中比较常见的,各种编程语言对字符串的处理也提供了大量函数,像php中mb_substr()函数可以实现对中文字符串的截取,如何使用自定义方法实现中文字符串截取无乱码这也是面试经常遇到 ...

  10. grub4dos新手指南-2

    Grub4dos 新手指南 一.GRUB4DOS的配置文件Grub4dos 有三个文件,grldr.grldr.mbr和menu.lst,配置文件是menu.lst,和GRUB一样.该文件一般放在和启 ...