(HDU - 3577)Fast Arrangement

本文介绍了一个基于线段树的数据结构解决方案,用于处理铁路票务系统的座位分配问题。具体实现包括了线段树的构建、更新及查询操作,确保每位乘客能够按照先来先得的原则获得座位。

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

(HDU - 3577)Fast Arrangement

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3405 Accepted Submission(s): 978

Problem Description

Chinese always have the railway tickets problem because of its’ huge amount of passangers and stations. Now goverment need you to develop a new tickets query system.
One train can just take k passangers. And each passanger can just buy one ticket from station a to station b. Each train cannot take more passangers any time. The one who buy the ticket earlier which can be sold will always get the ticket.

Input

The input contains servel test cases. The first line is the case number. In each test case:
The first line contains just one number k( 1 ≤ k ≤ 1000 ) and Q( 1 ≤ Q ≤ 100000 )
The following lines, each line contains two integers a and b, ( 1 ≤ a < b ≤ 1000000 ), indicate a query.
Huge Input, scanf recommanded.

Output

For each test case, output three lines:
Output the case number in the first line.
If the ith query can be satisfied, output i. i starting from 1. output an blank-space after each number.
Output a blank line after each test case.

Sample Input

1
3 6
1 6
1 6
3 4
1 5
1 2
2 4

Sample Output

Case 1:
1 2 3 5

题目大意:一辆公交车任何时刻都只能容纳k个人,现在有p个人依次订票,每个人订一张从车站a到车站b的票,当然先订的人先买到票,问这p个人能否做上公交车。

思路:线段树维护区间覆盖问题。值得注意的是一个人在b下车,那么他乘车区间是[a,b-1],并且注意输出格式。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define ls o<<1
#define lr o<<1|1
using namespace std;

const int maxn=1000005;

struct node
{
    int l,r,v;
    int lazy;
}T[maxn<<2];

void pushup(int o)
{
    T[o].v=max(T[ls].v,T[lr].v);
}

void build(int o,int l,int r)
{
    T[o].l=l;
    T[o].r=r;
    T[o].lazy=0; 
    if(l==r) 
    {
        T[o].v=0;
        return; 
    }
    int mid=(l+r)>>1;
    build(ls,l,mid);
    build(lr,mid+1,r);
    pushup(o);
}

void pushdown(int o)
{
    T[ls].lazy+=T[o].lazy;
    T[lr].lazy+=T[o].lazy;
    T[ls].v+=T[o].lazy;
    T[lr].v+=T[o].lazy;
    T[o].lazy=0;
}

void update(int o,int l,int r)
{
    if(T[o].l==l&&T[o].r==r)
    {
        T[o].v+=1;
        T[o].lazy+=1;
        return;
    }
    if(T[o].lazy) pushdown(o);//因为没有找到完全重合的区间,所以先更新下一层
    int mid=(T[o].l+T[o].r)>>1;
    if(mid>=r) update(ls,l,r);
    else if(mid<l) update(lr,l,r);
    else 
    {
        update(ls,l,mid);
        update(lr,mid+1,r);
    } 
    pushup(o);//最后还得往上返回更新父亲结点 
}

int query(int o,int l,int r)
{
    if(T[o].l==l&&T[o].r==r) return T[o].v;
    if(T[o].lazy) pushdown(o);
    int mid=(T[o].l+T[o].r)>>1;
    if(mid>=r) return query(ls,l,r);
    else if(mid<l) return query(lr,l,r);
    else return max(query(ls,l,mid),query(lr,mid+1,r));
}

int main()
{
    int t,kase=0;
    scanf("%d",&t);
    while(t--)
    {
        int k,q;
        scanf("%d%d",&k,&q);
        build(1,1,1000000);
        printf("Case %d:\n",++kase);
        for(int i=1;i<=q;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            b--;//乘客从a上车,b下车,所以乘客的乘车区间是[a,b-1]
            if(query(1,a,b)<k)
            {
                printf("%d ",i);
                update(1,a,b);
            } 
        }
        printf("\n\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值