Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 15426 | Accepted: 7688 |
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.
Source
题目意思:n个人在一个个的插队= =,然后会输入n个人的要插的位置,以及他们各自对应的价值。问当插队结束时,他们各自在队伍中的位置。
这个题目,是线段树。。。。。又是别人教我的,一起在第一天学的,第二天他就用上了,我却不会= =
这个也是简单的线段树,只需要用到更新函数即可。可以建一颗二叉树,叶子节点存放每个区间(我这里是[0,1)),每个区间代表一个空位,从左往右从下往上空位一个个增加。因为按照题目的输入,后来的人插队是可以改变前面的人的状态的,如果我从后面开始看,如果最后一个人插到第1个人的右边,那么他的最终位置也就确定了。所以就可以从后往前来一个个的确定位置。
#include<stdio.h>
#include<string.h>
#define M 200005
struct node{
int l,r,cnt;
}tree[M<<2];
struct que{
int pos,val;
}a[M];
int r[M];
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].cnt=r-l; //区间之差就是代表空位之差
if(l==r-1)return;
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid,r,root<<1|1);
}
void update(int root,int pos,int v)
{
tree[root].cnt--; //每次更新的时候就是有一个要插队的时候,必然会有一个位置被占用
if(tree[root].l==tree[root].r-1){
r[tree[root].l]=v; //存储答案。
return ;
}
if(tree[root<<1].cnt>pos) // 如果左子树的空位不大于要插的空位,就往右子树去看
update(root<<1,pos,v);
else {
update(root<<1|1,pos-tree[root<<1].cnt,v); //这里的pos-tree[root<<1].cnt表示要将左边空位的减去,否则就不能进右子树了
}
}
int main()
{
int n,i,j,k;
while(scanf("%d",&n)!=EOF)
{
build(0,n,1);
for(i=0;i<n;i++)
scanf("%d%d",&a[i].pos,&a[i].val);
for(i=n-1;i>=0;i--)
update(1,a[i].pos,a[i].val);
for(i=0;i<n;i++)
printf("%d ",r[i]);
printf("\n");
}
}