hdu3577 Fast Arrangement

題目:

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 0x3f3f3f3f
using 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;
}
發佈了90 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章