http://www.lydsy.com/JudgeOnline/problem.php?id=1588
Test instructions: Each read into a number, in the number of previous input to find a difference with the number of the least, the absolute value of all the difference is added up and output
1#include"bits/stdc++.h"2 using namespacestd;3 Const intMAXN =100010;4 Const intINF =0x3f3f3f3f;5 structSplaytreenode6 {7 intkey;8 intFather, child[2];9 }SPLAYTREENODE[MAXN];Ten intSplaytreeroot, Cntsplaytreenode; One A intn, num; - - voidAddsonnode (int&son,intFatherintkey) the { -++Cntsplaytreenode; -son =Cntsplaytreenode; -Splaytreenode[son].key =key; +Splaytreenode[son].father =father; -splaytreenode[son].child[0] = splaytreenode[son].child[1] =0; + } A at voidRotate (intIndexBOOLPOS) - { - intIndexfather =Splaytreenode[index].father; - intIndexfatherfather =Splaytreenode[indexfather].father; - intIndexson = splaytreenode[index].child[!POS]; - //parent Node Pips insplaytreenode[indexfather].child[pos] =Indexson; -splaytreenode[Indexson].father =Indexfather; to //Parent node of parent node Origin node + if(indexfatherfather) { - BOOLPosfather = splaytreenode[Indexfatherfather].child[1] ==Indexfather; thesplaytreenode[indexfatherfather].child[posfather] =index; * } $Splaytreenode[index].father =Splaytreenode[indexfather].father;Panax Notoginseng //Original node parent node -Splaytreenode[index].child[!pos] =Indexfather; theSplaytreenode[indexfather].father =index; + } A the voidSplay (intIndexintgoalfather) + { - intIndexfather; $ while((Indexfather = splaytreenode[index].father)! =goalfather) { $ BOOLpos = splaytreenode[indexfather].child[1] ==index; - //rotate once to achieve the goal - if(Splaytreenode[indexfather].father = =goalfather) { the Rotate (index, POS); - }Wuyi Else { the BOOLPosfather = splaytreenode[Splaytreenode[indexfather].father].child[1] ==Indexfather; - if(POS! =posfather) { Wu //the sub-node rotates two times in different directions - Rotate (index, POS); About Rotate (index, posfather); $ } - Else { - //same orientation, rotate parent node first, then rotate child node - Rotate (Indexfather, POS); A Rotate (index, POS); + } the } - } $ //The root node is rotated below the alternate node 0 the if(Goalfather = =0) theSplaytreeroot =index; the } the - //failed to return 0, successfully returned 1 in BOOLInsertnode (intkey) the { the intindex =Splaytreeroot; About while(Splaytreenode[index].child[Splaytreenode[index].key <Key]) { the //do not insert repeatedly the if(Splaytreenode[index].key = =key) { the //rotate to the root node +Splay (Index,0); - return 0; the }Bayiindex = splaytreenode[index].child[Splaytreenode[index].key <key]; the } theAddsonnode (splaytreenode[index].child[Splaytreenode[index].key <key], index, key); -Splay (splaytreenode[index].child[Splaytreenode[index].key < key],0); - return 1; the } the the intGetcolost_1 (intindex) the { - intIndexson = splaytreenode[index].child[0]; the if(Indexson = =0) the returnINF; the while(splaytreenode[indexson].child[1])94Indexson = splaytreenode[indexson].child[1]; the returnSplaytreenode[index].key-Splaytreenode[indexson].key; the } the 98 intGetcolost_2 (intindex) About { - intIndexson = splaytreenode[index].child[1];101 if(Indexson = =0)102 returnINF;103 while(splaytreenode[indexson].child[0])104Indexson = splaytreenode[indexson].child[0]; the return-splaytreenode[index].key +Splaytreenode[indexson].key;106 }107 108 intMain ()109 { the inti;111 while(SCANF ("%d", &n)! =EOF) { theSplaytreeroot = Cntsplaytreenode =0;113 intres =0; the for(i =1; I <= N; ++i) { the if(SCANF ("%d", &num) = =EOF)//data some problems, to add this thenum =0;117 if(i = =1) {118Res + =num;119Addsonnode (Splaytreeroot,0, num); - Continue;121 }122 if(Insertnode (num) = =0)123 Continue;124 intTmp_1 =getcolost_1 (splaytreeroot); the intTmp_2 =getcolost_2 (splaytreeroot);126Res + =min (tmp_1, tmp_2);127 } -printf"%d\n", res);129 } the}
Wo just want to add a little readability to the program ... I never thought I'd write this qaq.
1588: [HNOI2002] turnover statistics splay tree