hdu3577 Fast Arrangement

来源:互联网 发布:全民淘宝客下载 编辑:程序博客网 时间:2024/04/29 14:26

题目:

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个乘客,并且每个乘客仅仅只能从a->b买一张票,在任何时间每辆火车都不能载更多的乘客。一个人提前买的票将是有效的

思路:

也就是一个区间问题,某个乘客买a->b的票,需要询问a->b之间已有的最大乘客数量是否大于或等于k.如果不是,该乘客可以上车并且将a->b这个区间的所有乘客数+1.用线段树做,区间查询最大值,区间更新,注意乘客的实际乘车区间为[a,b);并且此题输出格式有毒,具体看代码。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define ls (rt<<1)#define rs (rt<<1|1)#define lson L,mid,ls#define rson mid+1,R,rs#define def_mid int mid=(L+R)>>1#define ll long long#define INF 0x3f3f3f3fusing namespace std;const int maxn = 1111111;int lazy[maxn<<2],maxs[maxn<<2];void pushup(int rt){    maxs[rt]=max(maxs[ls],maxs[rs]);}void pushlazy(int lz,int len,int rt){    maxs[rt]+=lz;    lazy[rt]+=lz;}void pushdown(int L,int R,int rt){    if(lazy[rt]!=0)    {        def_mid;        pushlazy(lazy[rt],mid-L+1,ls);        pushlazy(lazy[rt],R-mid,rs);        lazy[rt]=0;    }    return;}void build(int L,int R,int rt){    lazy[rt]=0;    if(L==R)    {        maxs[rt]=0;        return;    }    def_mid;    build(lson);    build(rson);    pushup(rt);}void update(int l,int r,int L,int R,int rt,int k){    if(l<=L&&r>=R)    {        maxs[rt]+=k;        lazy[rt]+=k;        return;    }    pushdown(L,R,rt);    def_mid;    if(l<=mid)        update(l,r,lson,k);    if(r>mid)        update(l,r,rson,k);    pushup(rt);}ll querymax(int l,int r,int L,int R,int rt){    if(l<=L&&r>=R)        return maxs[rt];    pushdown(L,R,rt);    def_mid;    ll ret=0;    if(l<=mid)        ret=max(ret,querymax(l,r,lson));    if(r>mid)        ret=max(ret,querymax(l,r,rson));    return ret;}int main(){    int q,k,T;    int ans[123456];    int top;    int no=0;    scanf("%d",&T);    while(T--)    {        while(scanf("%d%d",&k,&q)!=EOF)        {            memset(lazy,0,sizeof(lazy));            memset(maxs,0,sizeof(maxs));            int l,r;            build(1,maxn,1);            top=0;            for(int i=1;i<=q;i++)            {                scanf("%d%d",&l,&r);                r--;                if(querymax(l,r,1,maxn,1)<k)                {                    ans[top++]=i;                    update(l,r,1,maxn,1,1);                }            }            printf("Case %d:\n",++no);            for(int i=0;i<top;i++)                printf("%d ",ans[i]);            printf("\n\n");        }    }    return 0;}