1. 程式人生 > >洛谷P2894 [USACO08FEB]酒店Hotel [線段樹]

洛谷P2894 [USACO08FEB]酒店Hotel [線段樹]

  題目傳送門

酒店

題目描述

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

參考樣例,第一行輸入n,m ,n代表有n個房間,編號為1---n,開始都為空房,m表示以下有m行操作,以下 每行先輸入一個數 i ,表示一種操作:

若i為1,表示查詢房間,再輸入一個數x,表示在1--n 房間中找到長度為x的連續空房,輸出連續x個房間中左端的房間號,儘量讓這個房間號最小,若找不到長度為x的連續空房,輸出0。

若i為2,表示退房,再輸入兩個數 x,y 代表 房間號 x---x+y-1 退房,即讓房間為空。

輸入輸出格式

輸入格式:

 

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

 

輸出格式:

 

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

 

輸入輸出樣例

輸入樣例#1: 
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
輸出樣例#1: 
1
4
7
0
5

 


 

  分析:

  可以說這一類題目就是檢驗自己線段樹是否真正理解透徹的測驗。

  對於這題,我們需要維護四個資訊,$sum$表示這一段最大的連續空房間數,$sl$表示這一段從左數的最大連續空房間數,$sr$表示這一段從右數的最大連續空房間數,$sign$表示這段區間是否被修改。

  在維護資訊時,$pushup$操作如下,詳細解釋在程式碼中:

 

inline void pushup(int rt)
{
    if( t[ls].sum==t[ls].len )
        t[rt].sl=t[ls].sum+t[rs].sl;//如果左子樹全為空的情況
    else
        t[rt].sl=t[ls].sl;
    if( t[rs].sum==t[rs].len )
        t[rt].sr=t[rs].sum+t[ls].sr;//右子樹全為空的情況
    else
        t[rt].sr=t[rs].sr;
    t[rt].sum=max(max(t[ls].sum,t[rs].sum),t[ls].sr+t[rs].sl);
    //求最大連續空房間數。
}

 

  $pushdown$操作如下:

inline void pushdown(int rt)
{
    if( t[rt].sign==0 ) return;
    if( t[rt].sign==1 ) {//如果sign為1,則房間已經被佔用了
        t[ls].sum=t[ls].sl=t[ls].sr=0;
        t[rs].sum=t[rs].sl=t[rs].sr=0;
        t[ls].sign=t[rs].sign=1;
    }
    if( t[rt].sign==-1 ) {//如果sign為-1,則房間要空出來
        t[ls].sum=t[ls].sl=t[ls].sr=t[ls].len;
        t[rs].sum=t[rs].sl=t[rs].sr=t[rs].len;
        t[ls].sign=t[rs].sign=-1;
    }
    t[rt].sign=0;
}

  再結合全部程式碼應該就很好理解了。

  Code:

 

//It is made by HolseLee on 5th Nov 2018
//Luogu.org P2894
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;

const int N=1e5+7;
int n,m;
struct Segment {
    int sum,sl,sr,len,sign;
}t[N<<2];

inline int read()
{
    char ch=getchar(); int x=0; bool flag=false;
    while( ch<'0' || ch>'9' ) {
        if( ch=='-' ) flag=true; ch=getchar();
    }
    while( ch>='0' && ch<='9' ) {
        x=x*10+ch-'0'; ch=getchar();
    }
    return flag ? -x : x;
}

inline void pushup(int rt)
{
    if( t[ls].sum==t[ls].len )
        t[rt].sl=t[ls].sum+t[rs].sl;
    else
        t[rt].sl=t[ls].sl;
    if( t[rs].sum==t[rs].len )
        t[rt].sr=t[rs].sum+t[ls].sr;
    else
        t[rt].sr=t[rs].sr;
    t[rt].sum=max(max(t[ls].sum,t[rs].sum),t[ls].sr+t[rs].sl);
}

inline void pushdown(int rt)
{
    if( t[rt].sign==0 ) return;
    if( t[rt].sign==1 ) {
        t[ls].sum=t[ls].sl=t[ls].sr=0;
        t[rs].sum=t[rs].sl=t[rs].sr=0;
        t[ls].sign=t[rs].sign=1;
    }
    if( t[rt].sign==-1 ) {
        t[ls].sum=t[ls].sl=t[ls].sr=t[ls].len;
        t[rs].sum=t[rs].sl=t[rs].sr=t[rs].len;
        t[ls].sign=t[rs].sign=-1;
    }
    t[rt].sign=0;
}

void build(int l,int r,int rt)
{
    t[rt].sign=0;
    t[rt].sum=t[rt].sl=t[rt].sr=t[rt].len=r-l+1;
    if( l==r ) return;
    int mid=(l+r)>>1;
    build(l,mid,ls); build(mid+1,r,rs);
}

void update(int l,int r,int rt,int L,int R,int C)
{
    pushdown(rt);
    if( L<=l && r<=R ) {
        if( C==1 ) t[rt].sum=t[rt].sl=t[rt].sr=0;
        else t[rt].sum=t[rt].sl=t[rt].sr=t[rt].len;
        t[rt].sign=C;
        return;
    }
    int mid=(l+r)>>1;
    if( L<=mid ) update(l,mid,ls,L,R,C);
    if( R>mid ) update(mid+1,r,rs,L,R,C);
    pushup(rt);
}

int query(int l,int r,int rt,int C)
{
    pushdown(rt);
    if( l==r ) return l;
    int mid=(l+r)>>1;
    if( t[ls].sum>=C ) return query(l,mid,ls,C);
    else if( t[ls].sr+t[rs].sl>=C ) return (mid-t[ls].sr+1);
    else return query(mid+1,r,rs,C);
}

int main()
{
    n=read(), m=read();
    int opt,x,y;
    build(1,n,1);
    for(int i=1; i<=m; ++i) {
        opt=read();
        if( opt==1 ) {
            x=read();
            if( t[1].sum<x ) { puts("0"); continue; }
            printf("%d\n",y=query(1,n,1,x));
            update(1,n,1,y,y+x-1,1);
        } else {
            x=read(), y=read();
            update(1,n,1,x,x+y-1,-1);
        }
    }
    return 0;
}