POJ 2828(卡时线段树)

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

故用线段树 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.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值