Poj Training Plan 2828_buy tickets (line segment tree/Single Point update)

Source: Internet
Author: User

Solution report

Question:

The order of completion of the queue.

Ideas:

When the data is processed backwards, the I-th person occupies the space of the J = POS [I] + 1.

The segment tree maintains the Gap Information.

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node {    int x,v;} num[201000];int sum[1000000],ans[201000];void cbtree(int rt,int l,int r) {    if(l==r) {        sum[rt]=1;        return ;    }    int mid=(l+r)/2;    cbtree(rt*2,l,mid);    cbtree(rt*2+1,mid+1,r);    sum[rt]=sum[rt*2]+sum[rt*2+1];}void update(int rt,int l,int r,int p,int v) {    int mid=(l+r)/2;    if(sum[rt]==p&&l==r) {        ans[l]=v;        sum[rt]=0;        return;    }    if(sum[rt*2]<p) {        update(rt*2+1,mid+1,r,p-sum[rt*2],v);    } else {        update(rt*2,l,mid,p,v);    }    sum[rt]=sum[rt*2]+sum[rt*2+1];}int main() {    int n,i,j;    while(~scanf("%d",&n)) {        cbtree(1,1,n);        for(i=0; i<n; i++) {            scanf("%d%d",&num[i].x,&num[i].v);        }        for(i=n-1; i>=0; i--) {            update(1,1,n,num[i].x+1,num[i].v);        }        for(i=1; i<n; i++)            printf("%d ",ans[i]);        printf("%d\n",ans[n]);    }    return 0;}

Buy tickets
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:13178   Accepted:6535

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

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.