The paper looked for two days, which is the result:
1#include <cstdio>2 3 Const intMAXN =1000000;4 5 intN, a, B, C, root;6 intchild[maxn][2];7 intKEY[MAXN], SIZE[MAXN], Count;8 9 intNew_node (intv)Ten { Onecount++; AKey[count] =v; -Size[count] =1; - returnCount; the } - - voidRotate (int& X,intd) - { + intK = child[x][!d]; -CHILD[X][!D] =Child[k][d]; +CHILD[K][D] =x; ASIZE[K] =Size[x]; atSIZE[X] = size[child[x][0]] + size[child[x][1]] +1; -x =K; - } - - voidMaintain (int& T,intd) - { in if(!t)return; - if(Size[child[child[t][d]][d]] > size[child[t][!d]]) toRotate (t,!d); + Else if(Size[child[child[t][d]][!d]] > size[child[t][!d]]) -Rotate (Child[t][d], D), Rotate (t,!d); the Else return; *Maintain (child[t][0],0); $Maintain (child[t][1],1);Panax NotoginsengMaintain (T,0); -Maintain (T,1); the } + A voidInsert (int& T,intv) the { + if(T = =0) T =New_node (v); - Else $ { $size[t]++; -Insert (Child[t][v >=Key[t]], v); -Maintain (T, v >=key[t]); the } - }Wuyi the intDelete (int& T,intv) - { Wu intRet; -size[t]--; About if(v = = Key[t] | | Child[t][v > key[t]] = =0) $ { -Ret =Key[t]; - if(child[t][0] && child[t][1]) -Key[t] = Delete (child[t][0], V +1); A Elset = child[t][0] + child[t][1]; +}Else theRet = Delete (Child[t][v >Key[t]], v); - returnRet; $ } the the intRank (intTintv) the { the if(T = =0)return 1; - if(v <= key[t])returnRank (child[t][0], v); in returnRank (child[t][1], V) + size[child[t][0]] +1; the } the About intSelect (intTintk) the { the if(child[t][0] && size[child[t][0]] >=k) the returnSelect (child[t][0], k); + if(size[child[t][0]] +1= = k)returnKey[t]; - returnSelect (child[t][1], k-size[child[t][0]] -1); the }Bayi the voidPrint (intt) the { - if(T = =0)return; -Print (child[t][0]); theprintf ("%d", Key[t]); thePrint (child[t][1]); the } the - intMain () the { thescanf ("%d", &n); the for(inti =0; I < n; i++)94 { thescanf ("%d%d", &a, &b); the if(A = =1) Insert (root, b); the if(A = =2)98 { Aboutc =Delete (root, b); - if(c! =b) Insert (root, c);101 }102 if(A = =3) printf ("%d\n", Select (root, b));103 if(A = =4) printf ("%d\n", Rank (root, b));104 if(A = =5) the {106 if(b = =1)107 {108 for(intj =1; J <= Count; J + +)109printf ("Child :%d%d, Key:%d\n", child[j][0], child[j][1], key[j]); the}ElsePrint (Root), printf ("\ n");111 } the }113}
Size Balanced Tree