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. mac skim 修改背景色

    defaults write -app skim SKPageBackgroundColor -array 0.78 0.93 0.80 1

  2. POS tagging的解釋

    轉錄文章~~ 什么是词性标注(POS tagging) Tue, 04/13/2010 - 10:36 — Fuller 词性标注也叫词类标注,POS tagging是part-of-speech t ...

  3. 第二天——hibernate讲完了

    hibernate 逐步优化 第一步 只按照步骤来提取的 jre包导入错误 第二步 继续封装,把增删改查提取出来,同时进行代码的封装 HQL语句 be stranger in the code .be ...

  4. hibernate之增删改查demo

    package dao; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import o ...

  5. HTML5 Canvas实现刮刮卡效果实例

    HTML: <style> #canvas { border: 1px solid blue; position: absolute; left: 10px; top: 10px; bac ...

  6. Ubuntu Server 14.04 下root无法ssh登陆

    今天安装了Ubuntu Server 14.04   在终端配置了root密码后,使用SecureCRT和putty竟然不能ssh登陆,SecureCRT一直提示密码不对,但是可以肯定输入的密码100 ...

  7. 【转】 C++库常用函数一览

    本文中提到的函数库有:<string> <cctype> <algorithm> <cmath> <cstdlib> <iomanip ...

  8. 远程推送,集成极光的SDK,证书制造

    由于iOS操作系统限制,我们APP在后台不能做操作,也不能接收任何数据,所以需要用推送来接收消息. APNs服务,苹果官方网址:https://developer.apple.com/library/ ...

  9. UIDatePikcer的基本用法

    - (void)viewDidLoad { [super viewDidLoad]; _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectM ...

  10. 运用BeanUtils构建通用的查询 更新方法(个人拙作,不喜勿喷)

    ------------------------------------更新方法----------------------------------- public void update(Strin ...