poj 2828 Buy Tickets

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 13465 Accepted: 6708

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:

以下将会有几组测试数据。每组测试数据包括N+1行, N (1 ≤ N ≤ 200,000)在第一行,之后N行包括一对数Posi 和Vali 以ii(1 ≤ i ≤ N)递增的顺序。对于每个i,Posi 和 Vali 的范围和意义如下。

  • 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.
  • Posi ∈ [0, i − 1] ---第i个人跑过来,站在了Posi-th的右面。规定,第0个人且在队伍最前面的是队伍的第一个人。
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
  • 第i个人分配价值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 <stdio.h>
#include <string.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int maxn = 222222;
int sum[maxn << 2];
int p[maxn];

void build(int l, int r, int rt){
    int m;
    sum[rt] = r - l + 1;
    if (l == r){
         return ;
    }
    m = (l + r) >> 1;
    build(lson);
    build(rson);
    return;
}

int updata(int x, int l, int r, int rt){
    int m;
    int ret = 0;

    sum[rt] --;
    if (l == r)
        return l;

    m = (l + r) >> 1;
    if (sum[rt << 1] >= x){
        ret = updata(x, lson);
    }else{
        x -= sum[rt << 1];
        ret = updata(x, rson);
    }
    return ret;

}
int main(void){

    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int i, n, a[maxn], b[maxn];

    while(scanf("%d", &n) != EOF){
        build(0, n - 1, 1);
        for (i = 0;i < n;i ++){
            scanf("%d%d", &a[i], &b[i]);
        }
        for (i = n - 1;i >= 0;i --)
            p[updata(a[i] + 1, 0, n - 1, 1)] = b[i];

        for (i = 0;i < n;i ++){
            if (i == 0){
                printf("%d", p[i]);
            }else printf(" %d", p[i]);
        }
        printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值