Buy Tickets(线段树单点更新)

探讨了在购票场景下,如何通过逆序输入数据并利用线段树处理队列中人员插队的问题,最终输出队列中人员的正确顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Buy Tickets

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 21
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
 

Statistic |  Submit |  Back

题意:给一个整数n接下来n行每行两个数a,b表示b这个数字插在第a个数字后边,问最后形成的序列?

题解:此题建树的时候需要逆序输入数据,这样将后续的人员在对应位置向后插入即可,运用线段树来处理和操作

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200005
using namespace std;

int sum[maxn*4]; //注意此处定义是最大值的4倍
int ans[maxn];

void pushup(int pos)  //求左右子树和
{
    sum[pos]=sum[pos*2]+sum[pos*2+1];
}

void buildtree(int pos,int l,int r)  //建树
{
    if(l==r)
    {
        sum[pos]=1;//起始时刻,让所有的位置都站上人
        return ;
    }
    int mid=(l+r)/2;
    buildtree(pos*2,l,mid);
    buildtree(pos*2+1,mid+1,r);
    pushup(pos);//建成的树最底端每个位置都是第一个越往根部,人的编号越大,根处编号为n
} 

void update(int o,int l,int r,int pos,int v)
{
    if(l==r)
    {
        sum[o]=0;//当位于这个位置的时候
        ans[r]=v;     
        return ;//结束
    }
    int mid=(l+r)/2;
    if(pos<=sum[o*2])
        update(o*2,l,mid,pos,v);//搜索左子树时
    else
        update(o*2+1,mid+1,r,pos-sum[o*2],v);//搜索右子树因为右子树左端的值是左子树右端值+1所以查找右子树位置时查找到pos-左子树长度就可以了
    pushup(o);                               
}                          
int main()
{
    int n,m,j,i;
    int a[maxn],b[maxn];
    while(scanf("%d",&n)!=EOF)
    {
        buildtree(1,1,n);
        for(i=1;i<=n;i++)
            scanf("%d%d",&a[i],&b[i]);
        for(i=n;i>0;i--)   //注意是逆序
            update(1,1,n,a[i]+1,b[i]);//+1是因为题目数据是从0开始的 我们输数据的时候是从第一位而不是第0位输的  所以要+1,让位置对应
        for(i=1;i<=n;i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值