Advertisement
agul

Treap - WA9 #1

Aug 14th, 2011
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.32 KB | None | 0 0
  1. {$APPTYPE CONSOLE}
  2. {$R+,S+,Q+,I+,O-}
  3. {R-,S-,Q-,I-,O+}
  4.  
  5. uses
  6.     SysUtils, Math;
  7.  
  8. type
  9.     pnode=^node;
  10.     node=record
  11.         l,r,p:pnode;
  12.         x,y,n:longint;
  13.     end;
  14.  
  15. var
  16.     i,n,x,y:longint;
  17.     t,p,l,r:pnode;
  18.     a:array[0..50010] of pnode;
  19.  
  20. procedure split(t:pnode; x:longint; var l,r:pnode);
  21. begin
  22. if t = nil then begin
  23.     l := nil;
  24.     r := nil;
  25. end else
  26. if x < t^.x then begin
  27.     split(t^.l, x, l, r);
  28.     t^.l := r;
  29.     r := t;
  30. end else begin
  31.     split(t^.r, x, l, r);
  32.     t^.r := l;
  33.     l := t;
  34. end;
  35. end;
  36.  
  37. procedure insert(var t:pnode; x,y:longint; p:pnode);
  38.  
  39. var
  40.     tmp:pnode;
  41.    
  42. begin
  43. if (t = nil) or (y < t^.y) then begin
  44.     new(tmp);
  45.     split(t, x, tmp^.l, tmp^.r);
  46.     t := tmp;
  47.     t^.x := x;
  48.     t^.y := y;
  49.     t^.p := p;
  50.     t^.n := i;
  51.     if t^.l <> nil then t^.l^.p := t;
  52.     if t^.r <> nil then t^.r^.p := t;
  53.     a[i] := t;
  54. end else
  55. if x < t^.x then insert(t^.l, x, y, t) else insert(t^.r, x, y, t);
  56. end;
  57.  
  58. begin
  59. reset(input, 'input.txt');
  60. rewrite(output, 'output.txt');
  61. read(n);
  62. t := nil;
  63. for i := 1 to n do begin
  64.     read(x, y);
  65.     insert(t, x, y, nil);
  66. end;
  67. writeln('YES');
  68. for i := 1 to n do begin
  69.     p := a[i]^.p;
  70.     if p = nil then write(0) else write(p^.n);
  71.     write(' ');
  72.     l := a[i]^.l;
  73.     if l = nil then write(0) else write(l^.n);
  74.     write(' ');
  75.     r := a[i]^.r;
  76.     if r = nil then write(0) else write(r^.n);
  77.     writeln;
  78. end;
  79. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement