• Buy Tickets(线段树)


     Buy Tickets
    Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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.


     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define N 200010
     5 #define lson p << 1
     6 #define rson p << 1 | 1
     7 using namespace std;
     8 
     9 struct Nod
    10 {
    11     int l , r ;
    12     int va ; // Gap
    13 }node[N << 2];
    14 
    15 int pos[N] , val[N] , ans[N] ;
    16 
    17 void build ( int l , int r , int p )
    18 {
    19     node[p].l = l ;
    20     node[p].r = r ;
    21     node[p].va = r - l + 1 ;
    22     if ( l == r)
    23         return ;
    24     int mid = ( l + r ) >> 1 ;
    25     build ( l , mid , lson ) ;
    26     build ( mid + 1 , r , rson ) ;
    27 }
    28 
    29 int update ( int ps , int p )
    30 {
    31     node[p].va-- ;   //Gap - 1 ;
    32     if ( node[p].l == node[p].r ) {
    33         return node[p].l ; //return the position of the insert
    34     }
    35     if ( node[lson].va >= ps ) {
    36         update ( ps , lson ) ;//when lson's Gap equal to or greater than the insertion location , inserted to the left
    37     }
    38     else {
    39         ps -= node[lson].va ;//when Gap on the left is less than the ps , insert the right side , the right of the insertion laction is
    40                                 // ps minus Gap on the left
    41         update ( ps , rson ) ;
    42     }
    43 }
    44 
    45 int main ()
    46 {
    47     //freopen ( "a.txt" , "r" , stdin ) ;
    48     int n ;
    49     while (~scanf ("%d" , &n ) ) {
    50         build ( 1 , n , 1 ) ;
    51         for ( int i = 1 ; i <= n ; i++ ) {
    52             scanf ("%d%d" , pos + i , val + i ) ;
    53         }
    54         for ( int i = n ; i >= 1 ; i-- ) {
    55             int id = update ( pos[i] + 1 , 1 ) ;    // get the insert position
    56             ans[id] = val[i] ;  // in ans Array
    57         }
    58         for ( int i = 1 ; i < n ; i++ ) {
    59             printf ( "%d " , ans[i] ) ;
    60         }
    61         printf ( "%d
    " , ans[n] ) ;
    62     }
    63     return 0 ;
    64 }
    倒推法

     集训时大神介绍了一种能节省空间的写法:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define N 200010
     5 #define lson o << 1 , l , mid
     6 #define rson o << 1 | 1 , mid + 1 , r
     7 using namespace std;
     8 
     9 int va[N << 2] ; // Gap
    10 
    11 int pos[N] , val[N] , ans[N] ;
    12 
    13 void build (int o , int l , int r )
    14 {
    15     va[o] = r - l + 1 ;
    16     if ( l == r )
    17         return ;
    18     int mid = ( l + r ) >> 1 ;
    19     build ( lson ) ;
    20     build ( rson ) ;
    21 }
    22 
    23 int update ( int ps ,int o , int l , int r  )
    24 {
    25     va[o]-- ;   //Gap - 1 ;
    26     if ( l == r ) {
    27         return l ; //return the position of the insert
    28     }
    29     int mid = ( l + r ) >> 1 ;
    30     if ( va[o << 1] >= ps ) {
    31         update ( ps , lson ) ;//when lson's Gap equal to or greater than the insertion location , inserted to the left
    32     }
    33     else {
    34         ps -= va[o << 1] ;//when Gap on the left is less than the ps , insert the right side , the right of the insertion laction is
    35                                 // ps minus Gap on the left
    36         update ( ps , rson ) ;
    37     }
    38 }
    39 
    40 int main ()
    41 {
    42     //freopen ( "a.txt" , "r" , stdin ) ;
    43     int n ;
    44     while (~scanf ("%d" , &n ) ) {
    45         build ( 1 , 1 , n ) ;
    46         for ( int i = 1 ; i <= n ; i++ ) {
    47             scanf ("%d%d" , pos + i , val + i ) ;
    48         }
    49         for ( int i = n ; i >= 1 ; i-- ) {
    50             int id = update ( pos[i] + 1 , 1 , 1 , n ) ;    // get the insert position
    51             ans[id] = val[i] ;  // in ans Array
    52         }
    53         for ( int i = 1 ; i < n ; i++ ) {
    54             printf ( "%d " , ans[i] ) ;
    55         }
    56         printf ( "%d
    " , ans[n] ) ;
    57     }
    58     return 0 ;
    59 }
    省了4000k

     

  • 相关阅读:
    springboot~使用docker构建gradle项目
    CH BR8(小学生在上课-逆元和互质数一一对应关系)
    UNIX环境高级编程第二版代码笔记
    【Linux学习笔记】用nc实现两台主机间的文件传输(不需要输密码)
    hdu 1159
    轻量级的原型设计工具-Axure RP
    在Ubuntu 12.10 上安装部署Openstack
    [Android 中级]Voip之CSipSimple类库的编绎
    OpenStack云计算快速入门之一:OpenStack及其构成简介
    OpenStack云计算快速入门之二:OpenStack安装与配置
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4277339.html
Copyright © 2020-2023  润新知