Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 17077   Accepted: 8466

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

线段树处理。注意 0<=pos[i]<=i-1

#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Acmer{
int pos,val;
}acmer[MAXN];
struct node{
int l,r;
int sum;
}a[MAXN*]; int que[MAXN];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
a[rt].sum=;
return;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
}
void update(int rt,int pos,int val)
{
if(a[rt].r==a[rt].l)
{
que[a[rt].l]=val;
a[rt].sum--;
return ;
}
//pos-1 比其先来插队且位置在其前方的人数
if(pos<=a[rt<<].sum) update(rt<<,pos,val);//后来的排在前面(左面)
else update((rt<<)|,pos-a[rt<<].sum,val);//没有位置,留下空位置给排在前面的,自己向后排
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
{
scanf("%d%d",&acmer[i].pos,&acmer[i].val);
}
build(,,n);
for(int i=n-;i>=;i--)//最后一个位置是确定的,前一个根据后一个位置调整。
{
update(,acmer[i].pos+,acmer[i].val);
}
for(int i=;i<=n-;i++)
{
printf("%d ",que[i]);
}
printf("%d\n",que[n]);
}
return ;
}

POJ2828(插队问题)的更多相关文章

  1. 【poj2828】Buy Tickets 线段树 插队问题

    [poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...

  2. POJ2828 Buy Tickets(线段树之插队问题)

    飞翔 问题是这样的:现在有n个人要买票,但是天黑可以随便插队.依次给出将要买票的n个人的数据信息.包含两项:pos,当前第i号人来了之后他肯定要插入到pos这个位置,如果当前pos无人,那最好了,直接 ...

  3. 【插队问题-线段树-思维巧妙】【poj2828】Buy Tickets

    可耻的看了题解 巧妙的思维 逆序插入,pos 代表的意义为前面要有pos个空格才OK: 证明:仔细思考一下就觉得是正确的,但是要想到这种方式还是要很聪明,空格是前面的几个数字所形成的,所以要特地留出来 ...

  4. poj2828 Buy Tickets (线段树 插队问题)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22097   Accepted: 10834 Des ...

  5. POJ-2828 Buy Tickets---线段树+逆序

    题目链接: https://cn.vjudge.net/problem/POJ-2828 题目大意: 插队的问题,每个案例给出n,代表有n个插队的,每个给出p,v,意思是代号为v的人插在了第p个人的后 ...

  6. [POJ2828] Buy Tickets(待续)

    [POJ2828] Buy Tickets(待续) 题目大意:多组测试,每组给出\(n\)条信息\((a,b)\),表示\(b\)前面有\(a\)个人,顺序靠后的信息优先级高 Solution.1 由 ...

  7. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  8. 【POJ2828】Buy Tickets(线段树)

    题意:有一个输入序列,每次操作要把b[i]插入到第a[i]个,在第a[i]个后面的要后移,问最后序列. n<=200000 思路:顺序来只能用splay维护 考虑倒序,对于插入到第K个位置,在线 ...

  9. pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)

    这题开始的思路就是模拟:就像数组中插点一样,每一个想买票的人都想往前插队! 但是这样的话肯定TLE, 看了别人的思路之后才恍然大悟! 正解: 将开始的正序插入,变成倒序插入,这样的话,想一想:第 i ...

随机推荐

  1. 链接脚本使用一例2---将二进制文件 如图片、MP3音乐、词典一类的东西作为目标文件中的一个段

    参考文章: <程序员的自我修养——链接.转载与库> P68 这里介绍两种方法,实现将将一张图片作为二进制可执行程序的一个段,其中第一种方法在我之前的博客中已经有所介绍,不过,那是采用的是交 ...

  2. 【虚拟机】WIN8.1系统虚拟机完全彻底删除

    一.首先删除注册表 用管理员身份打开CMD,输入F:\>VMware-workstation-full-12.5.5-5234757.exe/clean,根据自己的虚拟机安装文件的路径进行改变( ...

  3. Docker入门系列3:使用

    入门 首先强烈建议玩一遍官方的入门教程,Interactive commandline tutorial,下面是答案: 查看版本:docker version 搜索Image:docker searc ...

  4. ANDROID 推送到底哪家强(转)

    之前在群里有同学问我关于推送的一些问题,解答之后我觉得这个话题还挺有用,因为几乎大部分人都会遇到这个问题,那姑且就写篇文章总结给你们吧. 1. 为什么要用推送? 推送功能可谓是现如今任何一个 App ...

  5. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  6. activemq 搭建--集群

      linux activmemq 集群安装,配置和高可用测试       从 ActiveMQ 5.9 开始,ActiveMQ 的集群实现方式取消了传统的Master-Slave 方式,增加了基于Z ...

  7. SSIS

    http://www.cnblogs.com/codefish/category/557802.html

  8. Html控件和Web控件(转)

    作为一名ASP.NET的初学者,了解并且区别一些混淆概念是很必须的,今天这篇博文 就是主要向大家介绍一下Html控件和Web控件.在ASP.net中,用户界面控件主要就是 Html控件和Web控件,在 ...

  9. opencv使用记录

    /*2017-1-14*/ /*视频的读取...*/ int g_n=0; void on_change(int pos,void *)//看来void*不能省! { printf("g_n ...

  10. 【24题】P2766最长不下降子序列问题

    网络流二十四题 网络流是个好东西,希望我也会. 网络流?\(orz\ zsy!!!!!\) P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(d ...