hdu 3577 Fast Arrangement(区间更新)

针对中国铁路旅客众多的问题,本文介绍了一种使用线段树算法实现的高效火车票查询系统设计方案。该系统确保每趟列车不超过最大乘客容量,并遵循先到先得的原则分配座位。

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

Fast Arrangement

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


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个人 有q个人去买票 买从a到b的票  问这个人能不能买到票

线段树的想法 用每个叶子节点模拟每一个车站中火车上人数的情况

注意的是买从a到b的票 该人是在a到b-1站中占着火车中的一个人数

在更新之前要先查询 这个区间的火车还能不能载人 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1000010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}
int kk,q;

struct node
{
    int l,r;
    int num,lazy;
};
node no[MAXN*4];
void build(int l,int r,int k)
{
    if(l==r)
    {
        no[k].l=l;
        no[k].r=r;
        no[k].num=kk;
        no[k].lazy=0;
        return;
    }
    int mid=(l+r)/2;
    no[k].l=l;
    no[k].r=r;
    no[k].num=kk;
    no[k].lazy=0;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
}

void pushdown(int k)
{
    if(no[k].lazy)
    {
        no[k*2].lazy+=no[k].lazy;
        no[k*2+1].lazy+=no[k].lazy;
        no[k*2].num-=no[k].lazy;
        no[k*2+1].num-=no[k].lazy;
        no[k].lazy=0;
        return;
    }
}

void update(int l,int r,int k)
{
    if(l<=no[k].l&&r>=no[k].r)
    {
        no[k].lazy++;
        no[k].num--;
        return;
    }
    pushdown(k);
    int mid=(no[k].l+no[k].r)/2;
    if(r<=mid) update(l,r,k*2);
    else if(l>mid) update(l,r,k*2+1);
    else
    {
        update(l,mid,k*2);
        update(mid+1,r,k*2+1);
    }
    no[k].num=min(no[k*2].num,no[k*2+1].num);
}

int query(int l,int r,int k)
{
    if(no[k].l>=l&&no[k].r<=r)
    {
        return no[k].num;
    }
    pushdown(k);
    int mid=(no[k].l+no[k].r)/2;
    if(r<=mid) return query(l,r,k*2);
    else if(l>mid) return query(l,r,k*2+1);
    else
    {
        return min(query(l,mid,k*2),query(mid+1,r,k*2+1));
    }
}

int main()
{
//    fread;
    int cs=1;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        scanf("%d%d",&kk,&q);
        build(1,1000000,1);
        int flag=0;
        printf("Case %d:\n",cs++);
        for(int i=1;i<=q;i++)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            r--;
            if(query(l,r,1)>0)
            {
//                if(flag)
//                    printf(" ");
//                else flag=1;
                printf("%d ",i);
                update(l,r,1);
            }
        }
        printf("\n\n");
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值