Bzoj 1588:splay Tree

Source: Internet
Author: User

Just learned the splay tree. Knocked on the code of the Great God, and understood a probable

Very useful data structure, can be used to maintain the sequence

It is recommended to look at SBT first so that you can better understand the "rotation"

#include"Cstdio"#include"Queue"#include"Cmath"#include"Stack"#include"iostream"#include"algorithm"#include"CString"#include"Queue"#include"Map"#include"Vector"#definell Long Long#defineMEMS (A, B) memset (A,b,sizeof (a))using namespacestd;Const intMAXN = 1e5+ -;Const intMaxe =200500;Const intINF =0x3f3f3f;intpre[maxn],val[maxn],ch[maxn][2];///father Knot, value, child node (0 left 1 right)intTot,root;///sum of points, root node///The sequence traversal is the maintained sequencevoidNewNodeint&pos,intFaintW) {POS=++tot; Pre[pos]=FA; Val[pos]=W; ch[pos][0]=ch[pos][1]=0;}voidRotate (intXintKind) {///0 left 1 right    intHasPre[x]; ch[fa][!kind]=Ch[x][kind]; Pre[ch[x][kind]]=FA; if(Pre[fa]) ch[pre[fa]][ch[pre[fa]][1]==fa]=x; PRE[X]=PRE[FA]; Ch[x][kind]=A; PRE[FA]=x;}voidSplay (intRintGoal) {///Rotate the R node below goal.     while(pre[r]!=goal) {        if(Pre[pre[r]]==goal) Rotate (r,ch[pre[r]][0]==R); Else{            intHasPre[r]; intkind=ch[pre[fa]][0]==FA; if(CH[FA][KIND]==R) {///alternating left and rightRotate (r,!kind);            Rotate (R,kind); }            Else{///Consistent DirectionRotate (Fa,kind);            Rotate (R,kind); }        }    }    if(!goal) Root=r;///when goal is not present, R is changed to the root node .}BOOLInsert (intkey) {    intR=Root;  while(ch[r][val[r]<Key]) {        if(Val[r]==key) {///The same value is inserted only onceSplay (R,0); return false; } R=ch[r][val[r]<Key]; } newnode (Ch[r][val[r]<Key],r,key); Splay (Ch[r][val[r]<key],0); return true;}///BST Query complexity log (n)intGet_pre (intx) {///looking for numbers larger than X but closest to X    intt=ch[x][0]; if(!t)returnINF;  while(ch[t][1]) t=ch[t][1]; returnval[x]-val[t];}intGet_next (intx) {///looking for a number smaller than x but closest to X    intt=ch[x][1]; if(!t)returnINF;  while(ch[t][0]) t=ch[t][0]; returnval[t]-val[x];}intMain () {intN; //freopen ("In.txt", "R", stdin);     while(~SCANF ("%d",&N)) {Root=tot=0; intans=0;  for(intI=1; i<=n;i++){            intx; if(SCANF ("%d", &x) ==eof) x=0; if(i==1) {ans+=x; NewNode (Root,0, x); Continue; }            if(! Insert (x))Continue; Ans+=min (Get_pre (root), get_next (root)); } cout<<ans<<Endl; }    return 0;}

Bzoj 1588:splay Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.