POJ2828Buy Tickets(AC)

来源:互联网 发布:东吴证券怎么样 知乎 编辑:程序博客网 时间:2024/06/05 23:42
Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal 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

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


线段树的基础 http://www.cnblogs.com/TenosDoIt/p/3453089.html
//这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 200005typedef 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+1build((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 771 511 332 69我们现在叶子节点都是存的1 2 3 4,所以输入的pos+1了,相当于变成1 772 512 333 69首先是插入3 691,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是根节点所以不管怎么说都-1if (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;}