Buy Tickets
 

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.

  写这道题之前要理清思路。

  考虑后面加的人,它目标在先加的人之前,它们前后关系就是稳定的,于是考虑倒着扫。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int tr[maxn<<];
int man[maxn],ID[maxn],ans[maxn]; void Init(int node,int l,int r)
{
tr[node]=r-l+;
if(l==r)return;
int mid=(l+r)>>;
Init(node<<,l,mid);
Init(node<<|,mid+,r);
} void Insert(int node,int l,int r,int to,int id)
{
tr[node]--;
if(l==r){
ans[l]=id;
return;
}
int mid=(l+r)>>;
if(to<=tr[node<<])
Insert(node<<,l,mid,to,id);
else
Insert(node<<|,mid+,r,to-tr[node<<],id);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
Init(,,n);
for(int i=;i<=n;man[i]++,i++)
scanf("%d%d",&man[i],&ID[i]);
for(int i=n;i>=;i--)
Insert(,,n,man[i],i);

for(int i=;i<=n;i++)
printf("%d ",ID[ans[i]]);
printf("\n");
}
return ;
}

线段树(倒序操作):POJ 2828 Buy Tickets的更多相关文章

  1. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...

  2. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  3. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  4. poj 2828 Buy Tickets 【线段树点更新】

    题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...

  5. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  6. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  7. POJ 2828 Buy Tickets(线段树 树状数组/单点更新)

    题目链接: 传送门 Buy Tickets Time Limit: 4000MS     Memory Limit: 65536K Description Railway tickets were d ...

  8. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  9. POJ - 2828 Buy Tickets(线段树单点更新)

    http://poj.org/problem?id=2828 题意 排队买票,依次给出当前人要插队的位置,每个人有个编号,然后问你最后整个的序列是什么? 分析 最后一个人的要插入的位置是确定的,所以逆 ...

随机推荐

  1. (转)JavaScript判断浏览器类型及版本

    IE 只有IE支持创建ActiveX控件,因此她有一个其他浏览器没有的东西,就是ActiveXObject函数.只要判断window对象存在ActiveXObject函数,就可以明确判断出当前浏览器是 ...

  2. MSSQLSERVER未分离LDF删除情况下的MDF附加

    经过网上资料搜索,此方法可以解决. LDF日志不要轻易删除,恢复主数据要用到,如果删除,记得先分离,然后移动到另外的地方. 下面是针对未分离删除日志文件,MDF文件附加,提示找不到日志的问题的解决方法 ...

  3. 后台线程,优先级,sleep,yield

    1.后台线程,是指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可获取的部分.当所有非后台线程结束时,程序也就 终止了,同时会杀死进程中所有后台线程.main()是一个非后 ...

  4. IOS-textField

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  5. directsound 应用实例

    sdk里边有个文件夹Samples\C++\XInput\AudioController这个就是

  6. centos 安装vnc服务

    1.安装tigervnc-server yum install tigervnc-server 2.启动vnc服务 vncserver:1 [错误提示待解决bad display name " ...

  7. js基础 - 兼容代码

    js基础 - 兼容代码 . scrollTop . chrome document.body.scrollTop . IE && firefox document.documentEl ...

  8. jquery元素查找方法集锦

    jQuery常用的元素查找方法总结 $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到 ...

  9. 隐藏和显示 ng-show ng-hide

    <div ng-controller='DeathraymenueController'>    <button ng-click="toggleMenue()" ...

  10. Content-Type实体首部字段

      现代互联网下,每天都会有数以亿计的各种媒体对象经由HTTP传输,如图像,文本,影视以及软件程序等.这些数据都包含在HTTP报文的实体内容中,如果把HTTP报文想像成一份快递,HTTP实体就是快递实 ...