Enumerate each number, calculate it as the maximum interval of the second large number, obviously, only need to use the answer of this interval to update the answer.
Find the number to the right of each number, the 1th to the left, 2 larger than it, and then discuss it in categories.
Find the process to put the number of sort after, from the big to the small place their position into set, a little maintenance can be.
#include <cstdio> #include <algorithm> #include <set>using namespace std; #define N 50001#define Maxbit 30set<int>t;typedef set<int>::iterator er;int root[n],ch[n* (maxbit+1)][2],sz[N* (MAXBIT+1)],tot;int Query (int l,int r,int W)//Ask A[l ... R] Medium W with its maximum XOR value {int ans=0; L=ROOT[L-1]; R=ROOT[R]; for (int i=maxbit-1;i>=0;--i) {int bit= (w>>i&1^1); if (sz[ch[r][bit]]-sz[ch[l][bit]]==0) bit^=1; else ans+=1<<i; R=ch[r][bit]; L=ch[l][bit]; } return ans; void Add (int now,int W)//First Add (0,0), then add (1...N,A[1...N]) {int old=root[now-1]; Root[now]=++tot; Now=root[now]; for (int i=maxbit-1;i>=0;--i) {int bit= (w>>i&1); sz[now]=sz[old]+1; CH[NOW][BIT^1]=CH[OLD][BIT^1]; Ch[now][bit]=++tot; Now=ch[now][bit]; Old=ch[old][bit]; } sz[now]=sz[old]+1;} int n,ans,b[n],ls[n],lls[n],rs[n],rrs[n];struct point{int x, y;}; BOOL operator < (const point &a,const point &b) {return a.x<b.x;} Point A[n];int Main () {scanf ("%d", &n), add (0,0), and for (int i=1;i<=n;++i) {scanf ("%d", &a[i].x); Add (i,a[i].x); A[i].y=i; b[i]=a[i].x; }sort (a+1,a+n+1); for (int i=n;i>=1;--i) {T.insert (A[I].Y); ER It=t.find (A[I].Y); if (It!=t.begin ()) {--it; Ls[a[i].y]=*it;if (It!=t.begin ()) {--it; Lls[a[i].y]=*it; ++it; }++it; } if ((++it)!=t.end ()) {rs[a[i].y]=*it;if ((++it)!=t.end ()) Rrs[a[i].y]=*it;else rrs[a[i].y]=n+1; }else rs[a[i].y]=rrs[a[i].y]=n+1; }for (int i=1;i<=n;++i) {if (Ls[i]) Ans=max (Ans,query (lls[i]+1,rs[i]-1,b[i))); else if (rrs[i]!=n+1) Ans=max (Ans,query (1,rrs[i]-1,b[i])); if (rs[i]!=n+1) Ans=max (Ans,query (Ls[i]+1,rrs[i]-1,b[i])); else if (Lls[i]) Ans=max (Ans,query (Lls[i]+1,n,b[i])); }printf ("%d\n", ans); return 0;}
Can persist Trie "set" bzoj3166 [Heoi2013]alo