[可持久化字典树 set] BZOJ 3166 [Heoi2013]Alo

枚举每个点 处理出它能作为次大值的区间

然后就是数据结构的问题了


按照权值的倒序,set维护位置,依次插入,则x的可行区间为[x前驱的前驱+1,x后继的后继-1](两半合起来)” ——hzwer


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
using namespace std;
 
inline char nc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
    return *p1++;
}
 
inline void read(int &x)
{
    char c=nc(),b=1;
    for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
    for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
 
inline void read(char &x)
{
    for (x=nc();x!='Q' && x!='A';x=nc());
}
 
const int N=50005;
const int K=30;
 
int ncnt;
int root[N];
int ch[N*(K+5)][2],sum[N*(K+5)];
 
inline void insert(int &rt,int x,int t){
    rt=++ncnt; 
    int y; sum[y=rt]=sum[x]+1;
    for (int i=K;~i;i--)
    {
        if ((t>>i)&1)
            ch[y][0]=ch[x][0],y=(ch[y][1]=++ncnt),x=ch[x][1];
        else
            ch[y][1]=ch[x][1],y=(ch[y][0]=++ncnt),x=ch[x][0];
        sum[y]=sum[x]+1;
    }
}
 
inline int query(int l,int r,int t){
    int ret=0,tmp;
    for (int i=K;~i;i--)
    {
        tmp=(t>>i)&1;
        if (sum[ch[r][tmp^1]]-sum[ch[l][tmp^1]])
            ret+=(1<<i),r=ch[r][tmp^1],l=ch[l][tmp^1];
        else
            r=ch[r][tmp],l=ch[l][tmp];
    }
    return ret;
}
 
int n;
 
struct abcd{
    int pos,val;
    bool operator <(const abcd &B) const{
        return val>B.val;
    }
}ege[N];
 
int ans=0;
set<int> Set;
typedef set<int>::iterator ITER;
 
int main()
{
    int l,r;
    read(n);
    for (int i=1;i<=n;i++)
    {
        read(ege[i].val); ege[i].pos=i;
        insert(root[i],root[i-1],ege[i].val);
    }
    Set.insert(-2); Set.insert(-1);
    Set.insert(1<<30); Set.insert((1<<30)+1);
    sort(ege+1,ege+n+1);
    Set.insert(ege[1].pos);
    for (int i=2;i<=n;i++)
    {
        ITER p,q;
        p=q=Set.lower_bound(ege[i].pos);
        r=ege[i].pos;   
        r=*q; q++; r=*q-1;
        l=ege[i].pos;   
        l=*--p; p--; l=*p+1;
        l=max(1,l); r=min(r,n);
        if (l!=r) 
            ans=max(ans,query(root[l-1],root[r],ege[i].val));
        Set.insert(ege[i].pos);
    }
    printf("%d\n",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值