POJ 2828 排队插队(线段树_好题)


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 Valiin 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.



输入n个有序对<pi, vi> pi表示在第pi个位置后面插入一个值为vi的人。。。最后一个人的位置一定是不变的,顺着思考下去,

考虑中间的状态,一个人坐下后,就定死了,如何后面有人插到他的前面,这时位置才会发生变动。

那么我们就倒过来思考,最后的都已经做好了,我们再来安排当前的位置,即要坐在pi+(前面已经做了几个人)...

(这边就有点抽象了)。

线段树于是就提供了动态查询的的功能。从整体出发,看看左子树剩余的空位是否满足pi,(如果前面有人坐着了,这边的位置已经去掉了)。

如果不满足,则跑到右子数。并减去左子树留下的空位。。。

想通了问题就很简单....

这边盗个图。。。方便同志们了解:

初始状态

首先是插入3 69

14结点有4个位置,

12结点有2个位置,小于3,因此放到14结点右孩子,且14结点空位置减1

到了14右孩子后,只要找到第3-2=1个位置即可,而34结点的左孩子33含有1个空位置,1>=1,所以放到33位置了。

 

插入2 33

 

★关键是这里如何处理★

插入2 51

此时14的左孩子只有1个位置,1<2,所以只能放到14的右孩子34

34的左孩子有0个位置,所以只能放在34的右孩子44上。

 

插入1 77

 

 附上ac代码。。。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 2e5+10;
int sum[N<<2],b[N];
struct node
{
	int p;
	int num;
}a[N];

void build(int l,int r,int rt)
{
	if(l==r) {
		sum[rt]=1;
		return;
	}
	int mid=(l+r)>>1;
	if(l<=mid) build(l,mid,rt<<1);
	if(r>mid) build(mid+1,r,rt<<1|1);
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void ask(int p,int l,int r,int rt,int num)
{
	if(l==r) {
		sum[rt]--;
		b[l]=num;
		return ;
	}
	int mid=(l+r)>>1;
	if(sum[rt<<1]>=p) ask(p,l,mid,rt<<1,num);
	else ask(p-sum[rt<<1],mid+1,r,rt<<1|1,num);
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}


int main()
{
	int n,i,j;
	while(scanf("%d",&n)!=EOF) {
		for(i=1;i<=n;i++) {
			scanf("%d%d",&a[i].p,&a[i].num);
			a[i].p++;
		}
		build(1,n,1);
		for(i=n;i>0;i--) {
			ask(a[i].p,1,n,1,a[i].num);
		}
		for(i=1;i<=n;i++) {
			printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
} 
















 








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值