POJ 3667 hotel 线段树区间合并

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 19993   Accepted: 8684

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

感谢https://blog.csdn.net/piaoyi0208/article/details/8149804给与思路

lsum代表从左端点开始的连续区间

rsum代表从右端点开始的连续区间

sum代表管理区间内最大的连续区间

因为线段树可以把区间最小分为1

所以寻找连续区间只需考虑中间这一种情况

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 5e4+10;
struct node
{
    int l,r;
    int lsum,rsum,sum;
    int tag;
}tree[maxn<<2];
void build(int l,int r,int rt)
{
     tree[rt].l=l,tree[rt].r=r;
     tree[rt].lsum=tree[rt].rsum=tree[rt].sum=r-l+1;
     tree[rt].tag=-1;
     if(l==r) return ;
     int mid=(l+r)>>1;
     build(l,mid,rt<<1);
     build(mid+1,r,rt<<1|1);
}
void pushup(int rt)
{
    tree[rt].lsum=tree[rt<<1].lsum; //父节点的lsum为左儿子的lsum
    tree[rt].rsum=tree[rt<<1|1].rsum; //同理
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(tree[rt].lsum==mid-tree[rt].l+1) tree[rt].lsum+=tree[rt<<1|1].lsum; //如果lsum全是空的则可以将右儿子的lsum连起来
    if(tree[rt].rsum==tree[rt].r-mid) tree[rt].rsum+=tree[rt<<1].rsum; //同理
    tree[rt].sum=max(max(tree[rt<<1].sum,tree[rt<<1|1].sum),tree[rt<<1].rsum+tree[rt<<1|1].lsum); //寻找最大的区间
}
void pushdown(int rt)
{
    if(tree[rt].tag!=-1)
    {
        tree[rt<<1].tag=tree[rt<<1|1].tag=tree[rt].tag; 
        if(tree[rt].tag)
        {
            tree[rt<<1].lsum=tree[rt<<1].rsum=tree[rt<<1].sum=0;
            tree[rt<<1|1].lsum=tree[rt<<1|1].rsum=tree[rt<<1|1].sum=0;
        }
        else
        {
            tree[rt<<1].lsum=tree[rt<<1].rsum=tree[rt<<1].sum=tree[rt<<1].r-tree[rt<<1].l+1;
            tree[rt<<1|1].lsum=tree[rt<<1|1].rsum=tree[rt<<1|1].sum=tree[rt<<1|1].r-tree[rt<<1|1].l+1;
        }
    }
    tree[rt].tag=-1;
}
void update(int x,int y,int rt,int v)
{
    if(x<=tree[rt].l&&tree[rt].r<=y)
    {
        tree[rt].tag=v;
        if(v)tree[rt].rsum=tree[rt].lsum=tree[rt].sum=0;
        else tree[rt].rsum=tree[rt].lsum=tree[rt].sum=tree[rt].r-tree[rt].l+1;
        return;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(x<=mid) update(x,y,rt<<1,v);
    if(mid<y) update(x,y,rt<<1|1,v);
    pushup(rt);
}
int query(int l,int r,int rt,int v)
{
    if(l==r) return l; 
    int mid=(l+r)>>1;
    pushdown(rt);
    if(tree[rt<<1].sum>=v) return query(l,mid,rt<<1,v);
    else if(tree[rt<<1].rsum+tree[rt<<1|1].lsum>=v) return mid-tree[rt<<1].rsum+1;
    else return query(mid+1,r,rt<<1|1,v);
}
int main()
{
    int i,j,n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            int t;
            scanf("%d",&t);
            if(t==1)
            {
                int t1;
                scanf("%d",&t1);
                if(tree[1].sum<t1)
                {
                    printf("0\n");
                    continue;
                }
                int t2=query(1,n,1,t1);
                printf("%d\n",t2);
                update(t2,t2+t1-1,1,1);
            }
            else
            {
                int t1,t2;
                scanf("%d%d",&t1,&t2);
                update(t1,t1+t2-1,1,0);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ljq199926/article/details/81302353
今日推荐