hdu 3577 Fast Arrangement(线段树)

Fast Arrangement

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


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
 

Author
Louty (Special Thanks Nick Gu)
 

Source
 

Recommend
zhouzeyong


题意:有一个1~1百万的乘坐火车的区间(这个该死的- -我一开始看错了范围,拼命RE),每个点最多只能容纳k个人,按顺序给出乘客搭乘区间,先到先得,若能上车则输出乘客的编号(由1开始一直下去)
题解:就是成段更新线段树,用lazy思想,询问的时候处理延迟的mark数组,还有,tree保存的是这个区间上的最大值,由于要立即更新最大值tree保证下次查询正确,增加的时候按条件往mark数组和tree数组加1,最后要tree[pos]=MA(tree[2*pos],tree[2*pos+1]); 这样确保了tree是最大值,而若询问时再更新tree[pos],则无法保证它之间的区间是否有值会比它大,所以tree[pos]要在增加时更新,mark仅延迟2*pos和2*pos+1和之后的更新

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000500
int tree[MAX*5],mark[MAX*5],k,q;
int MA(int a,int b)
{
    if(a>b) return a;
    return b;
}
int try_add(int l,int r,int pos,int templ,int tempr)
{
    int mid=(l+r)/2,f,g;

    if(templ<=l&&r<=tempr) return tree[pos];
    if(mark[pos]>0)
    {
        tree[2*pos]+=mark[pos];
        tree[2*pos+1]+=mark[pos];
        mark[2*pos]+=mark[pos];
        mark[2*pos+1]+=mark[pos];
        mark[pos]=0;
    }
    if(templ>mid) return try_add(mid+1,r,2*pos+1,templ,tempr);
    else if(tempr<=mid) return try_add(l,mid,2*pos,templ,tempr);
    else
    {
        f=try_add(mid+1,r,2*pos+1,mid+1,tempr);
        g=try_add(l,mid,2*pos,templ,mid);
        return MA(f,g);
    }
}
void add(int l,int r,int pos,int templ,int tempr)
{
    int mid=(l+r)/2;

    if(templ<=l&&r<=tempr)
    {
        mark[pos]++,tree[pos]++;
        return;
    }
    if(templ>mid) add(mid+1,r,2*pos+1,templ,tempr);
    else if(tempr<=mid) add(l,mid,2*pos,templ,tempr);
    else
    {
        add(mid+1,r,2*pos+1,mid+1,tempr);
        add(l,mid,2*pos,templ,mid);
    }
    tree[pos]=MA(tree[2*pos],tree[2*pos+1]);
}
int main()
{
    int cas,i,t,x,y,temp;

    scanf("%d",&cas);
    for(t=1;t<=cas;t++)
    {
        memset(tree,0,sizeof(tree));
        memset(mark,0,sizeof(mark));
        scanf("%d%d",&k,&q);
        printf("Case %d:\n",t);
        for(i=1;i<=q;i++)
        {
            scanf("%d%d",&x,&y);
            temp=try_add(1,1000000,1,x,y-1);
            if(temp<k)
            {
                add(1,1000000,1,x,y-1);
                printf("%d ",i);
            }
        }
        printf("\n\n");
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值