【POJ 3667】 hotel 【线段树 +区间操作+区间合并】

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14224 Accepted: 6178
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 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个房间,Q此操作。
操作1—— 1 a表示占据一段长度为a的连续的空房间,若有多个满足,尽量选择靠左的,输出选择区间的起点。
操作2—— 2 a b表示把 以房间a开始的长度为b的连续房间全部清空。
这个区间合并好玄学,难以理解,现在也只是大致理解了,这个题,想对了大半最后 在查询的问题上,还是可能没有理解深刻,还是弄不太好。没事的时候再想想吧,这种感觉和第一次接触线段树的感觉一样,时间长了,可能会好点。
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Tree{
    int l,r,len;
    int lsum,rsum,sum;
    int lazy;
}tree[MAXN<<2];
void Up(int o){     
    tree[o].lsum=tree[o<<1].lsum;
    tree[o].rsum=tree[o<<1|1].rsum;
    tree[o].sum=max(tree[o<<1].sum,tree[o<<1|1].sum);
    if(tree[o<<1].lsum==tree[o<<1].len) 
        tree[o].lsum+=tree[o<<1|1].lsum;
    if(tree[o<<1|1].rsum==tree[o<<1|1].len)
        tree[o].rsum+=tree[o<<1].rsum;
    tree[o].sum=max(tree[o].sum,tree[o<<1].rsum+tree[o<<1|1].lsum);
} 

void Down(int o){
    if(tree[o].lazy!=-1){
        tree[o<<1].lazy=tree[o<<1|1].lazy=tree[o].lazy;
        tree[o<<1].lsum=tree[o<<1].rsum=tree[o<<1].sum=tree[o<<1].len*(tree[o].lazy?0:1);
        tree[o<<1|1].lsum=tree[o<<1|1].rsum=tree[o<<1|1].sum=tree[o<<1|1].len*(tree[o].lazy?0:1);
        tree[o].lazy=-1;      
    }
}

void Build(int o,int le,int ri){
    tree[o].l=le;tree[o].r=ri;tree[o].len=ri-le+1;
    tree[o].lazy=-1;
    if(le==ri){
         tree[o].lsum=tree[o].rsum=tree[o].sum=1;
         return ;
    }
    int mid=(le+ri)>>1;
    Build(o<<1,le,mid);
    Build(o<<1|1,mid+1,ri); 
    Up(o);
}

int Query(int o,int val){
    if(tree[o].l==tree[o].r) return tree[o].l;
    Down(o);
    int mid=(tree[o].l+tree[o].r)>>1;
    if(val<=tree[o<<1].sum) // 三个if 必须要按照这个顺序,因为要的是最小的序号
        return Query(o<<1,val);
    else if(val<=tree[o<<1].rsum+tree[o<<1|1].lsum) 
        return mid-tree[o<<1].rsum+1;
    else if(val<=tree[o<<1|1].sum) 
        return Query(o<<1|1,val);
}

void Update(int o,int le,int ri,int val){
    if(le<=tree[o].l&&tree[o].r<=ri) {
        tree[o].lsum=tree[o].rsum=tree[o].sum=tree[o].len*(val?0:1);
        tree[o].lazy=val;
        return ;
    }
    Down(o);
    int mid=(tree[o].l+tree[o].r)>>1;
    if(ri<=mid) Update(o<<1,le,ri,val);
    else if(le>mid) Update(o<<1|1,le,ri,val);
    else {
        Update(o<<1,le,mid,val);
        Update(o<<1|1,mid+1,ri,val);
    } 
    Up(o);
}

int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n,q;scanf("%d%d",&n,&q);
    Build(1,1,n);
    int op,a,b;
    while(q--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d",&a);
            if(tree[1].sum<a) puts("0");
            else {
                int pos=Query(1,a);
                printf("%d\n",pos);
                Update(1,pos,pos+a-1,1);
            }
        }else {
            scanf("%d%d",&a,&b);
            Update(1,a,a+b-1,0);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值