• POJ 3667 Hotel


    Hotel
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 16782   Accepted: 7303

    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

      题目大意:有n个房间,支持一下两种操作:1.将区间所有的房间清空。2.从左往右查询一段长度大于或等于w的连续空房间并将这些房间填满,查询的返回值为这段房间的左端点。

      相比之前提到的线段树例题,这道题目又有了很大的不同。由于这道题的操作与查询都是建立在一段区间上的,所以不难想到这道题是要用线段树来维护的。(其实是标签贴的是线段树)但是具体怎么实现呢?这就要牵涉到线段树的区间合并操作了。首先我们要多开几个数组维护区间的信息,lsum[]用来维
    护这段区间内从左端点开始的一段连续空闲的区间长;rsum[]用来维护这段区间从右端点开始的一段连续空闲的区间长;sum[]用来维护这段区间中最长的一段空闲的区间长。有了这三个数组,就只要考虑着重考虑pushup操作和pushdown操作了。

      pushup操作:
    1 void pushup(int id){
    2     int l=left[id];int r=right[id];
    3     lsum[id]=lsum[id<<1];rsum[id]=rsum[id<<1|1];
    4     int mid=(l+r)>>1;
    5     if(lsum[id]==mid-l+1)lsum[id]+=lsum[id<<1|1];
    6     if(rsum[id]==r-mid)rsum[id]+=rsum[id<<1];
    7     sum[id]=max(max(sum[id<<1],sum[id<<1|1]),lsum[id<<1|1]+rsum[id<<1]);
    8 }  
      
      具体来分析一下这个操作的具体含义显然有根的lsum与它左孩子的lsum相同,因为它们的左端点相同;同理也有根的rsum与根的右孩子的rsum相同。由于lsum或是rsum的长度可能越过了mid,所以我们判定一下lsum与rsum的长度,如果已经到了mid了,则还要加上另一截,才能保证正确。最后sum的大小
    也很容易确定,就是左边最长的一段、右边最长的一段、还有中间一段长的最大值。

      pushdown操作:
    1 void pushdown(int id){
    2     if(cover[id]!=-1){
    3         cover[id<<1]=cover[id<<1|1]=cover[id];
    4         sum[id<<1]=lsum[id<<1]=rsum[id<<1]=cover[id]?0:right[id<<1]-left[id<<1]+1;
    5         sum[id<<1|1]=lsum[id<<1|1]=rsum[id<<1|1]=cover[id]?0:right[id<<1|1]-left[id<<1|1]+1;
    6         cover[id]=-1;
    7     }
    8 }
      
      这个操作还是比较简单的了,首先lazy tag的思想是要有的,相信有了前面的线段树的基础,这点是不在话下的。那么就好办了,直接判断cover标记,如果为0,则表示清空,那么这段区间sum、lsum、rsum都会是这段区间的完整长度;如果为1,那么则表示这段区间没有空闲空间了,则全部赋值为0.最后
    别忘了还原标记,这个忘了打我调了半天。

    最后附上AC代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 static const int maxm=1e6+10;
     7 
     8 int left[maxm],right[maxm],lsum[maxm],rsum[maxm],sum[maxm],cover[maxm];
     9 int n,m,f,opt,pos;
    10 
    11 void build(int id,int l,int r){
    12     left[id]=l;right[id]=r;cover[id]=-1;
    13     sum[id]=lsum[id]=rsum[id]=r-l+1;
    14     int mid=(l+r)>>1;
    15     if(l==r)return;
    16     build(id<<1,l,mid);
    17     build(id<<1|1,mid+1,r);
    18 }
    19 
    20 void pushdown(int id){
    21     if(cover[id]!=-1){
    22         cover[id<<1]=cover[id<<1|1]=cover[id];
    23         sum[id<<1]=lsum[id<<1]=rsum[id<<1]=cover[id]?0:right[id<<1]-left[id<<1]+1;
    24         sum[id<<1|1]=lsum[id<<1|1]=rsum[id<<1|1]=cover[id]?0:right[id<<1|1]-left[id<<1|1]+1;
    25         cover[id]=-1;
    26     }
    27 }
    28 
    29 void pushup(int id){
    30     int l=left[id];int r=right[id];
    31     lsum[id]=lsum[id<<1];rsum[id]=rsum[id<<1|1];
    32     int mid=(l+r)>>1;
    33     if(lsum[id]==mid-l+1)lsum[id]+=lsum[id<<1|1];
    34     if(rsum[id]==r-mid)rsum[id]+=rsum[id<<1];
    35     sum[id]=max(max(sum[id<<1],sum[id<<1|1]),lsum[id<<1|1]+rsum[id<<1]);
    36 }
    37 
    38 void update(int id,int l,int r,int c){
    39     if(left[id]>=l&&right[id]<=r){
    40         sum[id]=lsum[id]=rsum[id]=c?0:right[id]-left[id]+1;
    41         cover[id]=c;
    42         return;
    43     }
    44     if(left[id]>r||right[id]<l)return;
    45     pushdown(id);
    46     update(id<<1,l,r,c);
    47     update(id<<1|1,l,r,c);
    48     pushup(id);
    49 }
    50 
    51 int Query(int id,int c){
    52     if(left[id]==right[id])return left[id];
    53     pushdown(id);
    54     int mid=(left[id]+right[id])>>1;
    55     if(sum[id<<1]>=c)return Query(id<<1,c);
    56     else if(rsum[id<<1]+lsum[id<<1|1]>=c)return mid-rsum[id<<1]+1;
    57     else return Query(id<<1|1,c);
    58 }
    59 
    60 int main(){
    61     scanf("%d%d",&n,&m);
    62     build(1,1,n);
    63     
    64     while(m--){
    65         int x,y;
    66         scanf("%d",&opt);
    67         if(opt==1){
    68             scanf("%d",&x);
    69             if(sum[1]<x)printf("0
    ");
    70             else{
    71                 printf("%d
    ",pos=Query(1,x));
    72                 update(1,pos,pos+x-1,1);
    73             }
    74         }else{
    75             scanf("%d%d",&x,&y);
    76             update(1,x,x+y-1,0);
    77         }
    78     } 
    79     
    80     return 0;
    81 }
    View Code

    AC通道:http://poj.org/problem?id=3667




  • 相关阅读:
    js对象的直接赋值、浅拷贝与深拷贝
    如何使ElementUi中的el-dropdown传入多参数
    机器学习之垃圾邮件分类2
    机器学习之手写数字识别-小数据集
    机器学习之深度学习-卷积
    机器学习之朴素贝叶斯-垃圾邮件分类
    机器学习之分类与监督学习,朴素贝叶斯分类算法
    机器学习之主成分分析(PCA&特征选择)
    机器学习之逻辑回归实践
    机器学习之特征选择
  • 原文地址:https://www.cnblogs.com/Exbilar/p/6403398.html
Copyright © 2020-2023  润新知