poj 3667 Hotel 【线段树lazy + 区间合并】

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14224 Accepted: 6178

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 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
题意:N个房间,Q此操作。
操作1—— 1 a表示占据一段长度为a的连续的空房间,若有多个满足,尽量选择靠左的,输出选择区间的起点。
操作2—— 2 a b表示把 以房间a开始的长度为b的连续房间全部清空。
线段树lazy + 区间合并
注意—— 查询操作时,必须左子树——左子树+右子树——右子树,因为题目要求选择起点靠左的区间。
AC代码:有详细注释 重在PushUp函数的操作。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 500000+10
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Tree
{
    int l, r;//区间左右端点
    int lsum, rsum;//区间最左端为起点的连续的空位置长度 区间最右端为终点的连续的空位置长度
    int sum;//区间最长的连续空位置 的长度
    int lazy;//延迟标记
};
Tree tree[MAXN<<2];
void build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].lsum = tree[o].rsum = tree[o].sum = r - l + 1;
    tree[o].lazy = -1;
    if(tree[o].l == tree[o].r)
        return ;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}
void PushDown(int o)
{
    int m = tree[o].r - tree[o].l + 1;//区间长度
    if(tree[o].lazy != -1)//标记 更新
    {
        tree[ll].lazy = tree[rr].lazy = tree[o].lazy;//下传左子节点 和 右子节点
        tree[ll].lsum = tree[ll].rsum = tree[ll].sum = tree[o].lazy ? 0 : m - (m>>1);
        tree[rr].lsum = tree[rr].rsum = tree[rr].sum = tree[o].lazy ? 0 : (m>>1);
        tree[o].lazy = -1;//取消标记
    }
}
void PushUp(int o)
{
    int m = tree[o].r - tree[o].l + 1;//区间长度
    tree[o].lsum = tree[ll].lsum;
    tree[o].rsum = tree[rr].rsum;
    if(tree[o].lsum == m - (m>>1))//还可以继续容纳 向右子树延伸
        tree[o].lsum += tree[rr].lsum;
    if(tree[o].rsum == (m>>1))//还可以继续容纳 向左子树延伸
        tree[o].rsum += tree[ll].rsum;
    //更新最值  max(左子树右端点 + 右子树左端点, 左子树最值, 右子树最值)
    tree[o].sum = max(tree[ll].rsum + tree[rr].lsum, max(tree[ll].sum, tree[rr].sum));
}
int query(int o, int pos)
{
    if(tree[o].l == tree[o].r)
        return tree[o].l;
    PushDown(o);//延迟 下传
    int mid = (tree[o].l + tree[o].r) >> 1;
    //注意下面查询顺序不能反 因为题目要求尽量选靠左的区间
    if(pos <= tree[ll].sum)//左子树 可以容纳
        return query(ll, pos);
    else if(pos <= tree[ll].rsum + tree[rr].lsum)//左子树连续右端点 + 右子树连续左端点可以容纳
        return mid - tree[ll].rsum + 1;
    else if(pos <= tree[rr].sum)//右子树 可以容纳
        return query(rr, pos);
}
void update(int L, int R, int o, int v)//v为1时代表占据位置 为0时代表清空位置
{
    if(L <= tree[o].l && R >= tree[o].r)
    {
        tree[o].lsum = tree[o].rsum = tree[o].sum = v ? 0 : (tree[o].r - tree[o].l + 1);
        tree[o].lazy = v;
        return ;
    }
    PushDown(o);//延迟
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(L <= mid)
        update(L, R, ll, v);
    if(R > mid)
        update(L, R, rr, v);
    PushUp(o);//上传更新
}
int main()
{
    int N, Q;
    while(scanf("%d%d", &N, &Q) != EOF)
    {
        build(1, 1, N);//建树
        int op, a, b;
        while(Q--)
        {
            scanf("%d", &op);
            if(op == 1)
            {
                scanf("%d", &a);
                if(tree[1].sum < a)//位置不够
                    printf("0\n");
                else
                {
                    int pos = query(1, a);//查询位置
                    printf("%d\n", pos);
                    update(pos, pos+a-1, 1, 1);//更新
                }
            }
            else
            {
                scanf("%d%d", &a, &b);
                update(a, a+b-1, 1, 0);
            }
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值