POJ 2828 Buy Tickets

Source: Internet
Author: User

Original question:
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 is approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had-to-train-Mianyang, Sichuan Province for the winter camp selection of the national team of Olympia D in Informatics.

It was one o ' clock a.m. and dark outside. Chill Wind from the northwest does not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why isn't find a problem to think? That is none the less better than freezing to death!

People kept jumping the queue. Since It is too dark around, such moves would not being discovered even by the people adjacent to the queue-jumpers. "If every person in the queue is assigned a integral value and all the information to those who has jumped the queue And where they stand after queue-jumping are given, can I find out the final order of people in the queue? " Thought the Little Cat.

Input

There'll is several test cases in the input. Each test case consists of n + 1 lines where n (1≤n≤200,000) was given in the first line of the 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 is 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 is considered the first person In the queue.
Vali∈[0, 32767]-the i-th person is assigned the value Vali.
There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a space-separated integers which is the values of people in the order they STA nd 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 CAs E of the sample input.

English:

Queue problem, someone can jump in line, ask you the last ticket to the order of people.

#include <bits/stdc++.h> #include <iostream> #include <cstring> #define Lson l,m,rt<<1 #
Define Rson m+1,r,rt<<1|1 using namespace std;
const int MAXN=200000+10;
int seg[maxn<<2];
int ANS[MAXN],POS[MAXN],VAL[MAXN];
    void build (int l,int R,int RT) {int m= (L+R) >>1;
    seg[rt]=r-l+1;
    if (l==r) return;
    Build (Lson);
Build (Rson);
    } void Update (int l,int r,int rt,int po,int i) {int m= (L+R) >>1;
    seg[rt]--;
        if (l==r) {ans[l]=val[i];
    Return
    } if (SEG[RT&LT;&LT;1]&GT;=PO) update (lson,po,i);
        else {po-=seg[rt<<1];
    Update (RSON,PO,I);
    }} int main () {Ios::sync_with_stdio (false);
    int n;
        while (cin>>n) {build (1,n,1);
        memset (ans,0,sizeof (ans));
        for (int i=1;i<=n;i++) cin>>pos[i]>>val[i];
        for (int i=n;i>=1;i--) update (1,n,1,pos[i]+1,i); for (int i=1;i<=n;i++) {if (i!=n) cout<<ans[i]<< "";
        else cout<<ans[i]<<endl;
}} return 0;
 }

Answer:

First thought for a while, came up with such an idea.
First define an array as the priority of the position of the team, the smaller the number, the higher the priority, if now x people are already lined up, then there is a person who wants to insert the first position of the team, then the first person to the back of everyone's priority level plus 1. Because, it is in the line tree exercises to see this topic, it is obvious to think of the interval update operation, just excited to write code when suddenly realized that the problem of lazy operation. Considering the nature of the lazy operation, each time equivalent to store the updated value in a certain interval, the interval will not be updated when the query is not made, however, now to find out the updated values of all leaf nodes, you must query all leaf nodes. The complexity of the time is ~ hehe =_=
Although not made, but always a little idea, the interval query has more understanding, is a harvest it

Later on the online search of the http://www.cnblogs.com/CheeseZH/archive/2012/04/29/2476134.html
Very detailed, thinking is also very ingenious.
Use the segment tree to store the remaining idle positions for each current interval, such as a person preemption 3rd, then the front will be set aside two positions, so from the back to continue to insert the corresponding personnel. See the blog above for 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.