POJ2828Buy Tickets(AC)

Buy Tickets
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/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 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;
}








1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值