POJ 2828 Buy Tickets (segment tree single node query location update)

Source: Internet
Author: User

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 15533 Accepted: 7759

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 test case. The nextN lines contain the pairs of valuesposi and Vali in the increasing order ofI ( 1 ≤iN). For each i, the ranges and meanings ofposi andVali is as follows:

    • posi ∈[0, i ? 1]-the i-th person came to the queue and stood right behind theposi -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 valueVali.

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

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 CAs E of the sample input.

Source

POJ monthly--2006.05.28, Zhu, Zeyuan

I did not think of the solution to see the online thinking, reverse insertion such as 1th in the position of 1, 2nd to 1th in the position, third is also in the position of 1, then the order is 0 1 3 2 because the last person to insert the position must be his
Want to position, then we insert in reverse order, we must meet him first, and then we insert number 2nd, originally 2nd to insert 2 position (because he wanted in the back of 1), but was accounted for third, but also must meet the number third, put him back.
Finally, insert number 1th in position 1.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace std;const int inf=200003; int dict[inf];struct tree{int left,right,num;}    Tree[inf<<2];int Create (int root,int left,int right) {tree[root].left=left;    Tree[root].right=right;    if (left==right) {return tree[root].num=1;    } int a,b,mid= (left+right) >>1;    A=create (Root<<1,left,mid);    B=create (Root<<1|1,mid+1,right); return tree[root].num=a+b;} void Update (int root,int pos, int val) {if (tree[root].left==tree[root].right) {Tree[root].num=0;dict[tree[ro        Ot].left]=val;    return;    } if (pos<=tree[root<<1].num) update (root<<1,pos,val); else Update (Root<<1|1,pos-tree[root<<1].num,vaL); Tree[root].num=tree[root<<1].num+tree[root<<1|1].num;}    int main () {int n;        while (cin>>n) {Create (1,1,n); memset (Dict,0,sizeof (dict));        int Pos[inf],val[inf];        for (int i=0;i<n;i++) scanf ("%d%d", &pos[i],&val[i]);        for (int i=n-1;i>=0;i--) {update (1,++pos[i],val[i]); } for (int i=1;i<=n;i++) {printf ("%d%c", dict[i],i==n?)        ' \ n ': '); }} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2828 Buy Tickets (segment tree single node query location 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.