恶心的splay、、、Poj3481 double queue
并不知道不加快速输入输出会是什么速度。。。但是加了快速输入输出后47msAC了。。。

1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #define xh(a,b,c)for(int a=(b);a<=(c);a++) 7 #define dxh(a,b,c)for(int a=(b);a>=(c);a--) 8 #define hy(a)memset(a,0,sizeof(a)) 9 using namespace std; 10 struct jd{ 11 int x; 12 int l,r,h; 13 jd(int a=0){ 14 l=r=h=0; 15 x=a; 16 } 17 }; 18 const int N=1010101; 19 jd bst[N]; 20 int root; 21 void lj(int u,int d,int fx){ 22 if(fx==1)bst[u].r=d; 23 else bst[u].l=d; 24 bst[d].h=u; 25 } 26 void qin(int &x){ 27 int base=1,num; 28 char c=getchar(); 29 while(!(c=='-'||c>='0'&&c<='9'||c==EOF))c=getchar(); 30 if(c==EOF)exit(0); 31 if(c=='-')base=-1,c=getchar(); 32 num=c-'0'; 33 c=getchar(); 34 while(c>='0'&&c<='9'){ 35 num*=10; 36 num+=c-'0'; 37 c=getchar(); 38 } 39 x=num*base; 40 } 41 char integ[50]; 42 void qout(int x){ 43 if(x<0)putchar('-'),x=-x; 44 int len=0; 45 do{ 46 integ[len++]=x%10+'0'; 47 x/=10; 48 }while(x); 49 50 while(len--){ 51 putchar(integ[len]); 52 } 53 } 54 void zig(int x){ 55 int b1=bst[x].l,b2=bst[x].r,h=bst[x].h,b3=bst[h].r; 56 root=x; 57 bst[x].h=0; 58 lj(x,h,1); 59 lj(x,b1,-1); 60 lj(h,b2,-1); 61 lj(h,b3,1); 62 } 63 void zag(int x){ 64 int b1=bst[x].l,b2=bst[x].r,h=bst[x].h,b3=bst[h].l; 65 bst[x].h=0; 66 lj(x,h,-1); 67 lj(x,b2,1); 68 lj(h,b3,-1); 69 lj(h,b1,1); 70 } 71 void zigzig(int x){ 72 int b1=bst[x].l, 73 b2=bst[x].r, 74 h=bst[x].h, 75 b3=bst[h].r, 76 hh=bst[h].h, 77 b4=bst[hh].r, 78 hhh=bst[hh].h; 79 if(hhh==0){ 80 bst[x].h=0; 81 }else if(bst[hhh].l==hh)lj(hhh,x,-1); 82 else lj(hhh,x,1); 83 lj(x,h,1); 84 lj(h,hh,1); 85 lj(x,b1,-1); 86 lj(h,b2,-1); 87 lj(hh,b3,-1); 88 lj(hh,b4,1); 89 } 90 void zagzag(int x){ 91 int h=bst[x].h, 92 hh=bst[h].h, 93 hhh=bst[hh].h, 94 b1=bst[hh].l, 95 b2=bst[h].l, 96 b3=bst[x].l, 97 b4=bst[x].r; 98 if(hhh==0){ 99 bst[x].h=0; 100 }else if(bst[hhh].l==hh)lj(hhh,x,-1); 101 else lj(hhh,x,1); 102 lj(x,h,-1); 103 lj(h,hh,-1); 104 lj(x,b4,1); 105 lj(h,b3,1); 106 lj(hh,b1,-1); 107 lj(hh,b2,1); 108 } 109 void zigzag(int x){ 110 int b1=bst[x].l, 111 b2=bst[x].r, 112 h=bst[x].h, 113 b3=bst[h].r, 114 hh=bst[h].h, 115 b4=bst[hh].l, 116 hhh=bst[hh].h; 117 if(hhh==0){ 118 bst[x].h=0; 119 }else if(bst[hhh].l==hh)lj(hhh,x,-1); 120 else lj(hhh,x,1); 121 lj(x,h,1); 122 lj(x,hh,-1); 123 lj(hh,b1,1); 124 lj(h,b2,-1); 125 lj(h,b3,1); 126 lj(hh,b4,-1); 127 } 128 void zagzig(int x){ 129 int b1=bst[x].l, 130 b2=bst[x].r, 131 h=bst[x].h, 132 b3=bst[h].l, 133 hh=bst[h].h, 134 b4=bst[hh].r, 135 hhh=bst[hh].h; 136 if(hhh==0){ 137 bst[x].h=0; 138 }else if(bst[hhh].l==hh)lj(hhh,x,-1); 139 else lj(hhh,x,1); 140 lj(x,h,-1); 141 lj(x,hh,1); 142 lj(h,b1,1); 143 lj(hh,b2,-1); 144 lj(h,b3,-1); 145 lj(hh,b4,1); 146 } 147 void splay(int x){ 148 while(bst[x].h){ 149 if(bst[bst[x].h].h==0){ 150 if(bst[bst[x].h].l==x)zig(x); 151 else zag(x); 152 } 153 else{ 154 int h=bst[x].h,hh=bst[h].h; 155 if(bst[h].l==x){ 156 if(bst[hh].l==h)zigzig(x); 157 else zigzag(x); 158 } 159 else{ 160 if(bst[hh].l==h)zagzig(x); 161 else zagzag(x); 162 } 163 } 164 } 165 } 166 void ins(int r,int i){ 167 if(bst[i].x<bst[r].x){ 168 if(bst[r].l==0){ 169 lj(r,i,-1); 170 splay(i); 171 root=i; 172 } 173 else ins(bst[r].l,i); 174 } 175 else{ 176 if(bst[r].r==0){ 177 lj(r,i,1); 178 splay(i); 179 root=i; 180 } 181 else ins(bst[r].r,i); 182 } 183 } 184 int findmin(int r){ 185 int t=r; 186 while(bst[t].l)t=bst[t].l; 187 return t; 188 } 189 int findmax(int r){ 190 int t=r; 191 while(bst[t].r)t=bst[t].r; 192 return t; 193 } 194 void del(int x){ 195 splay(x); 196 root=x; 197 bst[bst[x].l].h=bst[bst[x].r].h=0; 198 int ma=findmax(bst[x].l); 199 splay(ma); 200 bst[ma].r=bst[x].r; 201 root=ma; 202 bst[x].l=bst[x].r=0; 203 } 204 int nownum; 205 int main(){ 206 //freopen("input.txt","r",stdin); 207 int id,p,cz,t; 208 nownum=0; 209 while(1){ 210 qin(cz); 211 if(cz==0)return 0; 212 else if(cz==1){ 213 qin(id); 214 qin(p); 215 bst[id]=jd(p); 216 if(nownum==0)root=id; 217 else ins(root,id); 218 nownum++; 219 } 220 else if(nownum==0){ 221 qout(0); 222 putchar('\n'); 223 }else{ 224 if(cz==3){ 225 t=findmin(root); 226 splay(t); 227 bst[bst[t].r].h=0; 228 root=bst[t].r; 229 qout(t); 230 putchar('\n'); 231 } 232 else{ 233 t=findmax(root); 234 splay(t); 235 bst[bst[t].l].h=0; 236 root=bst[t].l; 237 qout(t); 238 putchar('\n'); 239 } 240 nownum--; 241 } 242 } 243 return 0; 244 }
查找出最大最小值后删除。。。
按照删除节点的标准方法。。。把一个节点删掉,可以先把他旋到树根,然后去掉,分出左右子树,,,找出左子树的最大节点旋到树根,,,然后这个点的右子树设为原来的右子树。。。结果T了T_T
后来发现。。。把最小点旋到根后,,,它左边就没有节点了(这还用说吗),,,把最大点旋到根后,,它右边也没有节点了、、、于是直接删了它就没问题了T_T,然后47ms过了