码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2828 buy Tickets 用线段树模拟带插入的队列

时间:2015-04-21 14:30:19      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

Buy Tickets

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2795

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 ≤ iN). 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

 

题意

给你一个队列,有一群人来插队,然后让你输出最后队列的样子

题解:

感觉是个很傻B的题,但是为什么我非得用线段树来做呢?= =

vector亲测被T掉

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200050
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*
;
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
//**************************************************************************************
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(0);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}

struct node
{
    int l,r,v;
};

node a[maxn*4];

void build(int x,int l,int r)
{
    a[x].l=l,a[x].r=r;
    a[x].v=r-l+1;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
}

int update(int x,int val)
{
    int l=a[x].l,r=a[x].r;
    if(l==r)
    {
        a[x].v=0;
        return l;
    }
    else
    {
        int pos=0;
        if(a[x<<1].v>=val)
            pos=update(x<<1,val);
        else
            pos=update(x<<1|1,val-a[x<<1].v);
        a[x].v=a[x<<1].v+a[x<<1|1].v;
        return pos;
    }
}

int n;
int b[maxn];
int c[maxn];
int pos[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        build(1,1,n);
        for(int i=0;i<n;i++)
            b[i]=read(),c[i]=read();
        for(int i=n-1;i>=0;i--)
            pos[update(1,b[i]+1)]=c[i];
        for(int i=1;i<=n;i++)
            printf("%d ",pos[i]);
        puts("");
    }
}

 

poj 2828 buy Tickets 用线段树模拟带插入的队列

标签:

原文地址:http://www.cnblogs.com/qscqesze/p/4444103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!