• poj-----(2828)Buy Tickets(线段树单点更新)


    Buy Tickets
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 12930   Accepted: 6412

    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 N lines 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.

    Sample Input

    4
    0 77
    1 51
    1 33
    2 69
    4
    0 20523
    1 19243
    1 3890
    0 31492

    Sample Output

    77 33 69 51
    31492 20523 3890 19243

    Hint

    The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

    Source

     
    代码:   倒着插入数据....
    代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 const int maxn=200005;
     4 struct node
     5 {
     6   int lef,rig,ps;  //ps-->在前方有多少个空位
     7   int val;
     8   int mid(){
     9    return lef+((rig-lef)>>1);
    10   }
    11 };
    12 struct sac
    13 {
    14    int pos,val;
    15 };
    16 sac sav[maxn];
    17 node reg[maxn<<2];
    18 int ans[maxn];
    19 int cnt;
    20 void Build(int left,int right,int pos)
    21 {
    22   reg[pos]=(node){left,right,1,0};
    23   if(left==right) return ;
    24   int mid=reg[pos].mid();
    25   Build(left,mid,pos<<1);
    26   Build(mid+1,right,pos<<1|1);
    27   reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
    28 }
    29 
    30 void Update(int ps,int val ,int pos)
    31 {
    32     if(reg[pos].lef==reg[pos].rig)
    33     {
    34       reg[pos].ps=0;
    35       reg[pos].val=val;
    36       return ;
    37     }
    38    int mid=reg[pos].mid();
    39    if(reg[pos<<1].ps>ps)
    40      Update(ps,val,pos<<1);
    41     else Update(ps-reg[pos<<1].ps,val,pos<<1|1);
    42    reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
    43   return ;
    44 }
    45 void Query(int pos)
    46 {
    47     if(reg[pos].lef==reg[pos].rig)
    48     {
    49         if(cnt==0)
    50             printf("%d",reg[pos].val);
    51         else printf(" %d",reg[pos].val);
    52         cnt++;
    53       return ;
    54     }
    55     int mid=reg[pos].mid();
    56     Query(pos<<1);
    57     Query(pos<<1|1);
    58     return ;
    59 }
    60 int main()
    61 {
    62     int n;
    63     while(scanf("%d",&n)!=EOF)
    64     {
    65         cnt=0;
    66         Build(1,n,1);
    67         for(int i=0;i<n;i++)
    68          scanf("%d%d",&sav[i].pos,&sav[i].val);
    69         for(int i=n-1;i>=0;i--)
    70           Update(sav[i].pos,sav[i].val,1);
    71         Query(1);
    72         puts("");
    73     }
    74     return 0;
    75 }
    View Code

    优化代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 const int maxn=200005;
     4 struct node
     5 {
     6   int lef,rig,ps;  //ps-->在前方有多少个空位
     7   int mid(){
     8    return lef+((rig-lef)>>1);
     9   }
    10 };
    11 struct sac
    12 {
    13    int pos,val;
    14 };
    15 sac sav[maxn];
    16 node reg[maxn<<2];
    17 int ans[maxn];
    18 void Build(int left,int right,int pos)
    19 {
    20   reg[pos]=(node){left,right,1};
    21   if(left==right) return ;
    22   int mid=reg[pos].mid();
    23   Build(left,mid,pos<<1);
    24   Build(mid+1,right,pos<<1|1);
    25   reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
    26 }
    27 
    28 void Update(int ps,int val ,int pos)
    29 {
    30     if(reg[pos].lef==reg[pos].rig)
    31     {
    32       reg[pos].ps=0;
    33       ans[reg[pos].lef]=val;
    34       return ;
    35     }
    36    int mid=reg[pos].mid();
    37    if(reg[pos<<1].ps>ps)
    38      Update(ps,val,pos<<1);
    39     else Update(ps-reg[pos<<1].ps,val,pos<<1|1);
    40    reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
    41   return ;
    42 }
    43 int main()
    44 {
    45     int n;
    46     while(scanf("%d",&n)!=EOF)
    47     {
    48         Build(1,n,1);
    49         for(int i=0;i<n;i++)
    50          scanf("%d%d",&sav[i].pos,&sav[i].val);
    51         for(int i=n-1;i>=0;i--)
    52           Update(sav[i].pos,sav[i].val,1);
    53          printf("%d",ans[1]);
    54      for(int i=2;i<=n;i++)
    55         printf(" %d",ans[i]);
    56      puts("");
    57     }
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    那些年我们写过的T-SQL(下篇)(转)
    好的架构是进化来的,不是设计来的
    dhcpsrv:windows系统的优秀开源免费dhcp serve软件
    PXE(preboot execution environment):【网络】预启动执行环节:安装 debian 9系列:成功
    PXE(preboot execution environment):【网络】预启动执行环节:安装 ubuntu、rehat系列:成功
    问题:UltraISO:这个软件有问题,它制作的iso文件会造成无法正确识别。用PowerISO吧
    自windows8以后,所有版本(专业版、企业版、旗舰版)都支持从 vhd 启动
    SpringCloud Feign
    SpringCloud Netflix Ribbon(负载均衡)
    SpringCloud Netflix Eureka(服务注册/发现)
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3896807.html
Copyright © 2020-2023  润新知