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

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 ≤ iN). 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.



思路:线段树 倒序插入 更新节点时,左右子树要区分,如果左子树的空位大于等于现在要插入的位置,说明前面的空位没有占去。否则,进入右子树,而进入右子树时,空位变成了pos减去所有左子树的空位。即便左子树有空位但也不能插入,因为该数还没有插入,必须预留空位。



代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=200005;
int sum[maxn<<2],res[maxn<<2];
int data[maxn],location[maxn];
bool flag=false;
void pushup(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l, int r, int rt) {
sum[rt]=r-l+1;
if(l==r) return;
int mid=(l+r)>>1;
build(lson);
build(rson);
}
void update(int pos, int val, int l, int r, int rt) {
if(l==r) {
res[rt]=val;sum[rt]--;return;
}
int mid=(l+r)>>1;
if(pos<=sum[rt<<1]) update(pos,val,lson);
else update(pos-sum[rt<<1],val,rson);//要预留空位
pushup(rt);
}
void show(int l, int r, int rt) {
if(l==r) {
if(flag==false) printf("%d",res[rt]),flag=true;
else printf(" %d",res[rt]);
return;
}
int mid=(l+r)>>1;
show(lson);
show(rson);
}
int main() {
int n;
while(~scanf("%d",&n)) {
for(int i=0;i<n;++i) {
scanf("%d%d",&location[i],&data[i]);
}
build(1,n,1);
for(int i=n-1;i>=0;--i) {
update(location[i]+1,data[i],1,n,1);
}
flag=false;
show(1,n,1);
printf("\n");
}
return 0;
}

POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)的更多相关文章

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

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

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

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

  3. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  4. POJ 2828 Buy Tickets(线段树&#183;插队)

    题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性 ...

  5. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

  6. poj 2828 Buy Tickets (线段树)

    题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...

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

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  8. POJ 2828 Buy Tickets(单点更新) 详细题解和思路

    题意:给n个人插队,输出最后的队伍情况(题意写的有些粗糙) 思路:第一点:在最后的队伍中,我们唯一能确定的是最后一个人一定能排到指定位置.那么,倒数第二个是在最后一个基础上确定位置的,这样一层一层的倒 ...

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

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

随机推荐

  1. Android Studio常见问题解决

    1.Error:Execution failed for task ':XXXX:processDebugManifest'. > Manifest merger failed with mul ...

  2. Vue源码后记-更多options参数(2)

    写起来感觉都是老三套,AST => render => VNode => patch 之前是把AST弄完了,对事件和过滤器处理如图: render函数也只看这两部分的转换吧! 首先是 ...

  3. MQTT——连接报文

    学习MQTT协议.如果只是看了相关文档就认为可以了.那是一个错误的观念.笔者为了能更好的去理解MQTT协议.看了不少相关的开源Broker的项目.可惜这些项目一般都是不完全的.不过从这些项目中笔者至少 ...

  4. Linux分区规划与xshell使用排错

    1.1 没有重要数据 /boot   200M    存放系统的引导信息 内核 swap   交换分区   防止内存用光了 临时的一个内存 如果你的内存小于8G swap是内存的1.5倍   如果你的 ...

  5. 最最简单的CentOs6在线源搭建

    非常实用的在线源搭建,只要4步骤 1.点击进入http://mirrors.aliyun.com/repo/epel-6.repo ,这是阿里云的源 2.复制所有的代码  ctrl+a,ctrl+c ...

  6. AngularJS学习篇(十二)

    AngularJS SQL ASP.NET 中执行 SQL 获取数据 <!DOCTYPE html> <html> <head> <meta charset= ...

  7. Problem C: 线性表的基本操作

    Description 线性表是一类重要的且基础的数据结构.请定义MyList类,来模拟针对线性表的插入.删除等操作: 1. 数据成员int *elements:线性表元素. 2. 数据成员int l ...

  8. Java多线程其他

    1.interrupt()方法 interrupt方法不会真正中断线程,它只会清楚线程的wait,sleep,join的受阻状态,时线程重新获得CPU的执行权. 此时如果再次调用线程的wait,sle ...

  9. 【Kafka源码】KafkaController启动过程

    [TOC] 之前聊过了很多Kafka启动过程中的一些加载内容,也知道了broker可以分为很多的partition,每个partition内部也可以分为leader和follower,主从之间有数据的 ...

  10. 「JavaScript」同步、异步、回调执行顺序之经典闭包setTimeout分析

    聊聊同步.异步和回调 同步,异步,回调,我们傻傻分不清楚, 有一天,你找到公司刚来的程序员小T,跟他说:“我们要加个需求,你放下手里的事情优先支持,我会一直等你做完再离开”.小T微笑着答应了,眼角却滑 ...