"Consolidate" splay templates

Source: Internet
Author: User

Took two days to learn splay+ 's own template.

It should be done in the end.

Although there may still be a lot of less perfect places Otz

Always feel the simple maintenance set or SBT better use Quq (not to do the interval to not learn splay it!) )

Pointers all kinds of difficult to debug AH Otz

A remove one day (╯‵-′) ╯︵┻━┻

After that, unless it's an interval or something you don't want to use (the constant is so much bigger than SBT =0=)

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #define P 1000000#define MAXINT 100000000000using namespace std;struct splay{splay *ch[2],*fa;//ch[0] For left dial hand tree ch[1] for right subtree FA for parent node int data,cnt,size;//data node value cnt repeating element number size}*root,*no;int Ans;int n,key,flag,pet,human;void Calc (splay *x) {x->size=x->cnt;if (x->ch[0]) x->size+=x->ch[0]->size;if (x->ch[1]) x->size+= X->ch[1]->size;}    void rot (splay *x,bool flag)//flag=0 L-flag=1 right-spin saves programming complexity small optimizations {splay *y=x->fa;y->ch[!flag]=x->ch[flag]; if (x->ch[flag]!=null) x->ch[flag]->fa=y;x->fa=y->fa;if (y->fa!=null) if (y->fa->ch[0]==y)    y->fa->ch[0]=x; else Y->fa->ch[1]=x;x->ch[flag]=y;y->fa=x;if (y==root) root=x;} void Splay (splay *x,splay *f)//node x refers to node F (status with SBT maintain () same operation 0-0) {if (x==f| |    X==null) Return;while (x->fa!=f) {if (x->fa->fa==f) {if (x->fa->ch[0]==x) rot (x,1); else rot (x,0);} Else{spLay *y=x->fa,*z=y->fa;if (z->ch[0]==y) if (y->ch[0]==x) Rot (y,1), rot (x,1);    else rot (x,0), rot (x,1), else if (y->ch[0]==x) Rot (x,1), rot (x,0); else rot (y,0), rot (x,0);}} if (f==null) root=x;if (x!=null) calc (x); if (f!=null) Calc (f);} void Insert (int data) {splay *n=root,*x=null;while (n!=null) {x=n;if (data>=n->data) N=n->ch[1];else n=n-> CH[0];} N=new splay;n->ch[0]=n->ch[1]=null;n->size=1;n->data=data;n->cnt=1;n->fa=x;if (X==NULL) root=n; ElseIf (Data>=x->data) x->ch[1]=n;else x->ch[0]=n;if (n!=root) splay (n,null);} splay* find (int data) {if (root==null) return Null;splay *x=root,*y=null;while (x!=null) {if (data>x->data) y=x,x=x    ->ch[1];elseif (Data<x->data) y=x,x=x->ch[0];else {y=x;    return y; }}return NULL;} splay* Getmax () {if (root==null) return Null;splay *x=root;while (x->ch[1]) X=x->ch[1];return x;} splay* Getmin () {if (root==null) return Null;splay *x=root;whiLe (x->ch[0]) X=x->ch[0];return x;} int rank (int data) {splay *x=find (data); Splay (X,root); if (root->ch[0]==x) return x->ch[0]->size+1;else return root->ch[0]->size+1+x->ch[0 ]->size+1;} splay* Select (int k) {splay *x=root;while (x) {if (k>x->ch[0]->size+1) {k-=x->ch[0]->size+1;x=x->ch[ 1];} ElseIf (k<x->ch[0]->size+1) {x=x->ch[0];} else return x;}} splay* pred (splay *x,splay *y,int data) {if (x==null) return y;if (Data==x->data) return y;if (Data>x->data) retur n pred (x->ch[1],x,data); else return pred (x->ch[0],y,data);} splay* succ (splay *x,splay *y,int data) {if (x==0) return y;if (X->data==data) return x;if (X->data>data) return s UCC (X->ch[0],x,data); else return succ (x->ch[1],y,data);} void remove (int data) {if (root==null) Return;splay *x=find (data), *y;if (x==null) return; Splay (X,null); if (!x->ch[0]&&!x->ch[1]) root=null;if (x->ch[0]&&!x->ch[1]) root=x-> Ch[0],x->ch[0]->fa=nuLl;if (!x->ch[0]&&x->ch[1]) root=x->ch[1],x->ch[1]->fa=null;if (x->ch[0]&&x- >ch[1]) {y=x->ch[1];while (y->ch[0]) y=y->ch[0]; Splay (Y,null); Y->fa=null;y->ch[0]=x->ch[0];x->ch[0]->fa=y;calc (y); root=y;}} /*int Main () {}/*


"Consolidate" splay templates

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.