POJ 2828 Buy Tickets - Segment Tree

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 17030 Accepted: 8439

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 nextN lines contain the pairs of values Posi and Vali in the increasing order ofi (1 ≤ iN). For each i, the ranges and meanings ofPosi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind thePosi-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 valueVali.

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
刚学习线段树,有推荐说这道题很典型就做了,结果WA了... 最后一看居然是pos没初始化加一... 动手能力还有待加强啊...

这题如果拿衣服地按照Hint做,也就是顺序读取一次次插队,复杂度是o(n^2)的,会TLE,所以改用线段树。

给定N建立好保存区间[1,N]的树,它的节点存储的值除了左右界还有这个区间内尚能装下的人数n,对于叶子n初始为1,对于区间n初始为其长度。

由于后插队的优先级高,因此逆序读取,对每一个pos,插入的标准是从根节点开始先判断其左孩子的容量n是否大于等于pos,如果是说明装得下,往靠前的左孩子里装

如果不是就往右孩子里装,这时候pos要减去左孩子的n,换算成去掉左孩子中已经装好的点的队列中的相对位置(这里我一开始没想明白);装好后把n减1;

那个比较tricky的地方:由于队列已经有左孩子(设为n')个点被占用,这时候在右孩子内部的pos'加上n'才等于原来的pos;

如果到了叶子就把叶子的n清零,把val装在叶子对应ans的位置上,停止递归

最后顺序输出ans. 总的复杂度相当于N次线段树查找,为O(NlogN)

#include <cstdio>
#include <iostream>
using namespace std;
const int MAXN = 2e5 + 1;
int N;
int ans[MAXN];

struct person
{
    int pos, val;
}queue[MAXN];

struct node
{
    int l, r, n;
}tree[MAXN<<2];

void build(int i,int l,int r)
{
    tree[i].l = l;
    tree[i].r = r;
    if (tree[i].l == tree[i].r)
    {
        tree[i].n = 1;
        return;
    }
    build(2 * i, l, (l + r) / 2);
    build(2 * i + 1,(l + r) / 2 + 1,r);
    tree[i].n = tree[2 * i].n + tree[2 * i + 1].n;
}

void insert(int i,int pos, int val)
{
    if (tree[i].l == tree[i].r)
    {
        tree[i].n--;
        ans[tree[i].l] = val;
    }
    else
    {
        if (pos<=tree[2*i].n)
            insert(2 * i, pos, val);
        else
            insert(2 * i + 1, pos-tree[2*i].n, val);
        tree[i].n--;
    }
}

int main()
{
    while (~scanf("%d", &N))
    {
        build(1,1,N);
        for (int i = 1; i <= N; i++)
            scanf("%d %d",&(queue[i].pos),&(queue[i].val));
        for (int i = 1; i <= N; i++)
            queue[i].pos++;
        for (int i = N; i >= 1; i--)
            insert(1,queue[i].pos,queue[i].val);

        for (int i = 1; i <= N; i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值