BZOJ 3166: [Heoi2013]Alo 可持久化字典树
2017-07-24 18:39
405 查看
3166: [Heoi2013]Alo
Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1071 Solved: 495
[Submit][Status][Discuss]
Description
Welcome to ALO ( Arithmetic and Logistic Online)。这是一个VR MMORPG ,如名字所见,到处充满了数学的谜题。
现在你拥有n颗宝石,每颗宝石有一个能量密度,记为ai,这些宝石的能量
密度两两不同。现在你可以选取连续的一些宝石(必须多于一个)进行融合,设为 ai, ai+1, …, a j,则融合而成的宝石的能量密度为这些宝石中能量密度的次大值
与其他任意一颗宝石的能量密度按位异或的值,即,设该段宝石能量密度次大值
为k,则生成的宝石的能量密度为max{k xor ap | ap ≠ k , i ≤ p ≤ j}。
现在你需要知道你怎么选取需要融合的宝石,才能使生成的宝石能量密度最大。
Input
第一行,一个整数 n,表示宝石个数。第二行, n个整数,分别表示a1至an,表示每颗宝石的能量密度,保证对于i ≠ j有 ai ≠ aj。
Output
输出一行一个整数,表示最大能生成的宝石能量密度。Sample Input
59 2 1 4 7
Sample Output
14HINT
【样例解释】选择区间[1,5],最大值为 7 xor 9。
对于 100%的数据有 1 ≤ n ≤ 50000, 0 ≤ ai ≤ 10^9
可持久化字典树
话说我好像不想学高级写法了。。
继续用我的主席树写法吧。。
纵使跑的稍微慢点。。
对于一个区间,所获的价值为max(次大值 xor a[p])
考虑对于一个区间的处理。。。算了吧。。。光是枚举区间就够喝一壶的了。。。
发现区间的特征:
区间n^2个,但是次大值只有n-1个 这就是切入点
枚举每个次大值
对于x能与它构成新答案的区间[向左的二个大于x的数+1,向右的二个大于x的数-1]
单调队列好像搞不了 线段树一定可以
但是还有更棒棒的解法
预处理插入结束后 把数列由大到小排列
用set维护下标
插入最大值
之后由大到小插入,每次询问,set中的下标所指元素一定大于询问的元素
所以询问区间为[前驱的前驱+1,后继的后继-1]
觉的想法比较小巧,当然 最重要的是好写
刚开始看错题以为异或的是前缀和(整个区间)。。瞎YY半天
但是十分喜闻乐见1A了 哈哈
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<48||ch>57){if(ch=='-')f=-1;ch=getchar();}
while(ch<=57&&ch>=48){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+48);}
const int N=50100,inf=0X3f3f3f3f;
struct P{int val,pos;}a
;
inline bool cmp(const P &x,const P &y){return x.val>y.val;}
struct president_tree{int sz,ls,rs;}tr[N<<5];
int root
,sz;
void insert(int &k,int x,int pos,int val)
{
k=++sz;tr[k].sz=tr[x].sz+1;
if(!(~pos))return ;tr[k].ls=tr[x].ls;tr[k].rs=tr[x].rs;
(val&(1<<pos))?insert(tr[k].rs,tr[x].rs,pos-1,val):insert(tr[k].ls,tr[x].ls,pos-1,val);
}
int query(int x,int y,int val)
{
register int l=root[x],r=root[y],pos=30,res=0;
while(~pos)
{
if(val&(1<<pos))
{
if(tr[tr[r].ls].sz-tr[tr[l].ls].sz)
{res+=1<<pos;l=tr[l].ls;r=tr[r].ls;}
else l=tr[l].rs,r=tr[r].rs;
}
else
{
if(tr[tr[r].rs].sz-tr[tr[l].rs].sz)
{res+=1<<pos;l=tr[l].rs;r=tr[r].rs;}
else l=tr[l].ls,r=tr[r].ls;
}
pos--;
}
return res;
}
set<int>st;
int main()
{
int n=read();
register int i,x,y,ans=0;
for(i=1;i<=n;++i)a[i].val=read(),a[i].pos=i;
for(i=1;i<=n;++i)insert(root[i],root[i-1],30,a[i].val);
sort(a+1,a+1+n,cmp);
st.insert(a[1].pos);st.insert(-2);st.insert(-1);st.insert(inf+1);st.insert(inf);
register set<int>::iterator l,r;
for(i=2;i<=n;++i)
{
l=st.lower_bound(a[i].pos);r=st.lower_bound(a[i].pos);
r++;*r>=inf?y=n:y=*r-1;
l--;l--;*l<0?x=1:x=*l+1;
ans=max(ans,query(x-1,y,a[i].val));
st.insert(a[i].pos);
}
print(ans);puts("");
return 0;
}
/*
5
9 2 1 4 7
14
*/
相关文章推荐
- [BZOJ3166][HEOI2013]Alo-可持久化字典树
- bzoj 3166 [Heoi2013]Alo 可持久化字典树
- [可持久化字典树 set] BZOJ 3166 [Heoi2013]Alo
- 【可持久化Trie】【set】bzoj3166 [Heoi2013]Alo
- BZOJ3166 [Heoi2013]Alo 【可持久化trie树 + 二分 + ST表】
- BZOJ 3166 HEOI2013 Alo 可持久化Trie树
- 【BZOJ 3166】[Heoi2013]Alo 可持久化trie树+set
- [BZOJ3166][Heoi2013]Alo 可持久化Trie树
- 【bzoj3166】[Heoi2013]Alo 可持久化Trie树+STL-set
- BZOJ 3166: [Heoi2013]Alo|可持久化Trie树
- BZOJ3166 [Heoi2013]Alo 可持久化Trie
- 【BZOJ3166】[Heoi2013]Alo 可持久化Trie树+set
- BZOJ 3166: [Heoi2013]Alo
- BZOJ 3166 [Heoi2013]Alo
- bzoj3166: [Heoi2013]Alo
- BZOJ3166: [Heoi2013]Alo
- 【bzoj3166】【HEOI2013】【Alo】【set+可持久化trie】
- bzoj 3166: [Heoi2013]Alo (可持久化trie+线段树)
- 【BZOJ】【P3166】【Heoi2013】【Alo】【题解】【可持久化Trie+set】
- bzoj 3166 [Heoi2013]Alo 可持久化trie 线段树