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<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
struct node{
int l,r;
int sp;
}t[mxn<<];
int id[mxn],val[mxn];
int res[mxn];
int n;
void sp_update(int r){
t[r].sp=t[r*].sp+t[r*+].sp;
return;
}
void Build(int rt,int left,int right){
t[rt].l=left;
t[rt].r=right;
if(left==right){t[rt].sp=;return;}
int mid=(left+right)/;
Build(rt*,left,mid);
Build(rt*+,mid+,right);
sp_update(rt);
return;
}
void update(int rt,int l,int r,int x){//更新
if(l==r && r==x){
t[rt].sp=;
return;
}
int mid=(l+r)/;
if(x<=mid)update(rt*,l,mid,x);
else update(rt*+,mid+,r,x);
sp_update(rt);
}
int query(int rt,int l,int r,int x){//查询
if(l==r)return l;
int mid=(l+r)/;
int ans;
if(x<=t[rt*].sp){
ans=query(rt*,l,mid,x);
}
else ans=query(rt*+,mid+,r,x-t[rt*].sp);
return ans;
}
int main(){
int i,j;
while(scanf("%d",&n)!=EOF){
Build(,,n);
for(i=;i<=n;i++){
scanf("%d%d",&id[i],&val[i]);
}
for(i=n;i>=;i--){
int pos=query(,,n,id[i]+);//查询插入位置
res[pos]=val[i];
update(,,n,pos);
}
for(i=;i<=n;i++)printf("%d ",res[i]);
printf("\n");
}
return ;
}
POJ 2828 Buy Tickets的更多相关文章
- 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
		题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ... 
- 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 Description Railway tickets were difficult to buy around the Lunar New Year in China, so ... 
- poj 2828 Buy Tickets (线段树 单节点 查询位置更新)
		Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15533 Accepted: 7759 Desc ... 
- POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
		题目链接: 传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Description Railway tickets were d ... 
- poj 2828 Buy Tickets【线段树单点更新】【逆序输入】
		Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16273 Accepted: 8098 Desc ... 
- 线段树(倒序操作):POJ 2828 Buy Tickets
		Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, ... 
随机推荐
- 转: 关于Linux与JVM的内存关系分析
			转自: http://tech.meituan.com/linux-jvm-memory.html Linux与JVM的内存关系分析 葛吒2014-08-29 10:00 引言 在一些物理内存为8g的 ... 
- 序列化在Netty中的使用
			Java序列化的缺点 1.无法跨语言 对于Java序列化后的字节数组,别的语言无法进行反序列化 2.序列化后的码流过大 3.序列化性能低 使用JDK自带的序列化进行对象的传输 被传输的,实现了序列化接 ... 
- Quartz Cron 触发器 Cron Expression 的格式
			转自:http://blog.csdn.net/yefengmeander/article/details/5985064 上一文中提到 Cron触发器可以接受一个表达式来指定执行JOB,下面看看这个 ... 
- Delphi7 安装ICS,与简单使用
			官网 http://www.overbyte.be/ 下载 OverbyteIcsV816 完成后解压到E:\Delphi7\OverbyteIcsV816\ 1.在library里加入E:\Delp ... 
- Linux Linux程序练习九
			题目:利用多线程与有名管道技术,实现两个进程之间发送即时消息,实现聊天功能 思路:关键在于建立两个有名管道,利用多线程技术,进程A中线程1向管道A写数据,进程B中线程2从管道A读数据,进程A线程2从管 ... 
- U3D5.3.5f Monodevelop 仅支持到.NET 3.5
			2016年12月2号:发现这个标题是错误的,可以在monodevelop中选择.NET的版本,如下:打开solution,右击 Assembly-CSharp,options, build, gene ... 
- php基础03:数据类型
			<?php // day01:数据类型 //01.字符串 $x = "hello world"; echo $x; echo "<br>"; ... 
- HTTP基础(一):如何使用浏览器network查看请求和响应的信息
			一. 问题描述 HTTP作为前端开发与后开发链接的载体,其重要性不言而喻,今天我不复习关于HTTP自身的一些知识,只复习如何解读浏览器自带的的抓包工具(查看请求信息与响应信息)network. 二. ... 
- ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法
			今天在使用多字段去重时,由于某些字段有多种可能性,只需根据部分字段进行去重,在网上看到了rownumber() over(partition by col1 order by col2)去重的方法,很 ... 
- JS 之匿名函数
			匿名函数以及闭包 匿名函数就是没有名字的函数.闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见方式是在一个函数的内部创建另一个函数.闭包会携带包含它的函数的作用域,因此会比其他函数占用更 ... 
