zoukankan      html  css  js  c++  java
  • 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.
    


  • 相关阅读:
    Vue.js_础学习之DOM操作
    node REPL
    node npm
    Vue.js_getter and setter
    tomcat+nginx+redis实现均衡负载以及session共享
    深入浅出微服务框架dubbo(一):基础篇
    Linux下安装zip解压功能
    Linux下查看CPU型号,内存大小,硬盘空间的命令
    Linux查看系统信息命令
    MyBatis自动生成代码之generatorConfig配置文件及其详细解读
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319693.html
Copyright © 2011-2022 走看看