Buy Tickets
Time Limit: 4000MS |
|
Memory Limit: 65536K |
Total Submissions: 14905 |
|
Accepted: 7435 |
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) is 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 are 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
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
Test Instructions: The first line enters an integer n, which means there are n people, and the next n rows have two numbers per line x, y to represent the number of values Y is placed after the Count of X. (X==0 represents the number of Y in the first position), the sequence is required to be output sequentially. idea: Use line tree to do, the input data is inverted with the line tree update, so that the current number will be its last change.
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include < stdlib.h> #include <queue> #include <stack>using namespace std;const int maxn = 200001;struct node{int l; int R; int cnt; int sum;} Q[maxn<<4];int a[200001],b[200001];int n;int pf;void Build (int l,int R,int rt) {q[rt].l = l; Q[RT].R = R; q[rt].cnt = 0; q[rt].sum = 0; if (L = = r) {q[rt].sum = 1; return; } int mid = (l+r) >>1; Build (l,mid,rt<<1); Build (mid+1,r,rt<<1|1); Q[rt].sum = Q[rt<<1].sum + q[rt<<1|1].sum;} void Updata (int id,int k,int l,int r,int RT) {if (id<0) {pf = 1; return; } if (pf = = 1) {return; } if (L = = r && q[rt].sum = = 1) {q[rt].cnt = k; q[rt].sum = 0; return; } int mid = (l+r) >>1; if (id>q[rt<<1].sum) {id = id-q[rt<<1].sum; Updata (ID, k,mid+1,r,rt<<1|1); } else {updata (id,k,l,mid,rt<<1); } q[rt].sum = Q[rt<<1].sum + q[rt<<1|1].sum;} void Qurry (int l,int r,int RT) {if (L = = r) {if (L = = 1) {printf ("%d", q[rt].cnt); } else {printf ("%d", q[rt].cnt); } return; } int mid = (l+r) >>1; Qurry (l,mid,rt<<1); Qurry (mid+1,r,rt<<1|1);} int main () {while (scanf ("%d", &n)!=eof) {for (int i=0;i<n;i++) {scanf ("%d%d", &a[ I],&b[i]); } build (1,n,1); for (int i=n-1;i>=0;i--) {pf = 0; Updata (a[i]+1,b[i],1,n,1); } qurry (1,n,1); printf ("\ n"); } return 0;}
POJ 2828 Buy Tickets (segment tree)