bzoj3506 [Cqoi2014]排序机械臂
bzoj3506
此题是一道比较简单的spaly题目。
用splay维护序列,将每个点排到对应的位置之后删除,这样比较容易区间翻转。
我的指针写法在洛谷上AC了,但在bzoj上RE。
#include <cstdio>
#include <algorithm>
using namespace std;
inline int gi() {
register int a; register char c;
while(c=getchar(),c<'0');a=c-'0';
while(c=getchar(),'-'<c)a=(a<<3)+(a<<1)+c-'0';
return a;
}
#define N 100010
struct node {
int s; bool ly;
node*fa,*ch[2];
}MSET[N],*cnode=MSET,*mp[N],*nil,*rt;
pair<int,int>pr[N]; int rk[N];
inline void maintain(node*u) {u->s=1+u->ch[0]->s+u->ch[1]->s;}
inline void reverse(node*u) {u->ly^=1,swap(u->ch[0],u->ch[1]);}
inline void pushdown(node*u) {
if(!u->ly)return;
u->ly=false;
if(u->ch[0]!=nil)reverse(u->ch[0]);
if(u->ch[1]!=nil)reverse(u->ch[1]);
}
inline void rot(node*u) {
node*f=u->fa,*ff=f->fa;
int d=u==f->ch[1];
pushdown(f),pushdown(u);
if((f->ch[d]=u->ch[d^1])!=nil)f->ch[d]->fa=f;
if((u->fa=ff)!=nil)ff->ch[f==ff->ch[1]]=u;
u->ch[d^1]=f,f->fa=u;
maintain(f),maintain(u);
}
inline void splay(node*u,node*target) {
for(node*f;u->fa!=target;rot(u))
if((f=u->fa)->fa!=target)((u==f->ch[1])^(f==f->fa->ch[1]))?rot(u):rot(f);
if(target==nil) rt=u;
}
inline void del(node*u) {
splay(u,nil);
pushdown(u);
if(u->ch[0]==nil) rt=u->ch[1],rt->fa=nil;
else {
node*v=u->ch[0];
for(pushdown(v);v->ch[1]!=nil;pushdown(v=v->ch[1]));
splay(v,u);
if((v->ch[1]=u->ch[1])!=nil)v->ch[1]->fa=v;
v->fa=nil; maintain(rt=v);
}
}
node*build(int l,int r) {
if(l>r)return nil;
int m=l+r>>1;
node*u=cnode++;
mp[rk[m]]=u;
if((u->ch[0]=build(l,m-1))!=nil)u->ch[0]->fa=u;
if((u->ch[1]=build(m+1,r))!=nil)u->ch[1]->fa=u;
maintain(u);
}
int main() {
int n,i;
scanf("%d",&n);
for(i=1;i<=n;++i)scanf("%d",&pr[i].first),pr[i].second=i;
sort(pr+1,pr+n+1);
for(i=1;i<=n;++i)rk[pr[i].second]=i;
rk[n+1]=n+1;
nil=cnode++;
nil->ch[0]=nil->ch[1]=nil->fa=nil;
nil->s=nil->ly=0;
rt=build(0,n+1); rt->fa=nil;
for(i=1;i<=n;++i) {
splay(mp[i],nil);
printf("%d ",rt->ch[0]->s+i-1);
reverse(rt->ch[0]);
del(rt);
}return 0;
}
数组写法在bzoj也AC了。
#include <cstdio>
#include <algorithm>
using namespace std;
inline int gi() {
static int a; static char c;
while(c=getchar(),c<'0');a=c-'0';
while(c=getchar(),'-'<c)a=(a<<3)+(a<<1)+c-'0';
return a;
}
#define N 100010
int fa[N],ch[2][N],s[N],rt;
bool flag[N]; pair<int,int>a[N];
inline void reverse(const int&u) {flag[u]^=1; swap(ch[0][u],ch[1][u]);}
inline void down(const int&u) {
if(!flag[u])return;
flag[u]=false;
if(ch[0][u])reverse(ch[0][u]);
if(ch[1][u])reverse(ch[1][u]);
}
inline void up(const int&u) {s[u]=1+s[ch[0][u]]+s[ch[1][u]];}
inline void rot(const int&u) {
static int f,d,t;
f=fa[u],d=u==ch[1][f];
down(f),down(u);
(t=ch[d][f]=ch[d^1][u])?fa[t]=f:0;
(t=fa[u]=fa[f])?ch[f==ch[1][t]][t]=u:0;
fa[f]=u,ch[d^1][u]=f;
up(f),up(u);
}
inline void splay(int u,const int&target) {
for(int t;fa[u]^target;rot(u))
if(fa[t=fa[u]]^target) ((u==ch[1][t])^(t==ch[1][fa[t]]))?rot(u):rot(t);
if(!target) rt=u;
}
inline void del(int u) {
splay(u,0); down(u);
if(!ch[0][u]||!ch[1][u])fa[rt=ch[0][u]+ch[1][u]]=0;
else {
int t=ch[0][u];
for(down(t);ch[1][t];down(t))t=ch[1][t];
splay(t,u);
fa[ch[1][t]=ch[1][u]]=t;
fa[t]=0; rt=t;
up(t);
}
}
int build(int l,int r) {
if(l>r) return 0;
int m=l+r>>1;
s[m]=r-l+1;
if(ch[0][m]=build(l,m-1))fa[ch[0][m]]=m;
if(ch[1][m]=build(m+1,r))fa[ch[1][m]]=m;
return m;
}
int main() {
register int n=gi(),i;
for(i=1;i<=n;++i)a[i].first=gi(),a[i].second=i;
sort(a+1,a+n+1);
rt=build(1,n);
for(i=1;i<=n;++i) {
splay(a[i].second,0);
reverse(ch[0][rt]);
printf("%d ",s[ch[0][rt]]+i);
del(rt);
}
return 0;
}