[线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 16089 Accepted: 6998

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 rto 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 D(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


题意:


思路:

区间合并模板,思路都在代码里

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#define inf 1<<29

using namespace std;
const int N = 5e4+10;
struct{
    int l, r;
    int mark;//-1未标记,0入住,1退宿(之所以-1、1不合成一个是因为有延迟/懒惰标记
    int lsum, rsum, msum;//左区间(下界为起点的连续区间),右区间(上界为终点的连续区间),最大连续区间
}Tree[N*4];
//建树
void build(int root, int left, int right)
{
    Tree[root].mark = -1;
    Tree[root].l = left;
    Tree[root].r = right;
    Tree[root].msum = Tree[root].lsum = Tree[root].rsum = right - left + 1;
    if(left == right)
        return;
    int mid = (left + right) >> 1;
    build(root<<1, left, mid);
    build(root<<1|1, mid+1, right);
}
//延迟标记向下传递,维护root节点之下区间的准确性
void pushdown(int root)
{
    int k = Tree[root].mark;
    if(k != -1)
    {
        int mid = (Tree[root].l + Tree[root].r) >> 1;
        Tree[root<<1].mark = Tree[root<<1|1].mark = k;
        Tree[root<<1].msum = Tree[root<<1].lsum = Tree[root<<1].rsum = k ? (mid-Tree[root].l+1) : 0;
        Tree[root<<1|1].msum = Tree[root<<1|1].lsum = Tree[root<<1|1].rsum = k ? (Tree[root].r-mid) : 0;
        Tree[root].mark = -1;
    }
}
int query(int root, int len)
{
    if(Tree[root].l == Tree[root].r)
        return Tree[root].l;
    //向下维护,延迟/懒惰标记传递
    pushdown(root);
    int mid = (Tree[root].l + Tree[root].r) >> 1;
    //左子节点的最大连续区间
    if(Tree[root<<1].msum >= len)
        return query(root<<1, len);
    //左子节点的右区间+右子节点的左区间
    else if(Tree[root<<1].rsum + Tree[root<<1|1].lsum >= len)
        return mid - Tree[root<<1].rsum + 1;
    return query(root<<1|1, len);
}
//延迟标记向上更新,维护root节点之上区间的准确性
void pushup(int root)
{
    int mid = (Tree[root].l + Tree[root].r) >> 1;
    //当前节点的左区间一定包含左节点的左区间
    Tree[root].lsum = Tree[root<<1].lsum;
    //当前节点的右区间一定包含右节点的右区间
    Tree[root].rsum = Tree[root<<1|1].rsum;
    if(Tree[root<<1].lsum == mid-Tree[root].l+1)
    //当前节点的左区间等于左节点的最大连续区间,也包含右节点的左区间
        Tree[root].lsum += Tree[root<<1|1].lsum;
    if(Tree[root<<1|1].rsum == Tree[root].r-mid)
    //当前节点的右区间等于右节点的最大连续区间,也包含左节点的右区间
        Tree[root].rsum += Tree[root<<1].rsum;
    //当前节点的最大连续区间 = max{左节点的最大连续区间,右节点的最大连续区间,左节点右区间+右节点左区间};
    int t = max(Tree[root<<1].msum, Tree[root<<1|1].msum);
    Tree[root].msum = max(Tree[root<<1].rsum+Tree[root<<1|1].lsum, t);
}
void update(int root, int l, int r, int value)
{
    if(l > Tree[root].r || r < Tree[root].l)
        return;
    if(l <= Tree[root].l && r >= Tree[root].r)
    {
        Tree[root].mark = value;
        Tree[root].msum = Tree[root].lsum = Tree[root].rsum = value ? (Tree[root].r-Tree[root].l+1) : 0;
        return;
    }
    //延迟标记向下传递
    pushdown(root);
    update(root<<1, l, r, value);
    update(root<<1|1, l , r, value);
    //向上恢复标记
    pushup(root);
}
int main()
{
    int n, m;
    int a, b, c;

    scanf("%d%d", &n, &m);
    build(1, 1, n);
    while(m-->0)
    {
        scanf("%d", &a);
        if(a == 1)//入住,区间查询+更新
        {
            scanf("%d", &b);
            if(b > Tree[1].msum)
                printf("0\n");
            else
            {
                int t = query(1, b);
                printf("%d\n", t);
                update(1, t, t+b-1, 0);
            }
        }
        else//退宿,区间更新
        {
            scanf("%d%d", &b, &c);
            update(1, b, b+c-1, 1);
        }
    }
    return 0;
}
/*
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
------
1
4
7
0
5
*/


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值