POJ Hotel (线段树--区间合并[区间赋值])

题意:

n间空房子, 操作1问你能否连续住x 个房子, 如果能 就输出最左边的编号。 如果不能输出0;

操作2 将给定的区间[x,y] 清空。

思路:

线段树区间合并。

像极了HDU 1540  点击打开博客链接


建树 和 那个题一样, 每个结点 维护 一个区间  从左边开始最大连续长度l, 从右边开始最大连续长度r, 中间最大连续长度m(只不过改成了区间赋值的形式,加一个懒惰标记即可)

关键是查询。  查询挺有意思的。

传一个要查询的长度len 参数,直接类似单点查询递归的形式。

因为是最左边的编号, 先看左儿子的最大连续长度m 是否大于等于len 是的话,解肯定在左儿子上了。  直接递归到左区间上去。

如果不在左儿子上  看看是不是在左+右的中间区间上。

在看看是否在右儿子上。

现在才意识到 查询不必pushup()= =

#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max(a,b) ((a)<(b)?(b):(a))
using namespace std;

const int maxn = 50000 + 10;

struct Node{
    int L,R,len;
    int l,r,m; /// the sum of the empty chair!!!!!!!!!!!!
    int setv;
}nod[maxn<<2];


void pushup(int o){
    int lson = o << 1;
    int rson = o << 1 | 1;
    nod[o].l = nod[lson].l;
    if (nod[lson].l == nod[lson].len) nod[o].l += nod[rson].l;
    nod[o].r = nod[rson].r;
    if (nod[rson].r == nod[rson].len) nod[o].r += nod[lson].r;
    nod[o].m = Max(nod[o].l, nod[o].r);
    nod[o].m = Max(nod[lson].m, Max(nod[rson].m, nod[o].m));
    nod[o].m = Max(nod[o].m, nod[lson].r + nod[rson].l);
}

void pushdown(int o,int l,int r){
    if (nod[o].setv != -1){
        int lson = o << 1;
        int rson = o << 1 | 1;
        int m = l + r >> 1;
        nod[lson].setv = nod[rson].setv = nod[o].setv;
        if (nod[o].setv == 1){
            nod[lson].l = nod[lson].r = nod[lson].m = 0;
            nod[rson].l = nod[rson].r = nod[rson].m = 0;
        }
        else {
            nod[lson].l = nod[lson].r = nod[lson].m = m-l+1;
            nod[rson].l = nod[rson].r = nod[rson].m = r-m;
        }
        nod[o].setv = -1;
    }
}
void build(int l,int r,int o){
    nod[o].L = l;
    nod[o].R = r;
    nod[o].len = r - l + 1;
    nod[o].setv = -1;
    if (l == r){
        nod[o].l = nod[o].r = nod[o].m = 1;
        return;
    }
    int m = l + r >> 1;
    build(l,m,o<<1);
    build(m+1,r,o<<1|1);
    pushup(o);
}
void update(int L,int R,int c,int l,int r,int o){
    if (L <= l && r <= R){
        nod[o].setv = c;
        if (c == 1){
            nod[o].l = nod[o].r = nod[o].m = 0;
        }
        else {
            nod[o].l = nod[o].r = nod[o].m = r-l+1;
        }
        return;
    }

    pushdown(o,l,r);

    int m = l + r >> 1;
    if (m >= L){
        update(L,R,c,l,m,o<<1);
    }
    if (m < R){
        update(L,R,c,m+1,r,o<<1|1);
    }
    pushup(o);
}

int query(int len, int l,int r,int o){
    if (l == r){
        return l;
    }
    int m = l + r >> 1;
    int lson = o << 1;
    int rson = o << 1 | 1;
    pushdown(o,l,r);
    if (nod[lson].m >= len){
        return query(len, l,m,lson);
    }
    else if (nod[lson].r + nod[rson].l >= len){
        return m - nod[lson].r + 1;
    }
    else {
        return query(len, m+1,r,rson);
    }
}


int main(){

    int n, m;
    scanf("%d %d",&n, &m);
    build(1,n,1);
    for (int i = 0; i < m; ++i){
        int q,x,y;
        scanf("%d",&q);
        if (q == 1){
            scanf("%d",&x);
            int ans = 0;
            if (nod[1].m >= x) {
                ans = query(x,1,n,1);
                update(ans, ans + x - 1, 1, 1, n, 1);
            }
            printf("%d\n", ans);
        }
        else {
            scanf("%d%d",&x,&y);
            update(x,x+y-1,0,1,n,1);
        }
    }
    return 0;
}

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 17572 Accepted: 7607

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值