Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 26443   Accepted: 12639

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.

题意:买票的时候有n个人插队,输入每个人插队的位置pos和插队的时间val,如果两个人同一个位置,时间小的在前面,按从前到后输出队伍最后的排列顺序

题解:先初始化线段树,第i个位置前面有i个位置,从最后一个数据开始更新线段树,每处理完一个数据,空位置数减一,处理到最后即可

输入样例eg:

空  空  空  空

空  空  空  69

空  空  33  69

空  空  33  51  69//在33和69之间插入,没有占用位置

空  77  33  51  69

注:用cin的话,就算用加速一样会超时

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
using namespace std;
int n;
int tree[], pos[], val[], ans[];
void build(int num, int le, int ri)
{
tree[num] = ri - le + ;//初始化空格数量
if (le == ri)
{
return;
}
int mid = (le + ri) / ;//mid设为全局变量答案就错了
build(num * , le, mid);
build(num * + , mid + , ri);
} void update(int num, int le, int ri, int x, int y)
{
tree[num]--;//占用之后空格数减一;
if (le == ri)
{
ans[le] = y;
return;
}
int mid = (le + ri) / ;
if (x <= tree[num * ])
update(num * , le, mid, x, y);
else
update(num * + , mid + , ri, x - tree[num * ], y);
}
int main()
{
// ios::sync_with_stdio(false);
while (~scanf("%d",&n))
{
for (int i = ; i <= n; i++)
{
scanf("%d%d",&pos[i],&val[i]);
pos[i] = pos[i] + ;//初始化,i位置(包括i)前有几个空位置
}
build(, , n);
for (int i = n; i >= ; i--)//从最后一个人开始插队
{
update(, , n, pos[i], val[i]);
}
for (int i = ; i <= n; i++)
{
if (i == )
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
printf("\n");
}
return ;
}

POJ 2829 Buy Tickets的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. poj 2828 Buy Tickets 树状数组

    Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...

  7. poj 2828 Buy Tickets (线段树 单节点 查询位置更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15533   Accepted: 7759 Desc ...

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

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

  9. poj 2828 Buy Tickets【线段树单点更新】【逆序输入】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16273   Accepted: 8098 Desc ...

随机推荐

  1. [aac @ ...] Specified sample format s16 is invalid or not supported

    在使用FFmpeg打开编码器的时候出现以下错误: [aac @ 000001da19fd7200] Specified sample format s16 is invalid or not supp ...

  2. 五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)

    关联查询: 一个用户对应多个订单,一个订单只有一个用户 订单关联用户:两种方式 一:基于resultTYpe,一个与表关系一样的pojo实现 主表订单,从表用户 首先要有一个与关联查询表关系一样的po ...

  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:将悬停的颜色应用在行或者单元格上

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. QQ企业通---登录模块设计--知识点

    Rectangle 成员  存储一组整数,共四个,表示一个矩形的位置和大小.对于更高级的区域函数,请使用 Region 对象. Form与Control的区别? 窗体也是控件,继承Control类.可 ...

  5. css实现单行居中,两行居左

    居中需要用到 text-align:center,居左是默认值也就是text-align:left.要让两者结合起来需要多一个标签. <h2><p>单行居中,多行居左</ ...

  6. VMwara虚拟机三种网络模式

    虚拟机:虚拟机是能够让用户在一台物理机上模拟出多个操作系统的软件其本质是通过中间层实现计算机资源的管理和再分配让系统资源的利用率最大化VMware即是一款虚拟机软件注意:虚拟机和操作系统的区别,虚拟机 ...

  7. 插入和查询HBase速度都比较慢

    表层问题:插入和查询HBase速度比较慢 排查一,查看HBase节点状态,发现正常运行: 排查二,查看访问HBase服务的状态,发现服务停止: 依次点击服务实例,查看服务状态 133和135节点上的服 ...

  8. 小米xiaomi9 google play卡在检查信息

    终于在今年淘汰掉用了三年的果6s,换了小米9 ,结果发现科学..上网后,可以正常打开google网页,却无法登陆,gmail也无法使用.一直卡在“正在核对信息”. 检查服务,小米9 全系列自带了谷歌框 ...

  9. scikit_learn (sklearn)库中NearestNeighbors(最近邻)函数的各参数说明

    NearestNeighbors(n_neighbors=5, radius=1.0, algorithm='auto', leaf_size=30, metric='minkowski', p=2, ...

  10. Python 的 os 与 sys 模块

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...