poj 3667 Hotel(线段树区间合并模板)

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 20994   Accepted: 9131

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

两种操作第一种查询最左边的可用区间编号,第二种给一个区间置0;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson i*2,l,m
#define rson i*2+1,m+1,r
#define maxn 50010
int cover[maxn*4];//1占0空-1都有
int sub[maxn*4];//区间内由多少个可用空房间
int pre[maxn*4];//区间左边有多少可用空房间
int suf[maxn*4];//区间右边有多少可用空房间
using namespace std;
void pushdown(int i,int num)//向下推标记从父节点到子节点
{
    if(cover[i]!=-1)
    {
        cover[i*2]=cover[i*2+1]=cover[i];
        sub[i*2]=pre[i*2]=suf[i*2]=(cover[i]?0:(num-(num/2)));//为1置空为0置满
        sub[i*2+1]=pre[i*2+1]=suf[i*2+1]=(cover[i]?0:(num/2));
    }
}
void pushup(int i,int num)//由子节点向上推父节点
{
    sub[i]=max(max(sub[i*2+1],sub[i*2]),suf[i*2]+pre[i*2+1]);//左右子树中最大值或者左边区间右边的值加上上右边区间左边的值
    pre[i]=pre[i*2];
    suf[i]=suf[i*2+1];
    if(pre[i]==num-num/2)//全满
    pre[i]+=pre[i*2+1];//左右子树pre之和
    if(suf[i]==num/2)
    suf[i]+=suf[i*2];
    if(cover[i*2]==-1||cover[i*2+1]==-1)//由子节点推父节点
    cover[i]=-1;
    else if(cover[i*2]!=cover[i*2+1])
    cover[i]=-1;
    else cover[i]=cover[i*2];
}
void build(int i,int l,int r)
{
    cover[i]=0;
    sub[i]=pre[i]=suf[i]=r-l+1;//开始可用的区间长度开始全为空房间
    if(l==r)
    return ;

    int m=(l+r)/2;
    build(lson);
    build(rson);
}
int query(int w,int i,int l,int r)
{
    /*if(sub[i]<w)
    return 0;*/
    if(l==r)//叶子节点返回
    return l;
    pushdown(i,r-l+1);
    int m=(l+r)/2;
    if(w<=sub[i*2])//左边可用
    return query(w,lson);
    else if(w<=suf[i*2]+pre[i*2+1])//中间可用
    return m-suf[i*2]+1;
    else return query(w,rson);//右边可用
}
void update(int ql,int qr,int v,int i,int l,int r)
{
    if(ql<=l&&r<=qr)//在条件区间内更新,v为1不可用,为0可用
    {
        cover[i]=v;
        sub[i]=pre[i]=suf[i]=v?0:r-l+1;
        return ;
    }
    pushdown(i,r-l+1);
    int m=(l+r)/2;
    if(ql<=m)
    update(ql,qr,v,lson);
    if(m<qr)
    update(ql,qr,v,rson);
    pushup(i,r-l+1);
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        while(m--)
        {
            int op;
            int x,y;
            scanf("%d",&op);
            if(op==1)
            {scanf("%d",&y);
            if(sub[1]<y)
            {
                printf("0\n");
                continue;
            }
            int p=query(y,1,1,n);
            printf("%d\n",p);
            update(p,p+y-1,1,1,1,n);
        }
        else
        {
            scanf("%d%d",&x,&y);
            update(x,x+y-1,0,1,1,n);
        }
    }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/83753636
今日推荐