【ChairTrie】【bzoj 3166】: [Heoi2013]Alo

来源:互联网 发布:百度贴吧有mac版吗 编辑:程序博客网 时间:2024/05/21 10:04

http://www.lydsy.com/JudgeOnline/problem.php?id=3166


我又SB地把数组开小了一次。。。。。。。。。问题是手写大数据没有RE!!!!是神马情况!!!?


学习到了O(n^2)优化到几乎O(n)统计第二个比a[i]大的数

UPD

事实上是O(nalpha(n))

//#define _TEST _TEST#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <cmath>#include <algorithm>using namespace std;/************************************************Code By willinglive    Blog:http://willinglive.cf************************************************/#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)#define MS(arr,x) memset(arr,x,sizeof(arr))#define LL long long#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)inline const int read(){    int r=0,k=1;char c=getchar();    for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;    for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';    return k*r;}/////////////////////////////////////////////////const int N=1600000;int n;int a[50010];int L1[50010],R1[50010];int L2[50010],R2[50010];int s[N],top;int root[N],sz;int ls[N],rs[N];/////////////////////////////////////////////////void insert(int &o,int val,int d,int last){o=++sz; ls[o]=ls[last]; rs[o]=rs[last]; s[o]=s[last]+1;if(d<0) return;if(val&(1<<d)) insert(rs[o],val,d-1,rs[last]);else   insert(ls[o],val,d-1,ls[last]);}int query(int l,int r,int val){int res=val;int x=root[l-1],y=root[r];int d=29;do{int p=(val>>d)&1;if(p){if(s[ls[y]]-s[ls[x]]>0) y=ls[y],x=ls[x];else         res^=1<<d,y=rs[y],x=rs[x];}else{if(s[rs[y]]-s[rs[x]]>0) res^=1<<d,y=rs[y],x=rs[x];else         y=ls[y],x=ls[x];}}while(d--);return res;}/////////////////////////////////////////////////void input(){    n=read();    rep(i,1,n) a[i]=read();}void solve(){    top=0;    rep(i,1,n)    {    while(top&&a[i]>=a[s[top]])top--;    L1[i]=top?s[top]:0;    s[++top]=i;        L2[i]=0;    for(int j=L1[i]-1;j>=1;j=L1[j])    {    if(a[j]>a[i]){L2[i]=j;break;}    }    }    top=0;    per(i,n,1)    {    while(top&&a[i]>=a[s[top]])top--;    R1[i]=top?s[top]:n+1;    s[++top]=i;        R2[i]=n+1;    for(int j=R1[i]+1;j<=n;j=R1[j])    {    if(a[j]>a[i]){R2[i]=j;break;}    }    }    int ans=0;    rep(i,1,n) insert(root[i],a[i],29,root[i-1]);    rep(i,1,n)    {    if(L1[i]) ans=max(ans,query(L2[i]+1,R1[i]-1,a[i]));    if(R1[i]<=n) ans=max(ans,query(L1[i]+1,R2[i]-1,a[i]));    }    printf("%d\n",ans);}/////////////////////////////////////////////////int main(){    #ifndef _TEST    freopen("std.in","r",stdin); freopen("std.out","w",stdout);    #endif    input(),solve();    return 0;}


0 0