Poj2828 -- buy tickets (line segment tree + details)

Source: Internet
Author: User
Buy tickets
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:13618   Accepted:6802

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 Olympus in ICS ICs.

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 wocould 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 consistsN+ 1 lines whereN(1 ≤N≤ 200,000) is given in the first line of the test case. The nextNLines contain the pairs of ValuesPosiAndValiIn the increasing orderI(1 ≤IN). For eachI, The ranges and meaningsPosiAndValiAre as follows:

  • Posiε [0,I? 1]-I-Th person came to the queue and stood right behindPosi-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]-I-Th person was assigned the valueVali.

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

40 771 511 332 6940 205231 192431 38900 31492

Sample output

77 33 69 5131492 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

Poj monthly -- 2006.05.28, Zhu, zeyuan

 

The general idea is that there are n queues in each case. Each case gives N, which indicates that there are n queues, and each gives P, V, which means that the person codenamed V is inserted behind the person p, q: What is the final team arrangement?

At the beginning of the question, the entire queue is empty. That is to say, if I, J: indicates that the person codenamed J is inserted behind the person I, that is to say, I must have been found before him, and his position is I + 1.

Therefore, moving forward from the back to the front, each person encounters a fixed position, and the position selected by the last person will not change. Similarly, because of the flashback input, the person I entered the queue, that is to say, there must be I space in front of him.

After such conversion, we can use the line segment tree. The code is stored in lazy, and the CL array contains the number of spaces contained in each node.

 

 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 210000#define lmin 1#define rmax n#define root lmin,rmax,1#define lson l,(l+r)/2,rt<<1#define rson (l+r)/2+1,r,rt<<1|1#define now l,r,rt#define int_now int l,int r,int rtstruct node{    int a , b ;} p[maxn];int cl[maxn<<2] , lazy[maxn<<2] ;int n , i ;void push_up(int_now){    if( l != r )        cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;}void creat(int_now){    cl[rt] = lazy[rt] = 0 ;    if( l != r )    {        creat(lson);        creat(rson);        push_up(now);    }    else        cl[rt] = 1 ;    return ;}void update(int a,int k,int_now){    if( l == r && cl[rt] == 1 )    {        cl[rt] = 0 ;        lazy[rt] = k ;    }    else    {        if( a <= cl[rt<<1] )            update(a,k,lson);        else            update(a-cl[rt<<1],k,rson);        push_up(now);    }    return ;}void pre(int_now){    if( l == r )    {        if(n == 1)            printf("%d\n", lazy[rt]);        else            printf("%d ", lazy[rt]);        n-- ;    }    else    {        pre(lson);        pre(rson);    }    return ;}int main(){    while( scanf("%d", &n)!=EOF )    {        for(i = 0 ; i < n ; i++)        {            scanf("%d %d", &p[i].a, &p[i].b);            p[i].a++ ;        }        creat(root);        for(i = n - 1 ; i >= 0 ; i--)        {            update(p[i].a,p[i].b,root);        }        pre(root);    }    return 0;}


 

Poj2828 -- buy tickets (line segment tree + details)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.