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

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

Source

实现代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r)>>1
const int M = 2e5+;
ll sum[M<<],a[M<<],b[M<<],md,ans[M<<]; void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = ;
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int c,int l,int r,int rt){
if(l == r){
ans[l] = c;
sum[rt]--;
return ;
}
mid;
if(sum[rt<<] >= p) update(p,c,lson);
else update(p-sum[rt<<],c,rson);
pushup(rt);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF){
memset(ans,,sizeof(ans));
build(,n,);
for(int i = ;i < n;i ++){
scanf("%d%d",&a[i],&b[i]);
}
for(int i = n-;i >= ;i --){
update(a[i]+,b[i],,n,);
}
for(int i = ;i < n;i ++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return ;
}

poj2828 Buy Tickets (线段树 插队问题)的更多相关文章

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

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

  2. [poj2828] Buy Tickets (线段树)

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

  3. poj-----(2828)Buy Tickets(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

  4. [POJ2828]Buy Tickets(线段树,单点更新,二分,逆序)

    题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位 ...

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

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

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

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

  7. Buy Tickets(线段树)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16607   Accepted: 8275 Desc ...

  8. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

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

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

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

随机推荐

  1. 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记

    AOP = Aspect Oriental Programing  面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...

  2. scala学习——(1)scala基础(上)

    scala> val x = 1 x: Int = 1 一.值与变量 值(val):赋值后不可变 val值名称:类型 = XXX 变量(var):赋值后可以改变  var变量名称:类型 = XX ...

  3. Codeforces 955C Sad powers (数论)

    题目链接:Sad powers 题意:给出n个l和r,求出每个给出的[l,r]之间的可以使是另外一个数的k次方的数.(k>=2) 题解:题目给出的数据范围最大是1E18所以如果要直接把所有的从1 ...

  4. Django实现websocket完成实时通讯、聊天室、在线客服等

    一 什么是Websocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebS ...

  5. 【分享】熟练的Java程序员应该掌握哪些技术?

    Java程序员应该掌握哪些能力才能算是脱离菜鸟达到熟练的程度? 1.语法:Java程序员必须比较熟悉语法,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息 知道是什么样的语法错误并且知道任 ...

  6. [转载]windows下PHP + Nginx curl访问本地地址超时卡死问题的解决方案

    原因: windows 下 nginx+php环境,不支持并发. 解决方案: 1.在配置nginx vhost时,需要同时运行的网站设置不同的fastcgi_pass的端口号 server { ser ...

  7. allegro 基本步骤

    PCB 1.建立电路板 首先是打开PCB编辑器——开始--所有程序-- Allegro SPB 15.5--PCB Editor,在弹出的对话框中选择Allegro PCB Design 610(PC ...

  8. Keyshot+AD渲染PCB效果图

    Keyshot+AD渲染PCB效果图 1.前言 前些天,公司同事找到我说,公司的展会宣传册要更新的了,有几款新的产品需要更新添加上去,大部分的新产品都有实物demo,可以拍照修一下图弄上去.但不巧,其 ...

  9. 简言MVC

    什么是MVC? MVC是一种代码的组织结构,在一个工程项目中,将代码的数据处理,逻辑单元和交互部分分离开来达到一种低耦合的效果,便于工程的修改.MVC中M代表Model,V代表View,C代表Cont ...

  10. Linux内核分析-两种方式使用同一个系统调用

    实验部分 根据系统调用表,选取一个系统调用.我选得是mkdir这个系统调用,其系统调用号为39,即0x27 由于mkdir函数的原型为 int mkdir (const char *filename, ...