Splay 普通平衡樹模板

2022-05-31 04:12:12 字數 2021 閱讀 3180

口訣:

$rotate$:先上再下,最後自己

$splay$:祖父未到旋兩次,三點一線旋父親,三點折線旋自己。

$delete$:沒有兒子就刪光。單個兒子刪自己。兩個兒子找前驅。

易錯點:

$rotate$:祖父不在自己做根

$delete$:自己做根父親為0

$kth$:先減排名後轉移

/*

by dennyqi 2018

*/#include

#include

#include

#include

using

namespace

std;

typedef

long

long

ll;const

int maxn = 100010

;const

int inf = 0x3f3f3f3f

;inline

int max(const

int a, const

int b)

inline

int min(const

int a, const

int b)

inline

intread()

intn,opt,x,num_node;

int ch[maxn][2

],fa[maxn],val[maxn],size[maxn],cnt[maxn],root;

struct

splay

inline

void update(int

x) inline

void clear(int

x) inline

void rotate(int

x) inline

void splay(int x, int

target)

if(rson(gf,f) == rson(f,x)) rotate(f); else

rotate(x);

rotate(x);}}

inline

void insert(int

v)

for(;o;)

bool b = v>val[o];

if(!ch[o][b])

o = ch[o][v>val[o]];}}

inline

void find(int

v)

if(!ch[o][v>val[o]]) return

; }

}inline

void delete(int

v)

if(!ch[o][0] && !ch[o][1])

if(!ch[o][0])

if(!ch[o][1])

int l_max = ch[root][0

];

while(ch[l_max][1]) l_max = ch[l_max][1

]; splay(l_max, root);

ch[l_max][

1] = ch[root][1

]; fa[ch[root][

1]] =l_max;

fa[l_max]

= 0;

int pre_root =root;

root =l_max;

clear(pre_root);

}inline

int rnk(int

x) inline

int kth(int

k)

else}}

inline

int pre(int

v) inline

int nxt(int

v)}qxz;

intmain()

return0;

}

splay 普通平衡樹

平衡樹是一種很玄學的操作,這裡提供兩種基本模板 指標模板 這裡寫 片 include include include include include include using namespace std const int inf 1e9 const int max q 1e5 10 int n,...

(模板)Splay 平衡樹

不會講解,直接上板子,按照洛谷p3369的要求 include include include using namespace std const int maxn 1000000 int ch maxn 2 f maxn size maxn cnt maxn key maxn int nodecn...

平衡樹 Splay 模板

又是 機房最後乙個學spl ay splay splay的人 參考d al ao dalao dalao部落格 寫的非常好!最後放上我的 有個地方應該寫ch root 0 ch root 0 ch roo t 0 手殘寫成ch root 1 ch root 1 ch roo t 1 還查不出來t t...