您的位置:首页 > 其它

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

2016-05-15 12:54 429 查看
枚举每个点 处理出它能作为次大值的区间

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

“按照权值的倒序,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
;
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
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: