poj2828--Buy Tickets(线段树+详解)

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 13618   Accepted: 6802

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

POJ Monthly--2006.05.28, Zhu, Zeyuan

题目大意,插队的问题,每个案例给出n,代表有n个插队的,每个给出p,v,意思是代号为v的人插在了第p个人的后面,问最后的队伍的排列?

题目中一开始整个队列是空的,也就是说如果输入i,j:代表代号为j的人插在了第i个人的后面,也就是说在他之前一定有了i个人,而他的位置是i+1。

所以由后向前推到,每个遇到的都是确定位置的,最后的人选定的位置不会改变,同样因为是倒叙输入,在第i个人后插队,也就是说他的前面一定要留下i个空格。

经过这样的转化后就可以用线段树解决,lazy中存了代号,cl数组中存了每个节点中包含的空格的数目。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 210000
#define lmin 1
#define rmax n
#define root lmin,rmax,1
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define now l,r,rt
#define int_now int l,int r,int rt

struct node
{
    int a , b ;
} p[maxn];
int cl[maxn<<2] , lazy[maxn<<2] ;
int n , i ;
void push_up(int_now)
{
    if( l != r )
        cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;
}
void creat(int_now)
{
    cl[rt] = lazy[rt] = 0 ;
    if( l != r )
    {
        creat(lson);
        creat(rson);
        push_up(now);
    }
    else
        cl[rt] = 1 ;
    return ;
}
void update(int a,int k,int_now)
{
    if( l == r && cl[rt] == 1 )
    {
        cl[rt] = 0 ;
        lazy[rt] = k ;
    }
    else
    {
        if( a <= cl[rt<<1] )
            update(a,k,lson);
        else
            update(a-cl[rt<<1],k,rson);
        push_up(now);
    }
    return ;
}
void pre(int_now)
{
    if( l == r )
    {
        if(n == 1)
            printf("%d\n", lazy[rt]);
        else
            printf("%d ", lazy[rt]);
        n-- ;
    }
    else
    {
        pre(lson);
        pre(rson);
    }
    return ;
}
int main()
{
    while( scanf("%d", &n)!=EOF )
    {
        for(i = 0 ; i < n ; i++)
        {
            scanf("%d %d", &p[i].a, &p[i].b);
            p[i].a++ ;
        }
        creat(root);
        for(i = n - 1 ; i >= 0 ; i--)
        {
            update(p[i].a,p[i].b,root);
        }
        pre(root);
    }
    return 0;
}
时间: 2024-08-05 10:59:51

poj2828--Buy Tickets(线段树+详解)的相关文章

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

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 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 wa

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

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 w

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

题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位置表明,在他之前一定有k个空位,于是将他插在第k+1个位置上.我们可以在线段树上直接二分,根据这个位置,假如这个位置在左子树上,那么一直向左走,否则要减掉左子树剩下的位置后再向右边走,同时更新沿途非叶节点的空闲位置数量(减一),手工画一下图就知道了. 1 /* 2 ━━━━━┒ギリギリ♂ eye!

POJ 2828 Buy Tickets (线段树)

题目大意: 排队有人插队,每一次都插到第 i 个人的后面. 最后输出顺序. 思路分析: 你会发现,如果反向处理的话,你就知道这个人是第几个了. 那么问题一下子就简化了. 就是在线段树上找第几个空位置就行了. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <vector> #define lson num<<1

POJ 2828 Buy Tickets 线段树解法

此题应用线段树的方法非常巧妙.没做过真的难想得出是这么想的. 是一个逆向思维的运用. 其实一看到这道题目我就想到要运用逆向思维的了,但是就是没那么容易想通的. 思路: 1 要从后面往前更新线段树 2 线段树记录的是当前区间还剩下多少个记录空间 3 因为后面的插入会使得前面的插入往后退,那么前面插入的下标如果大于前面可以插入的空间,就需要往后退了. 好难理解的操作.仔细观察一下下面update这个函数吧. 二分地去查找适合的插入位置,把插入操作时间效率提高到 O(lgn),如果使用一般方法插入操作

《ACM/ICPC 算法训练教程》读书笔记 之 数据结构(线段树详解)

依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不错的. 线段树简介 这是一种二叉搜索树,类似于区间树,是一种描述线段的树形数据结构,也是ACMer必学的一种数据结构,主要用于查询对一段数据的处理和存储查询,对时间度的优化也是较为明显的,优化后的时间复杂为O(logN).此外,线段树还可以拓展为点树,ZWK线段树等等,与此类似的还有树状数组等等. 例如:要将

线段树详解 (原理,实现与应用)

线段树详解 By 岩之痕 目录: 一:综述 二:原理 三:递归实现 四:非递归原理 五:非递归实现 六:线段树解题模型 七:扫描线 八:可持久化 (主席树) 九:练习题 一:综述 假设有编号从1到n的n个点,每个点都存了一些信息,用[L,R]表示下标从L到R的这些点. 线段树的用处就是,对编号连续的一些点进行修改或者统计操作,修改和统计的复杂度都是O(log2(n)). 线段树的原理,就是,将[1,n]分解成若干特定的子区间(数量不超过4*n),然后,将每个区间[L,R]都分解为 少量特定的子区

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

[poj2828]Buy Tickets 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

POJ 2828 Buy Tickets(线段树单点)

https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的数就往后推一位.最后输出每个位置的ans. 思路:根据题 目可知,最后插入的位置的数才是最终不变的数,所以可以从最后的输入作第1个放入,依此类推,倒插入.在插入时也有一定的技术,首先创建一棵空线段树时,每个节点记录当前范围内有多少个空位置.在插入时,要注意,一个数放入之后那么这个位置就不用管了,那么