【线段树】[CQBZOJ1906]Hotel

来源:互联网 发布:ctf flag.php 编辑:程序博客网 时间:2024/05/22 13:46

题目描述
奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有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

分析:每一个节点存当前区间内最大连续空房间,从左端点开始最大连续空房间,从右端点开始最大连续空房间。

代码:

#include<cstdio>#include<algorithm>using namespace std;#define MAXN 50000struct node{    int l,r,tag,mx,ls,rs;}tree[MAXN*4+100];void Read(int &x){    char c;    while(c=getchar(),c!=EOF)        if(c>='0'&&c<='9'){            x=c-'0';            while(c=getchar(),c>='0'&&c<='9')                x=x*10+c-'0';            ungetc(c,stdin);            return;        }}int n,m;void build(int i,int l,int r){    tree[i].l=l;    tree[i].r=r;    tree[i].tag=-1;    tree[i].ls=tree[i].rs=tree[i].mx=r-l+1;    if(r==l)        return;    int mid=(l+r)/2;    build(i*2,l,mid);    build(i*2+1,mid+1,r);}void push_down(int i){    if(!tree[i].tag){        tree[i*2].ls=tree[i*2+1].ls=tree[i*2].rs=tree[i*2+1].rs=tree[i*2].mx=tree[i*2+1].mx=0;        tree[i*2].tag=tree[i*2+1].tag=0;    }    else{        tree[i*2].ls=tree[i*2].rs=tree[i*2].mx=tree[i*2].r-tree[i*2].l+1;        tree[i*2+1].ls=tree[i*2+1].rs=tree[i*2+1].mx=tree[i*2+1].r-tree[i*2+1].l+1;        tree[i*2].tag=tree[i*2+1].tag=1;    }    tree[i].tag=-1;}void insert(int i,int l,int r,int c){    if(l>tree[i].r||r<tree[i].l)        return;    if(l<=tree[i].l&&r>=tree[i].r){        tree[i].tag=c;        tree[i].ls=tree[i].rs=tree[i].mx=c*(tree[i].r-tree[i].l+1);        return;    }    if(tree[i].tag>=0)        push_down(i);    insert(i*2,l,r,c);    insert(i*2+1,l,r,c);    tree[i].mx=max(tree[i*2].mx,max(tree[i*2+1].mx,tree[i*2].rs+tree[i*2+1].ls));    tree[i].rs=tree[i*2+1].rs;    tree[i].ls=tree[i*2].ls;    if(tree[i*2].mx==tree[i*2].r-tree[i*2].l+1)        tree[i].ls+=tree[i*2+1].ls;    if(tree[i*2+1].mx==tree[i*2+1].r-tree[i*2+1].l+1)        tree[i].rs+=tree[i*2].rs;}int find(int i,int d){    if(tree[i].ls>=d)        return tree[i].l;    if(tree[i*2].mx>=d)        return find(i*2,d);    if(tree[i*2].rs+tree[i*2+1].ls>=d)        return tree[i*2].r-tree[i*2].rs+1;    return find(i*2+1,d);}int main(){    int p,x,d;    Read(n),Read(m);    build(1,1,n);    while(m--){        Read(p);        if(p==1){            Read(d);            if(tree[1].mx>=d){                int t=find(1,d);                insert(1,t,t+d-1,0);                printf("%d\n",t);            }            else                puts("0");        }        else{            Read(x),Read(d);            insert(1,x,x+d-1,1);        }    }}
0 0
原创粉丝点击