一开始跟着数据结构的书自己写了指针版本的,感觉写得太长太不优雅,后来想学别人用数组模拟指针的那种写法。。不过用不来,所以还是用自己写的吧,至少习惯,也没BUG吧。。LCT那边另外了,写不来,只能抄别人模板。。
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 typedef struct Node *pNode; 5 struct Node{ 6 int val,siz; bool rev; 7 pNode lch,rch,par; 8 Node(int _val,pNode _par):val(_val),siz(1),rev(0),lch(NULL),rch(NULL),par(_par){} 9 }; 10 pNode root; 11 inline void pushdown(pNode x){ 12 if(x->rev){ 13 swap(x->lch,x->rch); 14 if(x->lch) x->lch->rev^=1; 15 if(x->rch) x->rch->rev^=1; 16 x->rev=0; 17 } 18 } 19 inline void pushup(pNode x){ 20 x->siz=1; 21 if(x->lch) x->siz+=x->lch->siz; 22 if(x->rch) x->siz+=x->rch->siz; 23 } 24 inline void zig(pNode x){ 25 pNode y=x->par,z=y->par; 26 pushdown(y); pushdown(x); 27 28 x->par=z;// 29 if(z) z->lch==y?z->lch=x:z->rch=x; 30 z=x->rch;// 31 if(z) z->par=y; 32 y->lch=z; 33 y->par=x; x->rch=y;// 34 35 pushup(y); 36 } 37 inline void zag(pNode x){ 38 pNode y=x->par,z=y->par; 39 pushdown(y); pushdown(x); 40 41 x->par=z;// 42 if(z) z->lch==y?z->lch=x:z->rch=x; 43 z=x->lch;// 44 if(z) z->par=y; 45 y->rch=z; 46 y->par=x; x->lch=y;// 47 48 pushup(y); 49 } 50 inline void splay(pNode x,pNode par){ 51 pushdown(x); 52 while(x->par!=par){ 53 pNode y=x->par,z=y->par; 54 if(z==par){ 55 y->lch==x?zig(x):zag(x); 56 }else{ 57 if(z->lch==y){ 58 if(y->lch==x){ 59 zig(y); zig(x); 60 }else{ 61 zag(x); zig(x); 62 } 63 }else{ 64 if(y->lch==x){ 65 zig(x); zag(x); 66 }else{ 67 zag(y); zag(x); 68 } 69 } 70 } 71 } 72 pushup(x); 73 if(par==NULL) root=x; 74 } 75 inline pNode findkth(int k){ 76 ++k; 77 pNode x=root; 78 int lsiz; 79 for(;x;){ 80 pushdown(x); 81 lsiz=x->lch?x->lch->siz:0; 82 if(lsiz+1==k){ 83 return x; 84 }else if(k<=lsiz){ 85 x=x->lch; 86 }else{ 87 k-=lsiz+1; 88 x=x->rch; 89 } 90 } 91 return NULL; 92 } 93 int main(){ 94 root=NULL; 95 return 0; 96 }