poj 2828

分析

  见poj 2182:http://blog.csdn.net/a_loud_name/article/details/51530711


代码

type
  pnode=^tnode;
  tnode=record
    lc,rc:pnode;
    c:longint;
end;

var
  t:pnode;
  i,j,k:longint;
  x,y:longint;
  n,m:longint;
  a,b,ans:array[1..800000] of longint;

procedure qsort(l,r:longint);
  var
    i,j,key,temp:longint;
  begin
    if l>=r then exit;
    i:=l;j:=r;
    key:=ans[l+random(r-l+1)];
    repeat
      while (ans[i]<key) do inc(i);
      while (ans[j]>key) do dec(j);
      if i<=j then
      begin
        temp:=ans[i]; ans[i]:=ans[j]; ans[j]:=temp;
        temp:=b[i]; b[i]:=b[j]; b[j]:=temp;
        inc(i);dec(j);
      end;
    until i>j;
    qsort(l,j);
    qsort(i,r);
  end;

procedure neww(var t:pnode);
begin
  if t=nil then
    begin
      new(t);
      t^.c:=0;
      t^.lc:=nil;
      t^.rc:=nil;
    end;
end;

procedure insert(var t:pnode; l,r,cr:longint);
var
  i,j,k:longint;
  mid:longint;

begin
  with t^ do
        begin
          c:=cr;
          mid:=(l+r) div 2;
          if l=r
            then
              exit;
          neww(lc);
          neww(rc);
          insert(lc,l,mid,mid);
          insert(rc,mid+1,r,cr);
        end;
end;

function find(var t:pnode;l,r,cr:longint):longint;
var
  mid:longint;
begin
  t^.c:=t^.c-1;
  with t^ do
    begin
      if (lc=nil) and (rc=nil) then exit(l);
      mid:=(l+r) div 2;
      if lc^.c>=cr then find:=find(lc,l,mid,cr)
                   else find:=find(rc,mid+1,r,cr+mid-Lc^.c);
    end;
end;

begin
  while not eof do begin
  readln(n);
  fillchar(t,sizeof(t),0);
  fillchar(a,sizeof(a),0);
  fillchar(ans,sizeof(ans),0);
  neww(t);
  insert(t,1,n,n);
  for i:=1 to n do
    readln(a[i],b[i]);
  for i:=n downto 1 do
    ans[i]:=find(t,1,n,a[i]+1);
  qsort(1,n);
  for i:=1 to n do
    write(b[i],' ');
  writeln;
  end;
end.



posted @ 2016-05-29 10:23  一个响亮的蒟蒻  阅读(174)  评论(0编辑  收藏  举报