码迷,mamicode.com
首页 > 其他好文 > 详细

NOI’2003 - Day1 - Problem2 文本编辑器(Editor) 空间

时间:2016-03-02 22:03:00      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

题目百度把,太长了。

程序有点长,写的很渣。就不注释了。



 



1
Program Stone; 2 3 var n,qu,ji,tot,root,move:longint; 4 5 s:array[0..5000000]of char; 6 7 lc,rc,f,o:array[0..5000000]of longint; 8 9 10 11 Procedure rightturn(x:longint); 12 13 var i,j,k:longint; 14 15 Begin 16 17 i:=f[x]; 18 19 f[x]:=f[i]; 20 21 if i=lc[f[i]] then lc[f[i]]:=x else rc[f[i]]:=x; 22 23 lc[i]:=rc[x];f[lc[i]]:=i; 24 25 rc[x]:=i;f[i]:=x; 26 27 28 29 o[i]:=o[lc[i]]+o[rc[i]]+1; 30 31 o[x]:=o[lc[x]]+o[rc[x]]+1; 32 33 end; 34 35 Procedure leftturn(x:longint); 36 37 var i,j,k:longint; 38 39 Begin 40 41 i:=f[x]; 42 43 f[x]:=f[i]; 44 45 if i=lc[f[i]] then lc[f[i]]:=x else rc[f[i]]:=x; 46 47 rc[i]:=lc[x];f[rc[i]]:=i; 48 49 lc[x]:=i;f[i]:=x; 50 51 52 53 o[i]:=o[lc[i]]+o[rc[i]]+1; 54 55 o[x]:=o[lc[x]]+o[rc[x]]+1; 56 57 end; 58 59 60 61 Procedure promote(x,z:longint); 62 63 var i,j,k:longint; 64 65 begin 66 67 k:=f[x]; 68 69 if f[k]=z then begin 70 71 if x=lc[k] then rightturn(x) else leftturn(x); 72 73 exit; 74 75 end; 76 77 if k=lc[f[k]] then begin 78 79 if x=lc[k] then begin 80 81 rightturn(k); 82 83 rightturn(x); 84 85 end 86 87 else begin 88 89 leftturn(x); 90 91 rightturn(x); 92 93 end; 94 95 end 96 97 else begin 98 99 if x=lc[k] then begin 100 101 rightturn(x); 102 103 leftturn(x); 104 105 end 106 107 else begin 108 109 leftturn(k); 110 111 leftturn(x); 112 113 end; 114 115 end; 116 117 end; 118 119 Procedure splay(x,y:longint); 120 121 var i:longint; 122 123 begin 124 125 if x=y then exit; 126 127 while f[x]<>y do 128 129 begin 130 131 promote(x,y); 132 133 end; 134 135 end; 136 137 138 139 Procedure findk(m,x:longint); 140 141 var i,j,k:longint; 142 143 begin 144 145 if o[lc[x]]+1=m then begin qu:=x;exit;end; 146 147 if o[lc[x]]+1<m then begin findk(m-o[lc[x]]-1,rc[x]);exit;end; 148 149 if o[lc[x]]+1>m then begin findk(m,lc[x]);exit;end; 150 151 end; 152 153 154 155 Procedure built(x,y:longint); 156 157 var k:longint; 158 159 begin 160 161 if x=y then begin o[x]:=1;exit;end; 162 163 k:=(x+y)div 2; 164 165 o[k]:=y-x+1; 166 167 if x<=k-1 then begin 168 169 lc[k]:=(x+k-1)div 2; 170 171 f[(x+k-1)div 2]:=k; 172 173 built(x,k-1); 174 175 end; 176 177 if y>=k+1 then begin 178 179 rc[k]:=(y+k+1)div 2; 180 181 f[(y+k+1)div 2]:=k; 182 183 built(k+1,y); 184 185 end; 186 187 end; 188 189 190 191 Procedure insert(m:longint); 192 193 var i,j,k:longint; 194 195 c:char; 196 197 begin 198 199 k:=0; 200 201 while k<m do 202 203 begin 204 205 read(c); 206 207 if (ord(c)>=32)and(ord(c)<=126) then 208 209 begin 210 211 inc(tot); 212 213 s[tot]:=c; 214 215 inc(k); 216 217 end; 218 219 end; 220 221 readln; 222 223 if (tot-m+1<=tot-1) then begin 224 225 built(tot-m+1,tot-1); 226 227 lc[tot]:=(tot-m+tot)div 2; 228 229 f[(tot-m+tot)div 2]:=tot; 230 231 end; 232 233 o[tot]:=m+o[rc[root]]; 234 235 rc[tot]:=rc[root]; 236 237 f[rc[root]]:=tot; 238 239 rc[root]:=tot; 240 241 f[tot]:=root; 242 243 o[root]:=o[lc[root]]+o[rc[root]]+1; 244 245 246 247 end; 248 249 250 251 Procedure print(x:longint); 252 253 begin 254 255 if x<1 then exit; 256 257 if lc[x]<>0 then print(lc[x]); 258 259 if x<>1 then write(s[x]); 260 261 if rc[x]<>0 then print(rc[x]); 262 263 end; 264 265 procedure init; 266 267 var i,j,k,l,m:longint; 268 269 c:char; 270 271 begin 272 273 tot:=1; 274 275 root:=1;o[1]:=1; 276 277 move:=1; 278 279 readln(n); 280 281 for j:=1 to n do 282 283 begin 284 285 read(c); 286 287 case c of 288 289 I:Begin 290 291 for i:=1 to 6 do read(c); 292 293 readln(m); 294 295 findk(move,root); 296 297 splay(qu,0);root:=qu; 298 299 insert(m); 300 301 end; 302 303 M:Begin 304 305 for i:=1 to 4 do read(c); 306 307 readln(m); 308 309 move:=m+1; 310 311 end; 312 313 D:Begin 314 315 for i:=1 to 6 do read(c); 316 317 readln(m); 318 319 findk(move,root); 320 321 splay(qu,0);root:=qu; 322 323 if m>o[rc[root]] then m:=o[rc[root]]; 324 325 findk(m,rc[root]); 326 327 splay(qu,root); 328 329 rc[root]:=rc[qu]; 330 331 f[rc[qu]]:=root; 332 333 o[root]:=o[root]-o[qu]; 334 335 end; 336 337 G:Begin 338 339 for i:=1 to 3 do read(c); 340 341 readln(m); 342 343 findk(move,root); 344 345 splay(qu,0);root:=qu; 346 347 findk(m,rc[root]); 348 349 splay(qu,root); 350 351 print(lc[qu]); 352 353 writeln(s[qu]); 354 355 end; 356 357 P:Begin 358 359 for i:=1 to 4 do read(c); 360 361 readln; 362 363 move:=move-1; 364 365 end; 366 367 N:Begin 368 369 for i:=1 to 4 do read(c); 370 371 readln; 372 373 move:=move+1; 374 375 end; 376 377 end; 378 379 end; 380 381 end; 382 383 Begin 384 385 assign(input,editor.in);assign(output,editor.out); 386 387 reset(input);rewrite(output); 388 389 init; 390 391 close(input);close(output); 392 393 end.

 

NOI’2003 - Day1 - Problem2 文本编辑器(Editor) 空间

标签:

原文地址:http://www.cnblogs.com/yesphet/p/5236301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!