题目链接

/*有一个数组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. 自定义MVC框架(一)-(没有基于xml的)

    0.创建oracle的sql语句如下 --创建表 create table userinfo(id number primary key,uname varchar2(20),password var ...

  2. ejabberd组成模块

    转自:http://blog.sina.com.cn/u/1776260990 mod_adhoc 特定命令 (XEP-0050) mod_announce 管理公告推荐 mod_adhoc mod_ ...

  3. 【Machine Learning in Action --3】决策树ID3算法预测隐形眼睛类型

    本节讲解如何预测患者需要佩戴的隐形眼镜类型. 1.使用决策树预测隐形眼镜类型的一般流程 (1)收集数据:提供的文本文件(数据来源于UCI数据库) (2)准备数据:解析tab键分隔的数据行 (3)分析数 ...

  4. ubuntn安装五笔输入法

    1.首先要把原来的 ibus 卸载掉 sudo apt-get remove ibus 2.添加源 sudo add-apt-repository ppa:fcitx-t(www.111cn.net) ...

  5. 拦截asp.net mvc输出流做处理, 拦截HTML文本(asp.net MVC版)

    以前的一个贴子写过一个webForm的拦截HTML输出流的版本,最近用到mvc时用同样的方式发生一些问题. 如下图 查了好久也不知道啥原因. 好吧, 我最后选择放弃. 想起以前自定义Response. ...

  6. 在线GET/POST API接口请求模拟测试工具

    在前后端开发过程中经常需要对HTTP接口进行测试,推荐几款比较好用的测试工具 Postman https://www.getpostman.com/ 强大的HTTP请求测试工具 支持多平台 Advan ...

  7. VC MFC工具栏(CToolBar)控件

    一.工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创 ...

  8. Chapter 2 Open Book——16

    By Friday I was perfectly comfortable entering my Biology class, nolonger worried that Edward would ...

  9. Chapter 1 First Sight——33

    At that moment, the bell rang loudly, making me jump, and Edward Cullen was out of his seat. 在这个时候,铃 ...

  10. html标签全称和功能介绍

    html标签全称和功能介绍,里面有些大家不常用的,主要是方便大家查找检索   按字母顺序排列 DTD:指示在哪种 XHTML 1.0 DTD 中允许该标签.S=Strict, T=Transition ...