您的位置:首页 > 其它

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

2016-03-09 14:58 281 查看
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

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

附上ac代码。。。

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: