[poj 3367] Hotel 线段树应用

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15205 Accepted: 6583

Description

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.

Input

  • 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

Output

  • 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.

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

Source
USACO 2008 February Gold

题目链接http://poj.org/problem?id=3667
题意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,要求房间的编号为连续的r..r+Di-1并且r是最小的;visitors同样可能check out,并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式:

1  a     :  groups需要check in  a间编号连续的房间
2  a   b : visitors  check out 房间,其中房间编号是 a…a+b-1

要求对于每次request,输出为groups分配数目为a的房间中编号最小的房间编号;
思路:类比最大连续子段和 tr.lx代表左端最长的房间数; tr.rx代表右端最长的房间数;
更新:


    int mid=(l+r)>>1;
    if(tr[lson].lx==mid-l+1)  tr[id].lx=tr[lson].rx+tr[rson].lx;
    else tr[id].lx=tr[lson].lx;

    if(tr[rson].rx==r-mid)  tr[id].rx=tr[rson].lx+tr[lson].rx;
    else tr[id].rx=tr[rson].rx;

能左找左找–》query 函数

int query(int id,int l,int r,int k)
{
    //cout<<id<<"               "<<l<<"   "<<r<<"                           "<<tr[lson].val<<endl;
    int mid=(l+r)>>1;
    if(l!=r) 
    push_down(id,l,r);
    if(tr[id].val<k) return 0;//找不到
    if(tr[id].lx>=k) return l; //左段开始的长度够
    if(tr[lson].val>=k) return query(lson,l,mid,k);//左找
    if(tr[rson].lx+tr[lson].rx>=k) return mid-tr[lson].rx+1;//跨区间
    if(tr[rson].val>=k) return query(rson,mid+1,r,k);
    return 0;
}

代码

#include<iostream>
#include<string.h>
#include<stdio.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
struct node{
int lx,rx,val,lazy;
}tr[50002*4];
int n,m;
void push_down(int id,int l,int r)
{
    if(tr[id].lazy==-1) return;
    int mid=(l+r)>>1;
    tr[lson].lazy=tr[rson].lazy=tr[id].lazy;
    if(tr[id].lazy==1)
    {
        tr[lson].lx=tr[rson].lx=tr[lson].rx=tr[rson].rx=tr[lson].val=tr[rson].val=0;
    }
    else
    {
        tr[lson].lx=tr[lson].rx=tr[lson].val=mid-l+1;
        tr[rson].lx=tr[rson].rx=tr[rson].val=r-mid;
    }
    tr[id].lazy=-1;
}
void push_up(int id,int l,int r)
{
    int mid=(l+r)>>1;
    if(tr[lson].lx==mid-l+1)  tr[id].lx=tr[lson].rx+tr[rson].lx;
    else tr[id].lx=tr[lson].lx;

    if(tr[rson].rx==r-mid)  tr[id].rx=tr[rson].lx+tr[lson].rx;
    else tr[id].rx=tr[rson].rx;

    tr[id].val=max(max(tr[lson].val,tr[rson].val),tr[lson].rx+tr[rson].lx);
}
int query(int id,int l,int r,int k)
{
    //cout<<id<<"               "<<l<<"   "<<r<<"                           "<<tr[lson].val<<endl;
    int mid=(l+r)>>1;
    if(l!=r) 
    push_down(id,l,r);
    if(tr[id].val<k) return 0;
    if(tr[id].lx>=k) return l;
    if(tr[lson].val>=k) return query(lson,l,mid,k);
    if(tr[rson].lx+tr[lson].rx>=k) return mid-tr[lson].rx+1;
    if(tr[rson].val>=k) return query(rson,mid+1,r,k);
    return 0;
}
void add(int id,int L,int R,int l,int r,int v)
{

    if(R<L||r<L||l>R) return ;

    if(L>=l&&R<=r)
    {
        tr[id].lazy=v;
        if(v==0)
        {
            tr[id].lx=tr[id].rx=tr[id].val=R-L+1;

        }
        else
        {       
            tr[id].lx=tr[id].rx=tr[id].val=0;           
        }
        //cout<<"            "<<id<<"       "<<L<<"     "<<R<<"   "<<tr[id].lx<<"   "<<tr[id].rx<<"   "<<tr[id].val<<endl;
        return ;    
    }
    int mid=(L+R)>>1;
    push_down(id,L,R);
    if(l<=mid)
    add(lson,L,mid,l,r,v);
    if(r>mid)
    add(rson,mid+1,R,l,r,v);
    push_up(id,L,R);
    //cout<<"            "<<id<<"       "<<L<<"     "<<R<<"   "<<tr[id].lx<<"  "<<tr[id].rx<<"   "<<tr[id].val<<endl;

}
void build(int id,int l,int r)
{

    tr[id].rx=tr[id].lx=tr[id].val=r-l+1;
    tr[id].lazy=-1;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
}
int main()
{
    int aa,bb,cc;
    scanf("%d%d",&n,&m);
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&aa,&bb);
        if(aa==1)
        {
            int pos=query(1,1,n,bb);
            printf("%d\n",pos);
            if(pos)
            add(1,1,n,pos,pos+bb-1,1);
        }
        else 
        {
            scanf("%d",&cc);
            add(1,1,n,bb,bb+cc-1,0);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值