LLRBTree
// RBTree #define BLACK 0 #define RED 1 typedef int Int Struct RBNode{ RBNode *chi[2],*pa; int color,cnt; }*nil=new RBNode(),*root=nil; void Nil(){ CreatNode(nil,0,BLACK,0); } RBNode * CreatNode(RBNode * &node,Int val,int color,int cnt=1){ if(node!=NULL && node!=nil){ RBNode *p=node; delete p; } node=new RBNode(); node->val=val; node->cnt=cnt; node->pa=node->chi[0]=node->chi[1]=nil; return node; } void Connect(RBNode *pa,RBNode *node,int dir) { Nil(); pa->chi[dir]=node; node->pa=pa; Nil(); } RBNode * Extrema(RBNode * node,int dir){ while(node->chi[dir]!=nil) node=node->chi[dir]; return node; } RBNode * Cessor(RBNode * node,int dir){ if(node->chi[dir]!=nil) return Extrema(node->chi[dir],1-dir); for(RBNode * p=node->pa;p!=nil && p->chi[dir]==node;node=p,p=p->pa); return node->pa; } RBNode * Bound(RBNode * node,Int val,int dif=1){ if(node==nil) return nil; for(int dir;val!=node->val;node=node->chi[dir]){ dir=(val<node->val)?0:1; if(node->chi[dir]==nil) return node; } if(!dif && node->val!=val) return nil; return node; } void Rotate(RBNode *node,int rodir){ Nil(); RBNode *pivot=node->chi[1-rodir]; Connect(node,node->chi[1-rodir]->chi[rodir],1-rodir); Connect(node->pa,pivot,(node->pa->chi[0]==node)?0:1); Connect(pivot,node,rodir); Nil(); if(root==node) root=pivot; } void Fixup(RBNode *root,RBNode *node){ } void Insert(RBNode * &root,Int val){ if(root==nil){ root=CreatNode(root,val,RED); return; } Nil(); RBNode *p=Bound(root,val,1); if(p->val==val){ p->cnt++; return; } int dir=(val<p->val)?0:1; RBNode *node=nil; CreatNode(node,val); Connect(p,node,dir); Fixup(root,node); Nil(); } bool Delete(RBNode *&root,Int val){ if(root==nil) return false; Nil(); if((RBNode *p=Bound(root,val,0))==nil) return false; if(p->cnt>1){ (p->cnt)--; return true; } if(p->chi[0]!=nil || p->chi[1]!=nil) if((RBNode *suc=Cessor(p,1))!=nil || (suc=Cessor(p,0))!=nil) {p->val=suc->val; p->cnt=suc->cnt; p=suc; } int dir=p->pa->chi[0]==p?0:1; p->pa->chi[dir]=nil; p=p->pa; Balance(p,(!dir)?1:-1,1); if(p!=root) delete p; else {RBNode * t=p; root=nil; delete t;} Nil(); return true; } //------------- void Insert(BFNode * node,Int val){ BFNode *x=root; while(node->chi!=nil){ x=x->chi; if(x->size=4) Split(x); } if(x->size==2) x.Plus(val); else if(x->size==3) x.Plus4(val); return x; } //---------------- RBNode * Insert(RBNode node,Int key,Int val){ if(node==nil) return node=CreatNode(node,key,val); int cmp=Cmp(key,node->key); if(cmp==0) node->val=val; else if(cmp<0) Insert(node->chi[0],key,val); else Insert(node->chi[1],key,val); return Node; } void ColorFlip(RBNode * node) { node->color^=1; node->chi[0].color^=1; node->chi[1].color^=1; } //--------- RBNode * Insert(RBNode * node,Int key,Int val){ if(node==nil) return node=CreatNode(node,key,val,RED); int cmp=Cmp(key,node->key); if(cmp==0) node->val=val; else if(cmp<0) Insert(node->chi[0],key,val); else Insert(node->chi[1],key,val); if(node->chi[1].color==RED) Rotate(node,0); if(node->chi[0].color==RED && node->chi[1].color==RED) Rotate(node,1); if(node->chi[0]->color==RED && node->chi[1].color==RED) ColorFlip(node); return Node; } RBNode * Delete(RBNode * node,Int key){ if(key<node->key){ if(node->chi[0]->color==RED && node->chi[0]->chi[0]->color==RED){ MoveRedLeft(node); node->chi[0]=Delete(node->chi[0],key); }else{ if(node->chi[0]->color==RED) node=LeanRight(node); if(key==node->key && node->chi[1]==nil) return nil; if(node->chi[1]->color!=RED && node->chi[1]->chi[0]->color!=RED) node=MoveREDRight(node); if(node->key==key) { node->key=min(node->chi[1]); node->val=Get(node->chi[1],node->key); node->chi[1]=DeleteMin(node->chi[1]); } else node->chi[1]=Delete(node->chi[1],key); } } return Fixup(node); ]