Hotel POJ 3667

题目链接:点我


    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

题意:

有一个有n个房间的宾馆,每次可能会有Di个人入住,要求入住的的这Di个人的房间必须是连续的,并且要房间号尽可能的小,第二种操作是从Xi开始,共有Di个房间的人离开,这些房间中可能会有本来就是空的房间.

思路:

线段树的区间查询以及更新.线段树的每个人节点维护当前区间最大共有多少个连续的空房间和当前节点的左区间和右区间最大有多少个连续的空房间.由于需要区间更新,所以我们维护一个懒惰标记,记录当前节点是否应该向下更新.

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

struct ss{
    int l, r, lsum, sum, tag, rsum;
}a[200000];

void build(int cur, int l, int r){
    a[cur].l = l;
    a[cur].r = r;
    a[cur].tag = -1;//-1代表没有被标记,不需要更新
    a[cur].lsum = a[cur].sum = a[cur].rsum = r - l + 1;
    if (l == r)
        return ;
    int mid = (l + r) >> 1;
    build(cur << 1, l, mid);
    build(cur << 1|1, mid + 1, r);
}

void pushdown(int cur){
    if(a[cur].tag != -1){
        int l = a[cur << 1].r - a[cur << 1].l + 1;//记录左右儿子的区间长度
        int r = a[cur << 1|1].r - a[cur << 1|1].l + 1;
        a[cur << 1].tag = a[cur << 1|1].tag = a[cur].tag;//向下传送标记
        a[cur << 1].lsum = a[cur << 1].rsum = a[cur << 1].sum = a[cur << 1].tag ? 0 : l;//根据标记的值更新儿子节点
        a[cur << 1|1].lsum = a[cur << 1|1].rsum = a[cur << 1|1].sum = a[cur << 1|1].tag ? 0 : r;
        a[cur].tag = -1;
    }
}

void pushup(int cur){
    int l = a[cur << 1].r - a[cur << 1].l + 1;
    int r = a[cur << 1|1].r - a[cur << 1|1].l + 1;
    a[cur].lsum = a[cur << 1].lsum;
    if(a[cur << 1].lsum == l) a[cur].lsum += a[cur << 1|1].lsum;
    a[cur].rsum = a[cur << 1|1].rsum;
    if(a[cur << 1|1].rsum == r) a[cur].rsum += a[cur << 1].rsum;
    a[cur].sum = max(max(a[cur << 1].sum, a[cur << 1|1].sum),a[cur << 1].rsum + a[cur << 1|1].lsum);
}

int quiry(int key, int cur){
    if (a[cur].l == a[cur].r)
        return a[cur].l;
    pushdown(cur);//每次将向下调整,更新此节点
    int  mid = (a[cur].l + a[cur].r) >> 1;
    int ans;
    if (a[cur << 1].sum >= key) ans=quiry( key, cur << 1);//优先查询左区间
    else if(a[cur <<1].rsum + a[cur << 1|1].lsum >= key) ans = mid -a[cur << 1].rsum + 1;//如果左儿子的右区间和右儿子的左区间的和满足,直接返回
    else ans = quiry( key, cur << 1|1);
    return ans;
}

void update( int l, int r, int c, int cur){//更新l至r的区间的状态
    if (l == a[cur].l && r == a[cur].r){
        a[cur].sum = a[cur].lsum = a[cur].rsum = c ? 0 : r - l +1;
        a[cur].tag = c;//懒惰标记,入住标记为1,出标记为0
        return ;
    }
    pushdown(cur);
    int mid = (a[cur].l + a[cur].r) >> 1;
    if (l > mid) update( l, r, c, cur << 1|1);
    else if (r <= mid) update( l, r, c, cur << 1);
    else {
        update( l, mid, c, cur << 1);
        update( mid + 1, r, c, cur << 1|1);
    }
    pushup(cur);
}


int main()
{
    int n, m;
    while(scanf("%d %d",&n, &m) != EOF){
        build(1, 1, n);//初始化建树
        while(m--){
            int op;
            scanf("%d", &op);
            if (op == 1){
                int x;
                scanf("%d", &x);
                if (x > a[1].sum)//如果比最大的连续区间都大,表明已经住不下
                    printf("0\n");
                else{
                    int ans = quiry( x, 1);
                    printf("%d\n", ans);
                    update( ans, ans + x -1, 1, 1);
                }
            } else {
                int x, y;
                scanf("%d %d", &x, &y);
                update( x, x + y -1, 0, 1);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值