Splay learning - I Splay fight sorting
Description
Given the number N, your task is to put the number of small to large order n ^ _ ^
Input
The first line of an integer, N (1 <= n < = 200000), the number N expressed
Then there are N rows, each row a number
Output
N output lines, N is the number of already sorted
Sample Input
5
2
3
1
4
5
Sample Output
1
2
3
4
5
The problem I believe we will use the sorting algorithm, the reason why I use Splay, because they can not on OJ splay version of the title. . .
As a result, I can only try to insert, Splay operation
Code
//请自行忽略这两行注释
//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int a[200010];
struct node{
int son[2],v,t,f,size;
node(){t=f=son[0]=son[1]=0;size=1;}
};
node no[200010];int cntn=1,root=1;
int getson(int x){return (int)(no[no[x].f].son[1]==x);}
void updata(int x)
{
no[x].size=no[no[x].son[0]].size+no[no[x].son[1]].size+1;
}
void rotate(int x)
{
if(!no[x].f) return ;
int dirx,dirfa,fa,gf;
fa=no[x].f, gf=no[fa].f;
dirx=getson(x),dirfa=getson(fa);
no[fa].son[dirx]=no[x].son[dirx^1];
no[no[fa].son[dirx]].f=fa;
no[x].son[dirx^1]=fa;
if(gf>0) no[gf].son[dirfa]=x;
no[fa].f=x,no[x].f=gf;
updata(fa);updata(x);
}
void splay(int x,int y)
{
int z;
while(no[x].f!=y) rotate(x);
updata(x);
if(y==0) root=x;
}
void ins(int v)
{
int x=root;
while(x)
{
if(v==no[x].v){no[x].t++;break;}
if(v<no[x].v)
{
if(!no[x].son[0]) ++cntn,no[x].son[0]=cntn,no[cntn].v=v,no[cntn].f=x;
x=no[x].son[0];
}
else
{
if(!no[x].son[1]) ++cntn,no[x].son[1]=cntn,no[cntn].v=v,no[cntn].f=x;
x=no[x].son[1];
}
}
splay(x,0);
}
void DFS(int x)
{
if(x==0) return ;
DFS(no[x].son[0]);
for(int i=1;i<=no[x].t;i++) printf("%d\n",no[x].v);
DFS(no[x].son[1]);
}
int main()
{
freopen("sort.in","r",stdin);
freopen("sort.out","w",stdout);
int n,i,u,v,t;
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&a[i]);
no[root].v=a[1],no[root].t=1;
for(i=2;i<=n;i++)
{
ins(a[i]);
}
DFS(root);
return 0;
}
Submitted after the OJ. . .
TLE0!?
Me: "It must be splay constant too."
Then opened the O2
Not yet? Oh, ambidextrous
void splay(int x,int y)
{
int z;
while(no[x].f!=y)
{
z=no[x].f;
if(no[z].f!=y&&getson(z)==getson(x)) rotate(z);
else rotate(x);
}
updata(x);
if(y==0) root=x;
}
I: emmm ...
Then being passed with a random exchange Gou
probably after a few days,
a small room appeared similar sound
A student: I Splay TLE up!
So I baidu a bit,
splay timeout
Splay的伸展
当我们把某一个点x一路旋转至某个位置时……我们可以考虑一次性进行两次旋转来让伸展之后的树更平衡。
我们一般进行如下的分类讨论:
若x的父节点即为伸展目标:直接旋转x即可。
若x的父节点不是伸展目标,且x、x的父亲、x的父亲的父亲三点共线
(即x是x的父亲的左儿子,x的父亲的父亲也是x的父亲的左儿子; 或x是x的父亲的右儿子,x的父亲的父亲也是x的父亲的右儿子)。
这时先旋转x的父亲,再旋转x。
若三点不共线,对x进行两次旋转即可。
额。。。
void splay(int x,int y)
{
int z;
while(no[x].f!=y)
{
z=no[x].f;
if(no[z].f!=y&&getson(z)==getson(x)) rotate(z);
//else rotate(x);
rotate(x);
}
updata(x);
if(y==0) root=x;
}
完整代码
//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int a[200010];
struct node{
int son[2],v,t,f,size;
node(){t=f=son[0]=son[1]=0;size=1;}
};
node no[200010];int cntn=1,root=1;
int getson(int x){return (int)(no[no[x].f].son[1]==x);}
void updata(int x)
{
no[x].size=no[no[x].son[0]].size+no[no[x].son[1]].size+1;
}
void rotate(int x)
{
if(!no[x].f) return ;
int dirx,dirfa,fa,gf;
fa=no[x].f, gf=no[fa].f;
dirx=getson(x),dirfa=getson(fa);
no[fa].son[dirx]=no[x].son[dirx^1];
no[no[fa].son[dirx]].f=fa;
no[x].son[dirx^1]=fa;
if(gf>0) no[gf].son[dirfa]=x;
no[fa].f=x,no[x].f=gf;
updata(fa);updata(x);
}
void splay(int x,int y)
{
int z;
while(no[x].f!=y)
{
z=no[x].f;
if(no[z].f!=y&&getson(z)==getson(x)) rotate(z);
rotate(x);
}
updata(x);
if(y==0) root=x;
}
void ins(int v)
{
int x=root;
while(x)
{
if(v==no[x].v){no[x].t++;break;}
if(v<no[x].v)
{
if(!no[x].son[0]) ++cntn,no[x].son[0]=cntn,no[cntn].v=v,no[cntn].f=x;
x=no[x].son[0];
}
else
{
if(!no[x].son[1]) ++cntn,no[x].son[1]=cntn,no[cntn].v=v,no[cntn].f=x;
x=no[x].son[1];
}
}
// Db();
splay(x,0);
// Db();
}
void DFS(int x)
{
if(x==0) return ;
DFS(no[x].son[0]);
for(int i=1;i<=no[x].t;i++) printf("%d\n",no[x].v);
DFS(no[x].son[1]);
}
int main()
{
freopen("sort.in","r",stdin);
freopen("sort.out","w",stdout);
int n,i,u,v,t;
scanf("%d",&n);
srand(19260817);
for(i=1;i<=n;i++) scanf("%d",&a[i]);
no[root].v=a[1],no[root].t=1;
for(i=2;i<=n;i++)
{
ins(a[i]);
}
DFS(root);
return 0;
}