poj 2828 Buy Tickets

来源:互联网 发布:xtream path 1.6 mac 编辑:程序博客网 时间:2024/05/24 05:49
Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal 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

40 771 511 332 6940 205231 192431 38900 31492

Sample Output

77 33 69 5131492 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

比较巧妙的线段树,使用倒叙插入这颗树,线段长度代表还有多少个空位可以插进去,之前做过,这次做又忘了怎么做了,可见学艺不精。。
#include <stdio.h>#include <string.h>#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1const 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