【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并

题目描述

奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。

输入

* 第1行: 2个用空格隔开的整数:N、M

* 第2..M+1行: 第i+1描述了第i个请求,如果它是一个订房请求,则用2个数字 1、D_i描述,数字间用空格隔开;如果它是一个退房请求,用3 个以空格隔开的数字2、X_i、D_i描述

输出

* 第1..??行: 对于每个订房请求,输出1个独占1行的数字:如果请求能被满足 ,输出满足条件的最小的r;如果请求无法被满足,输出0

样例输入

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

样例输出

1
4
7
0
5


题解

线段树区间合并。

查询不是标准查询,需要自己脑补,其他的写法都差不多,反正别更改到不存在的节点就行。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l , mid , x << 1
#define rson mid + 1 , r , x << 1 | 1
using namespace std;
int ls[200010] , rs[200010] , ts[200010] , tag[200010];
void pushup(int l , int r , int x)
{
    int mid = (l + r) >> 1;
    ls[x] = ls[x << 1] == mid - l + 1 ? ls[x << 1] + ls[x << 1 | 1] : ls[x << 1];
    rs[x] = rs[x << 1 | 1] == r - mid ? rs[x << 1 | 1] + rs[x << 1] : rs[x << 1 | 1];
    ts[x] = max(max(ts[x << 1] , ts[x << 1 | 1]) , rs[x << 1] + ls[x << 1 | 1]);
}
void pushdown(int l , int r , int x)
{
    if(tag[x] != -1)
    {
        int mid = (l + r) >> 1;
        ls[x << 1] = rs[x << 1] = ts[x << 1] = (mid - l + 1) * (tag[x] ^ 1);
        ls[x << 1 | 1] = rs[x << 1 | 1] = ts[x << 1 | 1] = (r - mid) * (tag[x] ^ 1);
        tag[x << 1] = tag[x << 1 | 1] = tag[x];
        tag[x] = -1;
    }
}
void build(int l , int r , int x)
{
    if(l == r)
    {
        ls[x] = rs[x] = ts[x] = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(l , r , x);
}
void update(int b , int e , int a , int l , int r , int x)
{
    if(b <= l && r <= e)
    {
        ls[x] = rs[x] = ts[x] = (r - l + 1) * (a ^ 1);
        tag[x] = a;
        return;
    }
    pushdown(l , r , x);
    int mid = (l + r) >> 1;
    if(b <= mid) update(b , e , a , lson);
    if(e > mid) update(b , e , a , rson);
    pushup(l , r , x);
}
int query(int d , int l , int r , int x)
{
    if(ts[x] < d) return 0;
    if(ls[x] >= d) return l;
    pushdown(l , r , x);
    int mid = (l + r) >> 1;
    if(ts[x << 1] >= d) return query(d , lson);
    else if(rs[x << 1] + ls[x << 1 | 1] >= d) return mid - rs[x << 1] + 1;
    else return query(d , rson);
}
int main()
{
    int n , m , p , x , d;
    scanf("%d%d" , &n , &m);
    memset(tag , -1 , sizeof(tag));
    build(1 , n , 1);
    while(m -- )
    {
        scanf("%d" , &p);
        if(p == 1)
        {
            scanf("%d" , &d);
            x = query(d , 1 , n , 1);
            printf("%d
" , x);
            if(x) update(x , x + d - 1 , 1 , 1 , n , 1);
        }
        else
        {
            scanf("%d%d" , &x , &d);
            update(x , x + d - 1 , 0 , 1 , n , 1);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/GXZlegend/p/6409636.html