1. 程式人生 > >hdu 2828 Buy Tickets

hdu 2828 Buy Tickets

front end find lock lun ext inpu ttl rate

Buy Tickets

Time Limit : 8000/4000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 15
Problem 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 <p>There will be several test cases in the input. Each test case consists of <i>N</i> + 1 lines where <i>N</i> (1 ≤ <i>N</i> ≤ 200,000) is given in the first line of the test case. The next <i>N</i> lines contain the pairs of values <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> in the increasing order of <i>i</i> (1 ≤ <i>i</i> ≤ <i>N</i>). For each <i>i</i>, the ranges and meanings of <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> are as follows:</p><ul><li><i>Pos<sub>i</sub></i> ∈ [0, <i>i</i> ? 1] — The <i>i</i>-th person came to the queue and stood right behind the <i>Pos<sub>i</sub></i>-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.</li><li><i>Val<sub>i</sub></i> ∈ [0, 32767] — The <i>i</i>-th person was assigned the value <i>Val<sub>i</sub></i>.</li></ul><p>There no blank lines between test cases. Proceed to the end of input.</p>

Output <p>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.</p>

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

Source PKU 還是線段樹的新手,做完才恍然線段樹只是可供利用的一種數據結構,具體怎麽用要發揮自己的想象力。。 一開始還在想用lowbit()之類的常用函數。 這道題目的中心在,逆序插入的最後一人位置不變。在結點用ksum表示空位數量。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include<vector>
#include<cstring>
using namespace std;
int a[200005],b[200005],ans[200005];
int tem,t;
struct node
{
    int l,r,ksum;
}tr[200005<<2];
void build(int n,int l,int r)
{
    tr[n].l=l;
    tr[n].r=r;
    tr[n].ksum=r-l+1;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(n<<1,l,m);
    build(n<<1|1,m+1,r);
}
void f(int k,int v,int n)
{
    int m=(tr[n].l+tr[n].r)>>1;
    if(tr[n].l==tr[n].r)
    {
        tr[n].ksum=0;
        ans[tr[n].l]=v;
        return;
    }
    if(tr[n<<1].ksum>=k)
        f(k,v,n<<1);
    else
        f(k-tr[n<<1].ksum,v,n<<1|1);
    tr[n].ksum=tr[n<<1].ksum+tr[n<<1|1].ksum;
}


int main()
{

    while(~scanf("%d",&t))
    {
        build(1,1,t);
        tem=0;
        for(int i=1;i<=t;i++)
            scanf("%d%d",&a[i],&b[i]);
        for(int i=t;i>0;i--)
            f(a[i]+1,b[i],1);
        for(int i=1;i<t;i++)
            cout<<ans[i]<<" ";
            cout<<ans[t]<<endl;
    }
}

  

hdu 2828 Buy Tickets