[HEOI2013] ALO (persistence-Trie + chain)

You'll never believe a quick AFO player will not be persistent Trie.

In fact, XOR dumplings that question can be persistent Trie do, but I still useless, with the general Trie (in fact, may persistence is superfluous), so now still will not be persistent Trie.

This problem can be the first to discover requirement is a maximum, so a lot of the interval is not necessary, l1 [i] / r1 [i] represents a large number of left / right side than a [i], l2 [i] / r2 [i] means the second number greater than a [i] left / right, and then asks interval apparently [l2 [i] + 1, r1 [i] -1] and [l1 [i] + 1, r2 [i]] -1 both maximum value, then since the XOR interrogation maximum consecutive segments can be found is the persistence Trie board, as l [i], r [i], with the linked list maintenance, do not need set / treap other large constant practice.

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
const int N=50005;
int n,cnt,ans,a[N],rt[N],ch[N*32][2],sz[N*32],L[N],R[N];
pii b[N];
void build(int x,int id)
{
    int u,v=rt[id-1];u=rt[id]=++cnt;
    sz[u]=sz[v]+1;
    for(int i=29;~i;i--)
    {
        int c=x>>i&1;
        ch[u][c^1]=ch[v][c^1],ch[u][c]=++cnt;
        u=ch[u][c],v=ch[v][c],sz[u]=sz[v]+1;
    }
}
int query(int x,int l,int r)
{
    if(l>r)return 0;
    l=rt[l-1],r=rt[r];
    int ret=0;
    for(int i=29;~i;i--)
    {
        int c=x>>i&1;
        if(sz[ch[r][c^1]]-sz[ch[l][c^1]])ret+=1<<i,l=ch[l][c^1],r=ch[r][c^1];
        else l=ch[l][c],r=ch[r][c];
    }
    return ret;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),build(a[i],i),b[i]=pii(a[i],i),L[i]=i-1,R[i]=i+1;
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
    {
        int x=b[i].second,l=L[x],r=R[x];L[r]=l,R[l]=r;
        if(l)ans=max(ans,query(a[x],L[l]+1,r-1));
        if(r)ans=max(ans,query(a[x],l+1,R[r]-1));
    }
    printf("%d",ans);
}
View Code

 

Guess you like

Origin www.cnblogs.com/hfctf0210/p/10991674.html