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
5
9 2 1 4 7
Sample Output
14
HINT
【样例解释】
选择区间[1,5],最大值为 7 xor 9。
对于 100%的数据有 1 ≤ n ≤ 50000, 0 ≤ ai ≤ 10^9
可持久化字典树。
对于每个点我们需要预处理出左边右边第二个比他大的数
这个部分我们可以通过用set把所有的数从大到小插入
每次插入的时候他前驱的前驱和后缀的后缀就是我们要找的东西
可以显然的发现这个区间内都是合法的
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair<int,int>
#define Pil pair<int,ll>
#define Pli pair<ll,int>
#define Pll pair<ll,ll>
#define pb push_back
#define pc putchar
#define file(k) memset(k,0,sizeof(k))
#define ll long long
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror = 0;
inline char nc(){
static char buf[BUF_SIZE],*p1 = buf+BUF_SIZE, *pend = buf+BUF_SIZE;
if(p1 == pend){
p1 = buf; pend = buf+fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1){ IOerror = 1; return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror)return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
inline void read(ll &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror) return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace fastIO;
int n , Max , b[50010];
int root[50010] , l[50010] , r[50010];
struct trie{
int tot;
int ch[50010 * 31][2] , sum[50010*31];
int insert(int root,int last,int val)
{
int tmp = root = ++tot;
repp(i,29,0)
{
ch[root][0] = ch[last][0];ch[root][1] = ch[last][1];
int k = (val>>i)&1;
ch[root][k] = ++tot;
sum[root] = sum[last] + 1;
root = ch[root][k];
last = ch[last][k];
}
sum[root] = sum[last] + 1;
return tmp;
}
int query(int l,int r,int val)
{
int ans = 0;
repp(i,29,0)
{
int k = (val>>i)&1;
if(sum[ch[r][k^1]] - sum[ch[l][k^1]])
ans += 1<<i,
l = ch[l][k^1],
r = ch[r][k^1];
else l = ch[l][k] , r = ch[r][k];
}
return ans;
}
}trie;
struct data{
int num , id;
bool operator <(const data &a)const{
return num > a.num;
}
}a[50010];
set<int>hash;
set<int>::iterator it,now;
void pre()
{
read(n);
rep(i,1,n) read(a[i].num) , b[i] = a[i].num , a[i].id = i , root[i] = trie.insert(root[i],root[i-1],a[i].num);
sort(a+1,a+n+1);
rep(i,1,n)
{
hash.insert(a[i].id);
it = hash.lower_bound(a[i].id);
now = it;
rep(j,1,2) if(now != hash.end()) now++;
if(now != hash.end()) r[a[i].id] = *now-1;
else r[a[i].id] = n;
now = it;
if(now != hash.begin()) now--; else {l[a[i].id] = 1;continue;}
if(now != hash.begin()) now--; else {l[a[i].id] = 1;continue;}
l[a[i].id] = *now + 1;
}
Max = a[1].id;
return;
}
int main()
{
pre();
int ans = 0;
rep(i,1,n)
if(i != Max)
ans = max(ans , trie.query(root[l[i]-1] , root[r[i]] , b[i]));
printf("%d\n",ans);
return 0;
}