poj 2828 Buy Tickets 线段树

题意/Description

    Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

    The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

    It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

    People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.


读入/Input

   There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next Nlines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

      Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the     0th person and the person at the front of the queue was considered the first person in the queue.

      Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

   There no blank lines between test cases. Proceed to the end of input.


输出/Output

    For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.


题解/solution

    Draw a map,就知道最后一个插入到该位置的人位置是固定的,那么我们可以从后面进行插入操作,pos   o代表o要插入到pos位置,那么就是说pos前面要留出pos个位置,不是pos-1个,因为是从0开始的。
    线段树:ad记录该区间目前还剩ad个空位,每一次ins即插入的时候,如果该节点左儿子ad>=pos,那么只要在左儿子找就可以了否则要在右儿子中找,此时pos改为pos-左儿子ad。


代码/Code

type
  arr=record
    l,r:longint;
    ad:longint;
  end;
var
  n:longint;
  tree:array [0..800001] of arr;
  ap,a,ans:array [0..200001] of longint;
procedure cre(p,b,e:longint);
var
  m:longint;
begin
  with tree[p] do
    begin
      l:=b; r:=e;
      if l=r then
        begin
          ad:=1;
          exit;
        end;
      m:=(b+e) div 2;
      cre(p*2,b,m);
      cre(p*2+1,m+1,e);
      ad:=tree[p*2].ad+tree[p*2+1].ad;
    end;
end;

procedure ins(p,pos,o:longint);
var
  m:longint;
begin
  with tree[p] do
    begin
      dec(ad);
      if l=r then ans[l]:=o else
        if pos<=tree[p*2].ad then ins(p*2,pos,o) else
          ins(p*2+1,pos-tree[p*2].ad,o);
    end;
end;

procedure main;
var
  i:longint;
begin
  while not eof do
    begin
      readln(n);
      cre(1,1,n);
      for i:=1 to n do
        readln(ap[i],a[i]);
      for i:=n downto 1 do
        ins(1,ap[i]+1,a[i]);
      for i:=1 to n do
        write(ans[i],' ');
      writeln;
    end;
end;

begin
  main;
end.


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值