HDU 3577 线段树

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

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名乘客同时在车上,然后给出n名乘客的上车时间和下车时间, 若第i名乘客可以上车(买到票), 则输出该名乘客的编号。

思路: 线段树, 节点保存的信息是该区间内火车上的人数。 注意因为乘客在a时刻上车, b时刻下车,因此实际上有用的区间是[a,b-1]。涉及到区间修改,需要lazy标记,可以看一下下面博客的介绍。

线段树的基本知识:https://blog.csdn.net/xiji333/article/details/87973714

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

const int maxn=1000005;

struct node
{
	int l,r;
	int cnt;    //记录该区间内的人口数量
	int lazy;	//lazy标记
};

node tree[maxn<<2];

void build(int i,int l,int r)
{
    tree[i].l=l;
    tree[i].r=r;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
}

void down(int i)
{
    tree[i<<1].lazy+=tree[i].lazy;
    tree[i<<1|1].lazy+=tree[i].lazy;
    tree[i<<1].cnt+=tree[i].lazy;
    tree[i<<1|1].cnt+=tree[i].lazy;
    tree[i].lazy=0;
}

void update(int i,int l,int r)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        ++tree[i].lazy;
        ++tree[i].cnt;
        return ;
    }
    if(tree[i].lazy)
        down(i);
    int mid=(tree[i].l+tree[i].r)>>1;
    if(r<=mid)
        update(i<<1,l,r);
    else if(l>mid)
        update(i<<1|1,l,r);
    else
    {
        update(i<<1,l,mid);
        update(i<<1|1,mid+1,r);
    }
    tree[i].cnt=max(tree[i<<1].cnt,tree[i<<1|1].cnt);//更新记录最多的人口数量
}

int query(int i,int l,int r)
{
    if(tree[i].l==l&&tree[i].r==r)
        return tree[i].cnt;
    if(tree[i].lazy)
        down(i);
    int mid=(tree[i].l+tree[i].r)>>1;
    if(r<=mid)
        return query(i<<1,l,r);
    else if(l>mid)
        return query(i<<1|1,l,r);
    else
        return max(query(i<<1,l,mid),query(i<<1|1,mid+1,r));
}

int main()
{
    int t;
    scanf("%d",&t);
    int times=0;
    while(t--)
    {
        memset(tree,0,sizeof(tree));
        printf("Case %d:\n",++times);
        int k,n;
        scanf("%d%d",&k,&n);
        build(1,1,1000000);
        int t1,t2;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&t1,&t2);
            if(query(1,t1,t2-1)<k)//当前区间人数小于k
            {
                printf("%d ",i+1);
                update(1,t1,t2-1);
            }
        }
        printf("\n\n");
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值