http://www.lydsy.com/JudgeOnline/problem.php?id=1588
My first splay, an array of bottom-up implementations.
Test instructions is to give you a set of numbers, which is the absolute value of the difference between each number and the number closest to it in front of it.
Consider the characteristics of the splay two-fork search tree, where each newly inserted node is smaller and closest to its maximum value in the left subtree, and the other half is the same.
Code reference from: http://blog.csdn.net/ACM_cxlove?viewmode=contents
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intMAXN = 1e5 +Ten;6 Const intINF =0x3f3f3f3f;7 intPRE[MAXN], KEY[MAXN], ch[maxn][2], root, size;8 intN;9 Ten voidNew_node (int&r,intFatherintk) { OneR = + +size; APRE[R] =father; -key[r]=K; -ch[r][0] = ch[r][1] =0; the } - - voidRotateintXBOOLD) {//d = 1, right rotate - inty =Pre[x]; +CH[Y][!D] =Ch[x][d]; -PRE[CH[X][D]] =y; + if(Pre[y]) ch[pre[y]][ch[pre[y]][1] = = Y] =x; A //if Y is not the root, link X has a grandparent atPRE[X] =Pre[y]; -CH[X][D] =y; -Pre[y] =x; - } - - voidSplay (intUintdest) { in //root u to root dest - while(Pre[u]! =dest) { to if(Pre[pre[u]] = = dest) rotate (U, ch[pre[u]][0] ==u); + Else{ - inty =Pre[u]; the intD = ch[pre[y]][0] ==y; * if(Ch[y][d] = = u) {//Zig-zag $Rotate (U,!)d);Panax Notoginseng rotate (U, d); -}Else{ theRotate (y, D);//Zig-zig + rotate (U, d); A } the } + } - if(!dest) root =u; $ } $ - intInsertintk) { - intU =Root; the while(Ch[u][key[u] <K]) { - if(Key[u] = = k)returnSplay (U,0),0;WuyiU = Ch[u][key[u] <K]; the } - if(Key[u] = = k)returnSplay (U,0),0; WuNew_node (Ch[u][key[u) <K], u, k); -Splay (Ch[u][key[u] < K],0); About return 1; $ } - - intGet_pre (intx) { - intTEM = ch[x][0]; A if(!tem)returninf; + while(ch[tem][1]) tem = ch[tem][1]; the returnKEY[X]-Key[tem]; - } $ the intGet_next (intx) { the intTEM = ch[x][1]; the if(TEM = =0)returninf; the while(ch[tem][0]) tem = ch[tem][0]; - returnKey[tem]-Key[x]; in } the the intMain () { About //freopen ("In.txt", "R", stdin); the while(~SCANF ("%d", &N)) { theroot = size =0; the intAns =0; + for(inti =1; I <= N; i++){ - intnum; the if(SCANF ("%d", &num) = = EOF) num =0;Bayi if(i = =1){ theAns + =num; theNew_node (Root,0, num); - Continue; - } the if(!insert (num))Continue; the intA =Get_next (root); the intb =Get_pre (root); theAns + =min (A, b); - } theprintf"%d\n", ans); the } the return 0;94}
View Code
Bzoj 1588 turnover statistics (Hnoi 2002)