博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[poj2828] Buy Tickets (线段树)
阅读量:4554 次
发布时间:2019-06-08

本文共 3394 字,大约阅读时间需要 11 分钟。

线段树


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.

此处输入图片的描述


题目大意

有 n 个人排队买票,他们依次到来,第 i 个人来的时候会站在第pos[i]个人后面,并且他的编号为val[i]。

求最后的队列中每个位置人的编号。

题解

这题和poj2182可以说是一模一样。也是开一个线段树,维护当前区间中有几个空位,然后从后往前更新,在第几个空位插入这个人,并把该空位删除。可以自己手动模拟一下,具体可以参考。

代码

#include 
using namespace std;#define pushup(u) {sum[u] = sum[u<<1] + sum[u<<1|1];}#define ls u<<1,l,mid#define rs u<<1|1,mid+1,rconst int maxn = 2e5 + 5;int sum[maxn << 2];int ans[maxn];int pos[maxn], id[maxn];void build(int u,int l,int r) { sum[u] = r - l + 1; if(l == r)return; int mid = (l + r) >> 1; build(ls); build(rs);}void update(int u,int l,int r,int x,int a) { if(l == r){ sum[u] = 0; ans[l] = a; return; } int mid = (l + r) >> 1; if(sum[u << 1] >= x)update(ls,x,a); else update(rs,x - sum[u<<1],a); pushup(u);}int main() { ios::sync_with_stdio(false); cin.tie(0); int n; while(cin >> n) { build(1,1,n); for(int i = 1;i <= n;i++) { cin >> pos[i] >> id[i]; } for(int i = n;i > 0;i--) { update(1,1,n,pos[i] + 1,id[i]); } for(int i = 1;i <= n;i++) { cout << ans[i]; i == n ? (cout << endl) : (cout << ' '); } } return 0;}

转载于:https://www.cnblogs.com/ZegWe/p/5990245.html

你可能感兴趣的文章
aws cli command line interface的安装与使用
查看>>
10)将地址换成常量
查看>>
cocos2d-x3.0 解释具体的新的物理引擎setCategoryBitmask()、setContactTestBitmask()、setCollisionBitmask()...
查看>>
Cocos2d-x
查看>>
FIR滤波器设计
查看>>
1005 继续(3n+1)猜想 (25 分)
查看>>
【Uva 1252】Twenty Questions
查看>>
1_访问命令行
查看>>
File操作相关
查看>>
Linux:文本处理工具
查看>>
java,for穷举,经典题目,百鸡百钱
查看>>
mysql提示Column count doesn't match value count at row 1错误
查看>>
前端--jstree--异步加载数据
查看>>
CSS定位深入理解 完全掌握CSS定位 相对定位和绝对定位
查看>>
网络体系结构
查看>>
练习4.13、4.14、4.15、4.16
查看>>
SAP库龄表
查看>>
PhantomJS 基础及示例 (转)
查看>>
20175316盛茂淞 2018-2019-2 《Java程序设计》第3周学习总结
查看>>
zookeeper安装
查看>>