Poj 2828 -- buy tickets (tree array, line segment tree -- reverse traversal)

Source: Internet
Author: User

Buy tickets
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:13496   Accepted:6726

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


--------------------Split line--------------------


Question:

There are n people waiting in the queue to buy tickets. Each person has two messages: POS [I] and Val [I]. Pos [I] indicates that the I-th person is on the right of the POs [I] person, find the final sequence Val [I]


Ideas:

In reverse traversal, the POs [I] is changed to the number of vacancies in front of the I-th individual.

1: Tree array bit

Assume that the position of the person I is P, and the condition must be p-sum [p] = POS [I], and binary search can be used.

2: Line Segment tree St

Each node records the remaining length of the current interval. When someone is in the queue, the left and right sides are prioritized. When someone is inserted to the specified position, it is updated to the Father's Day point, remaining length-1



1. Tree array code

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>const int maxn=200001;using namespace std;int n,f[maxn];int c[maxn+1];struct node{    int x,y;}a[maxn+1];void update(int x,int v){    for(int i=x;i<=maxn;i+=i&-i){        c[i]+=v;    }}int getsum(int x){    int sum=0;    for(int i=x;i>0;i-=i&-i){        sum+=c[i];    }    return sum;}int Binary_search(int x,int y){    int l=2,r=n+1;    while(l<r){        int m=(l+r)>>1;        int tmp=m-getsum(m)-1;        if(tmp>=x) r=m;        else l=m+1;    }    f[l]=y;    return l;}int main(){    while(scanf("%d",&n)!=EOF){        memset(c,0,sizeof(c));        for(int i=0;i<n;++i){            scanf("%d %d",&a[i].x,&a[i].y);            a[i].x++;        }        for(int i=n-1;i>=0;--i){            update(Binary_search(a[i].x,a[i].y),1);        }        for(int i=2;i<=n+1;++i){            printf("%d",f[i]);            if(i!=n+1) printf(" ");        }        puts(" ");    }    return 0;}



2: Line Segment tree

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define localconst int maxn=200001;using namespace std;int n,f[maxn+1];struct node{    int x,y;}a[maxn+1];int len[maxn<<2];void push_up(int rt){    len[rt]=len[rt<<1]+len[rt<<1|1];}void build(int l,int r,int rt){    if(l==r){        len[rt]=1;        return ;    }    int m=(l+r)>>1;    build(lson);    build(rson);    push_up(rt);}void query(int x,int y,int l,int r,int rt){    if(l==r){        len[rt]--;        f[l]=y;        return ;    }    int m=(l+r)>>1;    if(len[rt<<1] >=x){        query(x,y,lson);    }    else query(x-len[rt<<1],y,rson);    push_up(rt);}int main(){    while(scanf("%d",&n)!=EOF){        for(int i=0;i<n;++i){            scanf("%d %d",&a[i].x,&a[i].y);            a[i].x++;        }        build(1,n,1);        for(int i=n-1;i>=0;--i){            query(a[i].x,a[i].y,1,n,1);        }        for(int i=1;i<=n;++i){            printf("%d",f[i]);            if(i!=n) printf(" ");        }        printf("\n");    }    return 0;}


Poj 2828 -- buy tickets (tree array, line segment tree -- reverse traversal)

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.