POJ3667:Hotel(线段树区间合并)

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 ≤ XiN-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
题意:1代表住进去n个人在一个连续区间,2代表从n开始的m个区间退房
思路:典型的线段树区间问题,记录一下区间的状态即可
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define lc l,mid,2*i
#define rc mid+1,r,2*i+1
#define lson 2*i
#define rson 2*i+1
const int L = 50000+100;
int n,m;

struct node
{
    int l,r,n;//n标记是否被覆盖
    int ls,rs,ms;
} a[L<<2];

void init(int l,int r,int i)
{
    a[i].l = l;
    a[i].r = r;
    a[i].n = -1;
    a[i].ls = a[i].rs = a[i].ms = r-l+1;
    if(l!=r)
    {
        int mid = (l+r)>>1;
        init(lc);
        init(rc);
    }
}
void pushup(int i)
{
    a[i].ls = a[lson].ls;
    a[i].rs = a[rson].rs;
    if(a[lson].ls == a[lson].r-a[lson].l+1)
        a[i].ls+=a[rson].ls;
    if(a[rson].rs == a[rson].r-a[rson].l+1)
        a[i].rs+=a[lson].rs;
    a[i].ms=max(max(a[lson].ms,a[rson].ms),a[lson].rs+a[rson].ls);
}

void pushdown(int i,int len)
{
    if(a[i].n != -1)
    {
        a[lson].n = a[rson].n = a[i].n;
        a[lson].ls = a[lson].rs = a[lson].ms = a[i].n?0:(len-len/2);
        a[rson].ls = a[rson].rs = a[rson].ms = a[i].n?0:(len/2);
        a[i].n = -1;
    }
}

void insert(int flag,int l,int r,int i)
{
    if(l<=a[i].l && a[i].r<=r)
    {
        a[i].ls = a[i].rs = a[i].ms = flag?0:a[i].r-a[i].l+1;
        a[i].n = flag;
        return;
    }
    pushdown(i,a[i].r-a[i].l+1);
    int mid = (a[i].l+a[i].r)>>1;
    if(l<=mid)
        insert(flag,l,r,lson);
    if(r>mid)
        insert(flag,l,r,rson);
    pushup(i);
}

int query(int t,int i)
{
    if(a[i].l == a[i].r)
        return 1;
    pushdown(i,a[i].r-a[i].l+1);
    int mid = (a[i].l+a[i].r)>>1;
    if(a[lson].ms>=t)
        return query(t,lson);
    else if(a[lson].rs+a[rson].ls>=t)
        return mid-a[lson].rs+1;//找出起始点
    else
        return query(t,rson);
}

int main()
{
    int flag,x,d,ans;
    while(~scanf("%d%d",&n,&m))
    {
        init(1,n,1);
        while(m--)
        {
            scanf("%d",&flag);
            if(flag == 1)
            {
                scanf("%d",&d);
                if(a[1].ms<d)//整个区间内连续的都不大于要住进来的人数
                    printf("0\n");
                else
                {
                    ans = query(d,1);
                    printf("%d\n",ans);
                    insert(1,ans,ans+d-1,1);
                }
            }
            else
            {
                scanf("%d%d",&x,&d);
                insert(0,x,x+d-1,1);
            }
        }
    }

    return 0;
}

你可能感兴趣的:(线段树,poj)