题目链接

/*有一个数组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. Java 序列化 transient关键字

    Java 序列化 transient关键字 @author 敏敏Alexia 转自:http://www.cnblogs.com/lanxuezaipiao/p/3369962.html 1. tra ...

  2. B树(B-树)

    1.什么是B树(B-树)? B树是一种m阶树,m>=2 性质: 1)树中每个结点至多m个孩子: 2)对于根结点,子树个树取值范围为[2,m],关键字个数范围[1,m-1]: 3)对于非根非叶结点 ...

  3. ubuntu libtiff-dev

    cc@cc:~$ dpkg -L libti libtiff5 libtiffxx5 libtimezonemap1 libtinyxml2- libtiff5-dev libtimedate-per ...

  4. OMCS ——卓尔不群的网络语音视频框架

    作为.NET平台上的开发人员,要开发出一个像样视频聊天系统或视频会议系统,非常艰难,这不仅仅是因为.NET对多媒体的支持比较有限,还因为网络语音视频这块涉及到了很多专业方面的技术,而.NET在这些方面 ...

  5. 希望获取到页面中所有的checkbox怎么做?

    var domList = document.getElementsByTagName(‘input’); var checkBoxList = []; var len = domList.lengt ...

  6. web小技巧

    如内容超出单元格,则隐藏style="TABLE-LAYOUT: fixed" 让弹出窗口总是在最上面: <body onblur="this.focus();&q ...

  7. 五笔拼音反查精灵 v6.69 绿色版

    软件名称:五笔拼音反查精灵 v6.69 绿色版软件语言: 简体中文授权方式: 免费软件应用平台: Win7 / Vista / Win2003 / WinXP / Win2008 软件大小: 197K ...

  8. C - 哗啦啦村的扩建

    C - 哗啦啦村的扩建 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 512000/256000KB (Java/Others) Sub ...

  9. javacript参数传递表单验证

    <!doctype html> <html> <head> <meta charset="utf-8"> <style typ ...

  10. ajax 假上传文件

    1. <form name="certForm" id="certForm" method="post" action="x ...