Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 20297 | Accepted: 10016 |
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 Olympiad in Informatics.
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 would 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 consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of 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 was considered the first person in the queue.
- Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
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
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 case of the sample input.
//这http://www.cnblogs.com/CheeseZH/archive/2012/04/29/2476134.html //这http://blog.csdn.net/qingniaofy/article/details/7748507 关于这道题讲的挺好的
//AC //整个题目是一个插入的动作,如果是按部就班的话,因为数据量太大,无法AC //看了题解说是要用线段树,其实就是二叉搜索树 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define MAXN 200005 typedef struct in { int pos; int val; }ins; typedef struct node { int left; int right; int res; //记录空间的空位 }nodes; ins input[MAXN]; int ans[MAXN]; nodes tree[MAXN<<2]; /* 功能:构建线段树 root:当前线段树的根节点下标 start:数组的起始位置 end:数组的结束位置 */ //定义包含n个节点的线段树 nodes tree[n],tree[0]表示根节点。 //那么对于节点tree[i],它的左孩子是tree[2*i+1],右孩子是tree[2*i+2] void build(int root, int start, int end) { tree[root].left = start; tree[root].right = end; if (start == end) { tree[root].res = 1; return; } //+优先级高,移位优先级低 build((root<<1) +1, start, (start+end)>>1); //左儿子 (start+end)>>1 就是(start+end) /2 (root<<1)|1 = root*2+1 build((root<<1) + 2, ((start + end)>>1)+1, end); //右儿子(start + end)>>1|1 就是(start + end)/ 2+1 //刷新根节点的空余个数 tree[root].res = tree[(root << 1) + 1].res + tree[(root << 1) + 2].res; } /* 0 77 1 51 1 33 2 69 我们现在叶子节点都是存的1 2 3 4,所以输入的pos+1了,相当于变成 1 77 2 51 2 33 3 69 首先是插入3 69 1,4结点有4个位置, 1,2结点有2个位置,小于3,因此放到1,4结点右孩子,且1,4结点空位置减1 到了1,4右孩子后,只要找到第3-2=1个位置即可,而3,4结点的左孩子3,3含有1个空位置,1>=1,所以放到3,3位置了。 插入2 33 插入2 51 此时1,4的左孩子只有1个位置,1<2,所以只能放到1,4的右孩子3,4上 3,4的左孩子有0个位置,所以只能放在3,4的右孩子4,4上。 插入1 77 */ //寻找pos在线段树中的位置 void updatetree(int root, int pos, int val) { //刷新节点空余个数 tree[root].res -=1; //root = 0是根节点所以不管怎么说都-1 if (tree[root].left == tree[root].right) //到叶子节点了 { ans[tree[root].left] = val; //已经是叶子节点了,所以left和right都是一样的 return; } //如果pos大于节点个数,那就找右儿子,如果小于节点剩余个数,那就找左儿子 if (pos <= (tree[(root << 1) + 1].res)) { //左儿子 updatetree((root << 1) + 1, pos, val); } else { //这步很重要,自己忘了,得要去掉左儿子剩余的个数 pos = pos - tree[(root << 1) + 1].res; //右儿子 updatetree((root << 1) + 2, pos, val); } return; } //从最后一个数字开始战队,因为最后一个人的位置肯定是定下来了 int main() { int N = 0; int i = 0; freopen("input.txt","r",stdin); while (1 == scanf("%d", &N)) { //创建线段树 build(0, 1, N); for (i = 1; i <= N; i++) { scanf("%d %d", &input[i].pos, &input[i].val); } //从后面开始放,后面的位置肯定是固定了的 //最后的叶子节点代码位置 for (i = N; i >= 1; i--) { updatetree(0, input[i].pos + 1, input[i].val); } for (i = 1; i <= N; i++) { printf("%d ", ans[i]); } printf("\n"); } return 0; }