The idea of solving problems: Finding the nearest value on the edge.
Problem-Solving ideas: splay
Problem Solving Code:
1 //File Name:poj3468.sz.cpp2 //Author:darkdream3 //Created time:2014 November 07 Friday 08:45 48 seconds4#include <limits.h>5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - #defineMAXN 1000010 - using namespacestd; - #defineValtree Ch[ch[root][1]][0] - structsplaytree{ in intch[maxn][2]; - intPRE[MAXN]; to intRoot; + intTop1; -InlinevoidRotate (intXintf) { the inty =Pre[x]; *CH[Y][!F] =Ch[x][f]; $PRE[CH[X][F]] =y;Panax NotoginsengPRE[X] =Pre[y]; - if(Pre[x]) thech[pre[y]][ch[pre[y]][1] = = Y] =x; +CH[X][F] =y; APre[y] =x; the } + voidSplay (intXintgoal) { - while(Pre[x]! =goal) { $ if(Pre[pre[x]] = =goal) { $Rotate (x,ch[pre[x]][0] ==x); -}Else{ - inty = Pre[x],z =Pre[y]; the intF = (ch[z][0] ==y); - if(Ch[y][f] = =x)Wuyi { theRotate (x,!f), Rotate (x,f); -}Else{ Wu Rotate (y,f), Rotate (x,f); - } About } $ } - if(Goal = =0) root =x; - } - intABS (intx) A { + if(X <0) the return-x; - returnx; $ } the intRotateto (intKintgoal) the { the intR =Root; the intMi =Int_max; - while(1) in { themi = min (mi,abs (val[r]-k)); the if(!ch[r][val[r] <K]) About Break; ther = Ch[r][val[r] <K]; the } the int&tt = Ch[r][val[r] <K]; + NewNode (tt,k); -PRE[TT] =R; theSplay (TT,0);Bayi returnmi; the } the /*void Erase (int x) { - int father = pre[x]; - int head = 0, tail = 0; the For () the }*/ the /*void Debug () {printf ("%d\n", root); Treaval (root);} the void Treaval (int x) { - if (x) { the Treaval (ch[x][0]); the printf ("Knot%2d: Left son%2d right son%2d parent node%2d size =%2d, val =%2d sum =%3lld\n", X,ch[x][0],ch[x][1],pre[x],sz[x] , Val[x],sum[x]); the Treaval (ch[x][1]);94 } the }*/ the voidNewNodeint&x,intc) the {98x = + +Top1; Aboutch[x][0] = ch[x][1] = pre[x] =0 ; -VAL[X] =C; 101 }102 voidInitintv)103 {104ch[0][0] = ch[0][1] = pre[0] =0 ; theroot = Top1 =0 ; 106NewNode (ROOT,V);//here, note that root starts from 1.107 }108 intVAL[MAXN];109 }sp; the intMain () {111 intN; thescanf"%d",&n);113 if(n = =0 ) the { theprintf"%d\n",0); the return 0;117 }118 intv;119scanf"%d",&v); - Sp.init (v);121 intAns =v;122 for(inti =1; i < N;i + +)123 {124 if(SCANF ("%d", &v) = =EOF) thev =0 ; 126Ans + = sp. Rotateto (V,0);127 } -printf"%d\n", ans);129 return 0; the}View Code
HNOI2002 turnover statistics splay