线段树(倒序操作):POJ 2828 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的更多相关文章
- 线段树(单点更新) POJ 2828 Buy tickets
题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- poj 2828 Buy Tickets(树状数组 | 线段树)
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...
- poj 2828 Buy Tickets 【线段树点更新】
题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...
- POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19725 Accepted: 9756 Desc ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
题目链接: 传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Description Railway tickets were d ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ - 2828 Buy Tickets(线段树单点更新)
http://poj.org/problem?id=2828 题意 排队买票,依次给出当前人要插队的位置,每个人有个编号,然后问你最后整个的序列是什么? 分析 最后一个人的要插入的位置是确定的,所以逆 ...
随机推荐
- Session 原理
Session天天用,但是你真的理解了么? 今天遇到了这个问题,于是研究了一下.要解决这个问题,首先就要明白一些Session的机理.Session在服务器是以散列表形式存在的,我们都知道Sessio ...
- svn设置
svnserver -d -r /home/peter.mycode 如果想要开机自启动,将上述启动命令添加到:/etc/rc.local中.
- angularjs kindEditor 中自定义按钮 弹出dialog
1.angular-kindeditor.js 第38行左右加 editorConfig.items = ["placehoder"]; 2.en.js 第234行 placeho ...
- 在java中使用 File.renameTo(File)实现重命名.
Here is part of my files: [北京圣思园Java培训教学视频]Java.SE.前9日学习成果测试题(2010年12月2日).rar [北京圣思园Java培训教学视频]Java. ...
- sp_addlinkedserver的一些操作
sp_addlinkedserver 创建一个链接的服务器,使其允许对分布式的.针对 OLE DB 数据源的异类查询进行访问.在使用 sp_addlinkedserver 创建链接的服务器之后,此服务 ...
- Debug your C# project more efficiently
I am a very beginner working with C# and Visual Studio 2013. When I debug my project, I always reope ...
- phpcms源码跟踪(1)
本次跟踪解决几个问题: 1.缓存文件从哪里来,过程中被删除了怎么办 2.模板html是如何被引入的 进入首页时,通过最初的调用,进入控制器\phpcms\modules\content\index.p ...
- Mobile开发之meta篇
Mobile开发之meta篇 <meta name="viewport" content="width=device-width, initial-scale=1, ...
- 永久关闭防火墙和selinux
临时关闭selinux: setenforce 0 //设置SELinux 成为permissive模式 彻底禁用selinux: 使用root用户,vim /etc/sysconfig/sel ...
- Crystal Report制作使用
Crystal Report制作使用 本文主要划分为以下六部分: 一.Crystal Report for .NET 的功能 二.Crystal Report总体结构 三.报表数据访问执行模式 四.报 ...