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.

用线段树存储某位置之前的空位。

数据倒着处理,先插入后来者到固定位置,然后再利用队列里的空位推算先来者的位置

 /**/
#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的更多相关文章

  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 ...

  10. 线段树(倒序操作):POJ 2828 Buy Tickets

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

随机推荐

  1. JMeter学习(五)检查点

    JMeter也有像LR中的检查点,本篇就来介绍下JMeter的检查点如何去实现. JMeter里面的检查点通过添加断言来完成. 检查点:上一章讲到,我们对用户名和密码进行了参数化,那么怎样来判断jme ...

  2. MySQL的重装问题解决方法

    最近在工作上遇到了在Windows环境中将MySQL重装的问题,今天记录一下我的解决过程. 首先因为某些原因,我不得不把mysql卸载,然后重装,不论我用控制面板的卸载删除程序方式还是安全卫士的卸载, ...

  3. EasyUI中页面必须刷新才显示tree组件最新数据的BUG解决方案

    在URL地址后面加个时间戳,这样就避免从浏览器缓存里读取数据了 $("#devtree").tree({ url: '/Deviceinfo/ModelsTree.aspx?cmd ...

  4. TIF、JPG图片手动添加地理坐标的方法(转载)

    题目:为TIF.JPG图片添加地理坐标/平面直角坐标. 图片来源:GOOGLE EARTH.(当然也可以是其他知道四角点坐标的图片) 截图工具:GEtscreen(此软件截图时可以自动生成图片四角点坐 ...

  5. [原创]Gerrit中文乱码问题解决方案分享

    应开发同事的要求,部署了Gitlab+Gerrit+Jenkins的持续集成环境. 但是发现了一个问题,Gerrit登陆后有中文乱码出现. 具体情况如下: (1)Git代码中的中文乱码处理: 为妥善解 ...

  6. NOI 1.7编程基础之字符串(35题)

    01:统计数字字符个数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一行字符,统计出其中数字字符的个数. 输入 一行字符串,总长度不超过255. 输出 ...

  7. Css 特殊或不常用属性

    1. -webkit-font-smoothing: antialiased; CSS3中用于webkit引擎(如chrome)中设置字体的抗锯齿或者说光滑度的属性.有3个属性:none用于小像素的文 ...

  8. mysql-5.7.14-winx64免安装版在win10下的详细配置过程

    1.配置文件 如果使用mysql的默认配置,在使用的过程中会出现很多问题,如汉字是乱码等. 在mysql的根目录(如:D:\mysql\mysql-5.7.14-winx64\)下,新建配置文件my. ...

  9. word2010 数学公式/联立方程/大括号内方程组如何左对齐?

    如何在word中输入的联立方程使其条件左对齐? 如输入: 实现如下对齐: 就是在每个逗号 .前输入一个 & 号就可以了, 注意这个逗号一定要是 位于这个方框里头,然后在其前面输入 & ...

  10. Java实验四 TCP客户端和服务器的应用

    实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全 4.对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送: 发送方A——————>接收方B A加密时,用B ...