https://www.lydsy.com/JudgeOnline/problem.php?id=1552
I just want to fuck
This problem has take me almost two afternoons
It's my second time to write the Splay
an amazing Data structure
But it's too difficult to write
and the details are too many
but I finally AC this problem
and I'm so tire
because I debug hour by hour
the BZOJ 3506 is the same as this problem
for this problem
(the simplest problem in Splay Tree
you should add lazy label to a whole Tree
and floor by floor
and also take the Min position in his children
code of AC:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
const int inf=1<<30;
struct P{int x,y;}A[N];
bool cmp1(P a,P b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
bool cmp2(P a,P b){
return a.y<b.y;
}
int val[N],l[N],r[N],fa[N],Min[N],MinPos[N],cnt[N],rev[N],s[N];
int root,tot=0;
void pushdown(int x){
if(rev[x]){
swap(l[x],r[x]);
rev[l[x]]^=1;
rev[r[x]]^=1;
rev[x]^=1;
}
}
void update(int x){
int L=l[x],R=r[x];
cnt[x]=cnt[L]+cnt[R]+1;
Min[x]=min(val[x],min(Min[L],Min[R]));
if(Min[x]==val[x]) MinPos[x]=x;
else if(Min[x]==Min[L]) MinPos[x]=MinPos[L];
else MinPos[x]=MinPos[R];
}
void rotate(int x,int &k){
int y=fa[x];int z=fa[y];
int R=(r[y]==x);
if(y==k) k=x;
else{
if(l[z]==y) l[z]=x;
else r[z]=x;
}
if(R){
fa[x]=z;fa[y]=x;fa[l[x]]=y;
r[y]=l[x];
l[x]=y;
}else{
fa[x]=z;fa[y]=x;fa[r[x]]=y;
l[y]=r[x];
r[x]=y;
}
update(x);
update(y);
}
void splay(int x,int &k){
int top=0;s[++top]=x;
for(int i=x;fa[i];i=fa[i])
s[++top]=fa[i];
for(int i=top;i;i--)
if(rev[s[i]])pushdown(s[i]);
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if((l[y]==x)^(l[z]==y))
rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int p,int x){
pushdown(p);
int L=l[p],R=r[p];
if(cnt[L]+1==x){
return p;
}
if(cnt[l[p]]>=x) return find(l[p],x);
else return find(r[p],x-1-cnt[l[p]]);
}
void reverse(int L,int R){
int x=find(root,L),y=find(root,R+2);
splay(x,root);
splay(y,r[x]);
rev[l[y]]^=1;
}
int search(int L,int R){
int x=find(root,L);
int y=find(root,R+2);
splay(x,root);
splay(y,r[x]);
int z=MinPos[l[y]];
splay(z,root);
return cnt[l[z]];
}
void Insert(int x){
fa[root]=(++tot);
l[tot]=root;
val[tot]=x;
cnt[tot]=cnt[root]+1;
Min[tot]=min(x,Min[root]);
if(Min[tot]==x) MinPos[tot]=tot;
else MinPos[tot]=MinPos[root];
root=tot;
}
int main(){
int n;
cin>>n;
val[0]=inf;
Min[0]=inf;
Insert(inf);
for(int i=1;i<=n;++i){
scanf("%d",&A[i].x);
A[i].y=i;
}
sort(A+1,A+1+n,cmp1);
for(int i=1;i<=n;++i){
A[i].x=i;
}
sort(A+1,A+1+n,cmp2);
for(int i=1;i<=n;++i){
Insert(A[i].x);
}
Insert(inf);
for(int i=1;i<=n;++i){
int x=search(i,n);
printf("%d",x);
reverse(i,x);
if(i!=n) printf(" ");
else printf("\n");
}
}