线段树


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

代码

#include <iostream>
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,r const 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;
}

[poj2828] Buy Tickets (线段树)的更多相关文章

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

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

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

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

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

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

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

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

  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 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  7. Buy Tickets(线段树)

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

  8. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

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

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

随机推荐

  1. C#构造Http 破解学校教务系统学生账号密码

    背景介绍 我们学校的教务系统的是以学生学号作为登陆账号,初始密码是自己的生日. 一点点想法 每次期末查成绩的时候,我都会有一个想法,要是我能跑到系统后台,把自己的成绩修改一下,那该时间多么舒坦的事情啊 ...

  2. 实现服务器端与客户端的实时通信 SignalR(1)

    一.本文出处:SignalR 实例介绍 (建议看原著里面有DEMO下载) 二.这篇文章介绍如何利用 VS2012 创建一个简单的实时聊天系统,建好后的样子如下(模拟三个在线用户):    三.Demo ...

  3. jdk动态代理学习

    在jdk的好多底层代码中很多都使用jdk的动态代理,下面就写写简单的代码来look look. 老规矩先上代码: public interface SayDao { public String say ...

  4. angularJ表单验证

    常用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,只要在输入字段元素上添加HTML5标记required即可: <input type="text" requir ...

  5. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  6. Android 自定义RecyclerView 实现真正的Gallery效果

    http://blog.csdn.net/lmj623565791/article/details/38173061

  7. Fiddler- -Composer创建和发送HTTP Request

    Fiddler的功能还有很多, 很多功能都没有被挖掘出来.这次我们介绍Fiddler中的一个非常有用的功能Composer,是用来创建和发送HTTP Request的.Composer的使用方法很简单 ...

  8. NSArray倒序

    NSArray倒序 NSArray *tmparr = [[upLoadImageArr reverseObjectEnumerator] allObjects]; ios 正则表达式替换 . 不可变 ...

  9. C++ 中的类构造函数 & 析构函数

    类的构造函数 类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行. 构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void.构造函数可用于为某些成员变量设置 ...

  10. 导入charts开源库到工程里面

    http://blog.csdn.net/zww1984774346/article/details/50608338 http://blog.csdn.net/zww1984774346/artic ...