Buy Tickets
| Time Limit: 4000MS |
|
Memory Limit: 65536K |
| Total Submissions: 16273 |
|
Accepted: 8098 |
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.
Test instructions: Queue up to buy a ticket, give an integer n the next n rows of two numbers per row, A, b means that the number is inserted behind the first number
The problem: When this topic builds up, it is necessary to enter data in reverse order, so that the subsequent personnel can be inserted backwards in the corresponding position.
#include <stdio.h> #include <string.h> #include <algorithm> #define MAX 200005using namespace Std;int Sum[max<<2];int ans[max];void pushup (int o) {sum[o]=sum[o<<1]+sum[o<<1|1];} void Gettree (int o,int l,int R) {if (l==r) {sum[o]=1;//start time, let all positions stand for the return;} int mid= (L+R) >>1;gettree (O<<1,l,mid), Gettree (o<<1|1,mid+1,r);p ushup (o);//The bottom of the tree is the first one to go to the root. , the larger the number of people, the root of the}//number is n void update (int o,int l,int r,int Pos,int v) {if (l==r) {sum[o]=0;//when this position ans[r]=v; return;} int mid= (L+R) >>1;if (pos<=sum[o<<1]) update (O<<1,L,MID,POS,V);//Search left subtree else update (o<<1|1 , mid+1,r,pos-sum[o<<1],v);//Search right subtree because the value of the left end of the right subtree is pushup (o); Zuozi right End value +1 so find right subtree position}//Find to pos-left dial hand tree length OK int main () {int N,m,j,i;int A[max],b[max ];while (scanf ("%d", &n)!=eof) {gettree (1,1,n); for (i=1;i<=n;i++) scanf ("%d%d", &a[i],&b[i]); for (i=n; i>0;i--)//reverse-order the number into the update (1,1,n,A[i]+1,b[i]);//+1 is because the title data is from 0 when we lose the data for (i=1;i<=n;i++)//is from the first bit instead of the No. 0 loser so +1, so that the position corresponds to printf ("%d", Ans[i]);p rintf ("\ n");} return 0;}
POJ 2828 Buy Tickets "segment Tree single point update" "Reverse input"