Advertisement
agul

Treap - WA9 #2

Aug 14th, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.57 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.     PE = ^TE;
  10.     TE = record
  11.         x,y,n:longint;
  12.         l,r,p:PE;
  13.     end;
  14.  
  15. var
  16.     i,n,x,y:longint;
  17.     a:array[0..50010] of pe;
  18.     p,l,r,root:pe;
  19.  
  20.  
  21. procedure Init(var root:PE);
  22. begin
  23. root := nil;
  24. end;
  25.  
  26. procedure Split(pt : PE; key : longint; var pL, pR : PE);
  27. begin
  28. if pt = nil then begin
  29.     pL := nil;
  30.     pR := nil;
  31. end else
  32. if pt^.x < x then begin
  33.     pL := pt;
  34.     Split(pL^.R, key, pL^.R, pR);
  35. end else begin
  36.     pR := pt;
  37.     Split(pR^.L, key, pL, pR^.L);
  38. end;
  39. end;
  40.  
  41. procedure Add1(var p : PE; var np : PE; prev:PE);
  42. begin
  43. if p = nil then begin
  44.     p := np;
  45.     p^.p := prev;
  46. end else
  47.     if np^.y > p^.y then
  48.         if np^.x = p^.x then exit else
  49.         if np^.x < p^.x then Add1(p^.L, np, p) else Add1(p^.R, np, p)
  50.     else begin
  51.         Split(p, np^.x, np^.L, np^.R);
  52.         p := np;
  53.         p^.p := prev;
  54.         if p^.l <> nil then p^.l^.p := p;
  55.         if p^.r <> nil then p^.r^.p := p;
  56.     end;
  57. end;
  58.  
  59.  
  60. procedure Add(var root : PE; x,y:longint);
  61.  
  62. var
  63.     np : PE;
  64.  
  65. begin
  66. new(np);
  67. np^.x := x;
  68. np^.y := y;
  69. np^.n := i;
  70. np^.l := nil;
  71. np^.r := nil;
  72. np^.p := nil;
  73. Add1(root, np, nil);
  74. a[i] := np;
  75. end;
  76.  
  77. begin
  78. reset(input, 'input.txt');
  79. rewrite(output, 'output.txt');
  80. read(n);
  81. init(root);
  82. for i := 1 to n do begin
  83.     read(x, y);
  84.     add(root, x, y);
  85. end;
  86.  
  87. writeln('YES');
  88. for i := 1 to n do begin
  89.     p := a[i]^.p;
  90.     if p = nil then write(0) else write(p^.n);
  91.     write(' ');
  92.     l := a[i]^.l;
  93.     if l = nil then write(0) else write(l^.n);
  94.     write(' ');
  95.     r := a[i]^.r;
  96.     if r = nil then write(0) else write(r^.n);
  97.     writeln;
  98. end;
  99. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement