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. DuiLib中FlashDemo的例子经验杂粹1

    转载:https://www.jianshu.com/p/3e958ae9e5ab 最近用duilib做个东西,经常卡壳 ,而且以前学的现在又忘.现在觉得应该好好做笔记,以前老是觉得博客是很郑重的东西 ...

  2. java 实现用户自由选择字段实现导出EXCEL表格

    package com.thinkgem.jeesite.common.utils.excel; import java.io.File; import java.io.OutputStream; i ...

  3. 基于Qt 5.9.8,编译Qtxlsx

    1.源码下载地址:https://github.com/dbzhang800/QtXlsxWriter 2.下载并安装编译时需要的工具 Perl:https://www.perl.org/get.ht ...

  4. eclipse问题集合

    [eclipse更换jdk版本]

  5. JAVA--文件内容属性替换

    说明:文件中执行内容是变量,随着环境不同会配置不同,在程序启动后,读取配置进行变量替换 1.测试类如下: public class FileUtilsTest { //public static bo ...

  6. 141、Java内部类之实例化外部类对象

    01. 代码如下: package TIANPAN; class Outer { // 外部类 private static String msg = "Hello World !" ...

  7. windows下hashcat利用GPU显卡性能破解密码

    由于一般密码破解工具的破解速度实在是太慢,而且支持的密码破解协议也不多,暴力破解的话,有的密码1年时间也破不出来,用字典跑的话必须要明文密码在字典里才行,而且密码字典太大的话,也很浪费时间,跑不出来也 ...

  8. css 图形样式

    参考:https://css-tricks.com/examples/ShapesOfCSS/

  9. Angular4之时间管道

    {{时间戳 |date:“yyyy/MM/dd HH:mm:ss”}} “YYYY/MM/DD”不可

  10. ch5 创建类似按钮的链接

    锚是行内元素,所以只有在单击链接的内容时它们才会激活,如果可以实现为按钮的效果,就可以有更大的可单击区域,实现方法为:display设置为block,修改width.height和其他属性,代码如下: ...