(HDU

来源:互联网 发布:java破解单机游戏大全 编辑:程序博客网 时间:2024/06/05 22:37

(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|1using 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;}