POJ 2828(卡时线段树)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nike0good/article/details/7897259

本题乍一看链表,实际上肯定超时%……

故用线段树 LogN查找……

Program P2828;
const
   maxn=200000;
var
   n,i,j:longint;
   pos,val:array[1..maxn] of longint;
   sum:array[1..maxn*10] of longint;
   ans:array[1..maxn] of longint;
procedure pushup(root:longint);
begin
   sum[root]:=sum[root*2]+sum[root*2+1];
end;
procedure build(l,r,root:longint);
var
   mid:longint;
begin
   if l=r then
   begin
      sum[root]:=1;
      exit;
   end;
   mid:=(l+r) shr 1;
   build(l,mid,root*2);
   build(mid+1,r,root*2+1);
   pushup(root);
end;
procedure decr(l,r,root,node:longint);
var
   mid:longint;
begin
   if l=r then
   begin
      dec(sum[root]);
      exit;
   end;
   mid:=(l+r) shr 1;
   if (node<=mid) then decr(l,mid,root shl 1,node)
   else decr(mid+1,r,root shl 1+1,node);
   pushup(root);
end;
function find(l,r,root,value:longint):longint;
var
   mid:longint;
begin
   mid:=(l+r) shr 1;
   if l=r then
   begin
      decr(1,n,1,l);
      exit(l);
   end;
   if (sum[root shl 1]>=value) then exit(find(l,mid,root shl 1,value))
   else exit(find(mid+1,r,root shl 1+1,value-sum[root shl 1]));
end;
begin
   while not seekeof do
   begin
      read(n);
      for i:=1 to n do read(pos[i],val[i]);
      build(1,n,1);
      for i:=n downto 1 do
      begin
         ans[find(1,n,1,pos[i]+1)]:=val[i];

      end;
      for i:=1 to n-1 do write(ans[i],' ');
      writeln(ans[n]);
   end;
end.


展开阅读全文

没有更多推荐了,返回首页