Splay的初步学习

具体是啥,qwq

有时间再补吧,贴一下代码;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#define MAXN 10086666
using namespace std;
int f[MAXN],cnt[MAXN],value[MAXN];
int sons[MAXN][],sub_size[MAXN];
int root,whole_size;
int m,num,be_dealt;
inline int read()
{
int x = ;
int f = ;
char ch = getchar();
while(!isdigit(ch))
{
if(ch == '-')
f = -;
ch = getchar();
}
while(isdigit(ch))
{
x = x * + ch - ;
ch = getchar();
}
return x * f;
}
inline void S_clear(int x)
{
sons[x][] = sons[x][] = ;
f[x] = cnt[x] = value[x] = ;
sub_size[x] = ;
}
inline bool get_which(int x)
{
return sons[f[x]][] == x;
}
inline void update(int x)
{
if(x)
{
sub_size[x] = cnt[x];
if(sons[x][])
sub_size[x] += sub_size[sons[x][]];
if(sons[x][])
sub_size[x] += sub_size[sons[x][]];
}
return ;
}
inline void rotate(int x)
{
int father = f[x];
int g_father = f[father];
int which_son = get_which(x);
sons[father][which_son] = sons[x][which_son ^ ];
f[sons[father][which_son]] = father;
sons[x][which_son ^ ] = father;
f[father] = x;
f[x] = g_father;
if(g_father)
sons[g_father][sons[g_father][] == father] = x;
update(father);
update(x);
}
inline void splay(int x)
{
for(int fa;fa = f[x];rotate(x))
if(f[fa])
rotate((get_which(x)) == get_which(fa) ? fa : x);
root = x;
}
inline void insert(int x)
{
if(!root)
{
whole_size++;
sons[whole_size][] = sons[whole_size][] = f[whole_size] = ;
root = whole_size;
sub_size[whole_size] = cnt[whole_size]++;
value[whole_size] = x;
return ;
}
int now = root;
int fa = ;
while()
{
if(x == value[now])
{
cnt[now]++;
update(now);
update(fa);
splay(now);
break;
}
fa = now;
now = sons[now][value[now] < x];
if(!now)
{
whole_size++;
sons[whole_size][] = sons[whole_size][] = ;
f[whole_size] = fa;
sub_size[whole_size] = cnt[whole_size] = ;
sons[fa][value[fa] < x] = whole_size;
value[whole_size] = x;
update(fa);
splay(whole_size);
break;
}
}
}
inline int find_sum(int x)
{
int now = root;
while()
{
if(sons[now][] && x <= sub_size[sons[now][]])
now = sons[now][];
else
{
int temp = (sons[now][] ? sub_size[sons[now][]] : ) + cnt[now];
if(x <= temp)
return value[now];
x -= temp;
now = sons[now][];
}
}
}
inline int find_num(int x)
{
int now = root;
while()
{
if(sons[now][] && x <= sub_size[sons[now][]])
now = sons[now][];
else
{
int temp = (sons[now][] ? sub_size[sons[now][]] : ) + cnt[now];
if(x <= temp)
return value[now];
x -= temp;
now = sons[now][];
}
}
}
inline int find_rank(int x)
{
int now = root;
int ans = ;
while()
{
if(x < value[now])
now = sons[now][];
else
{
ans += (sons[now][] ? sub_size[sons[now][]] : );
if(x >= value[now])
{
splay(now);
return ans + ;
}
ans += cnt[now];
now = sons[now][];
}
}
}
inline int find_pre()
{
int now = sons[root][];
while(sons[now][])
now = sons[now][];
return now;
}
inline int find_suffix()
{
int now = sons[root][];
while(sons[now][])
now = sons[now][];
return now;
}
inline void my_delete(int x)
{
int kkk = find_rank(x);
if(cnt[root] > )
{
cnt[root]--;
update(root);
return ;
}
if(!sons[root][] && !sons[root][])
{
S_clear(root);
root = ;
return ;
}
if(!sons[root][])
{
int old_root = root;
root = sons[root][];
f[root] = ;
S_clear(old_root);
return ;
}
else
if(!sons[root][])
{
int old_root = root;
root = sons[root][];
f[root] = ;
S_clear(old_root);
return ;
}
int left_max = find_pre();
int old_root = root;
splay(left_max);
sons[root][] = sons[old_root][];
f[sons[old_root][]] = root;
S_clear(old_root);
update(root);
}
int main()
{
scanf("%d",&m);
for(int i=;i<=m;i++)
{
num = read();
be_dealt = read();
switch(num)
{
case : insert(be_dealt);break;
case : my_delete(be_dealt);break;
case : printf("%d\n",find_rank(be_dealt));break;
case : printf("%d\n",find_num(be_dealt));break;
case : insert(be_dealt);printf("%d\n",value[find_pre()]);my_delete(be_dealt);break;
case : insert(be_dealt);printf("%d\n",value[find_suffix()]);my_delete(be_dealt);break;
}
}
return ;
}
上一篇:使用Axure做验证码之校验验证码(二)


下一篇:html里文本编辑器如何制作呢?