POJ3667 Hotel

URL:http://poj.org/problem?id=3667

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 numbersr..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 ofr to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi andDi 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 andDi (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 integerr, 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

线段树区间更新

Node节点含义:

mark               对应区间的状态,0 未定、1 全空、2 全满

ls                     左端最长空区间长度

rs                     右端最长空区间长度

ms                   区间内最长空区间的长度

pos                  空区间开始位置


#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 50010
using namespace std;

struct interval_tree{
    struct Node{
        int ls, rs, ms, pos, mark;
    } node[maxn << 2];
    
    void build(int l, int r, int i){
        node[i].ls = node[i].rs = node[i].ms = r - l + 1;
        node[i].pos = l;
        if(l == r) return;
        int mid = (l + r) >> 1;
        build(l, mid, i << 1);
        build(mid + 1, r, i << 1 | 1);
    }

    bool all_space(int l, int r, int i){
        return node[i].ls == r - l + 1;
    }

    void update(int l, int r, int i){
        if(!node[i].mark) return;
        if(node[i].mark == 1){
            int len = r -l + 1;
            node[i << 1].ls = node[i << 1].rs = node[i << 1].ms = (len + 1) / 2;
            node[i << 1].pos = l;
            node[i << 1 | 1].ls = node[i << 1 | 1].rs = node[i << 1 | 1].ms = len / 2;
            node[i << 1 | 1].pos = (l + r) >> 1 + 1;
            node[i << 1].mark = node[i << 1 |1].mark = 1;
        }
        else{
            node[i << 1].ls = node[i << 1].rs = node[i << 1].ms = 0;
            node[i << 1].pos = l;
            node[i << 1 | 1].ls = node[i << 1 | 1].rs = node[i << 1 | 1].ms = 0;
            node[i << 1 | 1].pos = (l + r) >> 1 + 1;
            node[i << 1].mark = node[i << 1 |1].mark = 2;
        }
        node[i].mark = 0;
    }

    int query(int d, int l, int r, int i){
        update(l, r, i);
        if(node[i].ms < d) return 0;
        if(node[i].ms == d) return node[i].pos;
        int mid = (l + r) >> 1;
        if(node[i << 1].ms >= d) return query(d, l, mid, i << 1);
        if(node[i << 1].rs + node[i << 1 | 1].ls >= d) return mid - node[i << 1].rs + 1;
        return query(d, mid + 1, r, i << 1 | 1);
    }
    
    void change(int tl, int tr, int l, int r, int i, bool flag){
        if(tr < l || tl > r) return;
        if(tl <= l && r <= tr) {
            if(flag) {
                node[i].ls = node[i].rs = node[i].ms = 0;
                node[i].pos = l;
                node[i].mark = 2;
            }
            else{
                node[i].ls = node[i].rs = node[i].ms = r - l + 1;
                node[i].pos = l;
                node[i].mark = 1;
            }
            return;
        }
        update(l, r, i);
        int mid = (l + r) >> 1;
        change(tl, tr, l, mid, i << 1, flag);
        change(tl, tr, mid + 1, r, i << 1 | 1, flag);
        node[i].ls = node[i << 1].ls;
        if(all_space(l, mid, i << 1))
            node[i].ls += node[i << 1 | 1].ls;
        node[i].rs = node[i << 1 | 1].rs;
        if(all_space(mid + 1, r, i << 1 | 1))
            node[i].rs += node[i << 1].rs;
        node[i].ms = max(node[i << 1].rs + node[i << 1 | 1].ls, max(node[i << 1].ms, node[i << 1 | 1].ms));
        if(node[i].ms == node[i << 1].ms)
            node[i].pos = node[i << 1].pos;
        else if(node[i].ms == node[i << 1].rs + node[i << 1| 1].ls)
            node[i].pos = mid - node[i << 1].rs + 1;
        else
            node[i].pos = node[i << 1 | 1].pos;
    }
} tree;


int main(){
    int n, q;
    scanf("%d %d", &n, &q);
    memset(tree.node, 0, sizeof(tree.node));
    tree.build(1, n, 1);
    int k, x, d;
    while(q--){
        scanf("%d", &k);
        if(k == 1){
            scanf("%d", &d);
            int res = tree.query(d, 1, n, 1);
            printf("%d\n", res);
            if(res)
                tree.change(res, res + d - 1, 1, n, 1, 1);
        }
        else{
            scanf("%d %d", &x, &d);
            tree.change(x, x + d - 1, 1, n, 1, 0);
        }

    }
    return 0;
}

 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值