Poj2828 buy tickets (line segment Tree single point update)

Source: Internet
Author: User

Link: HuangJing
Idea: given n inserts, if the push is positive, the insert will affect the last position, so we should solve the problem in reverse order, if this person is in the position of the I-th person, there must be an I-null position in front of this person, because it is taken forward from the back, therefore, when updating each time, we should consider inserting this person after there is an I vacancy in the front. Then, the final sequence will meet the conditions .. Question:
Language: Default Buy tickets
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:13705   Accepted:6846

Description

Railway Tickets were difficult to buy und 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 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]-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
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>#include<cmath>#include<string>#include<queue>#define eps 1e-9#define ll long long#define INF 0x3f3f3f3f#define lson l,mid,dex<<1#define rson mid+1,r,dex<<1|1using namespace std;const int maxn=200000+10;int tree[maxn<<2],ans[maxn],pos[maxn],val[maxn],n;void push_up(int dex){    tree[dex]=tree[dex<<1]+tree[dex<<1|1];}void buildtree(int l,int r,int dex){    if(l==r)    {        tree[dex]=1;        return;    }    int mid=(l+r)>>1;    buildtree(lson);    buildtree(rson);    push_up(dex);}int update(int l,int r,int dex,int p){    tree[dex]--;    if(l==r)        return l;    int mid=(l+r)>>1;    if(tree[dex<<1]>=p) update(lson,p);    else    {        p=p-tree[dex<<1];        update(rson,p);    }}int main(){    int ly;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            scanf("%d%d",&pos[i],&val[i]);            pos[i]++;        }        buildtree(1,n,1);        for(int i=n;i>=1;i--)        {            ly=update(1,n,1,pos[i]);            ans[ly]=val[i];        }        for(int i=1;i<n;i++)          printf("%d ",ans[i]);        printf("%d\n",ans[n]);    }    return 0;}




Poj2828 buy tickets (line segment Tree single point update)

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.