poj 2828 Buy Tickets

来源:互联网 发布:宏正软件 编辑:程序博客网 时间:2024/05/22 14:34

原题:
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.
这里写图片描述
中文:

排队问题,有人可以插队,问你最后买票人的顺序。

//#include <bits/stdc++.h>#include <iostream>#include <cstring>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int maxn=200000+10;int seg[maxn<<2];int ans[maxn],pos[maxn],val[maxn];void build(int l,int r,int rt){    int m=(l+r)>>1;    seg[rt]=r-l+1;    if(l==r)        return;    build(lson);    build(rson);}void update(int l,int r,int rt,int po,int i){    int m=(l+r)>>1;    seg[rt]--;    if(l==r)    {        ans[l]=val[i];        return;    }    if(seg[rt<<1]>=po)        update(lson,po,i);    else    {        po-=seg[rt<<1];        update(rson,po,i);    }}int main(){    ios::sync_with_stdio(false);    int n;    while(cin>>n)    {        build(1,n,1);        memset(ans,0,sizeof(ans));        for(int i=1;i<=n;i++)            cin>>pos[i]>>val[i];        for(int i=n;i>=1;i--)            update(1,n,1,pos[i]+1,i);        for(int i=1;i<=n;i++)        {            if(i!=n)                cout<<ans[i]<<" ";            else                cout<<ans[i]<<endl;        }    }    return 0;}

解答:

先是自己想了一会,想出了这么一个想法。
首先定义一个数组作为队伍位置的优先级,数越小,优先级越高,假如现在又x人已经排好队了,那么现在有一个人想要插入这个队伍的第i个位置,那么就把第i个人到后面的所有人的优先级加1,。由于,是在线段树练习题当中看到的这个题目,很明显的就想到了区间更新操作,正当兴奋的要写代码的时候突然意识到了,lazy操作当中的问题。考虑lazy操作的性质,每次相当于把更新的值存储在某一段区间上,在不进行查询的时候,这段区间就不会向下更新,然而,现在要找出所有叶子节点的更新值,就必须查询所有叶子节点。这时间复杂度就~呵呵了=_=
虽然没弄出来,但是总归是有点想法的,对区间查询有了更多的理解,算是收获吧

后来上网上查的题解http://www.cnblogs.com/CheeseZH/archive/2012/04/29/2476134.html
很详细,思维也非常巧妙!
用线段树存储每次当前区间剩余的空闲位置,比如有一个人抢占3号,那么前面就要留出两个位置,所以从后向前不断插入对应人员即可。详细见上面的博客。

0 0
原创粉丝点击