zoukankan      html  css  js  c++  java
  • 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel

    题目描述

    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.

    参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

    若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

    若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

    输入输出格式

    输入格式:

    * 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

    输出格式:

    * 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.

    输入输出样例

    输入样例#1: 复制
    10 6
    1 3
    1 3
    1 3
    1 3
    2 5 5
    1 6
    
    输出样例#1: 复制
    1
    4
    7
    0
    5

    线段树区间合并

    代码:

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 typedef long long ll;
      4 const int maxn=5e5+10;
      5 #define lson l,m,rt<<1
      6 #define rson m+1,r,rt<<1|1
      7 
      8 struct Tree{
      9     int lch,rch,val,lazy;
     10 }tree[maxn<<2];
     11 
     12 void pushup(int l,int r,int rt)
     13 {
     14     int m=(l+r)>>1;
     15     if(tree[rt<<1].val==(m-l+1)) tree[rt].lch=tree[rt<<1].val+tree[rt<<1|1].lch;
     16     else tree[rt].lch=tree[rt<<1].lch;
     17     if(tree[rt<<1|1].val==(r-m)) tree[rt].rch=tree[rt<<1|1].val+tree[rt<<1].rch;
     18     else tree[rt].rch=tree[rt<<1|1].rch;
     19     tree[rt].val=max(max(tree[rt<<1].val,tree[rt<<1|1].val),tree[rt<<1].rch+tree[rt<<1|1].lch);
     20 }
     21 
     22 void pushdown(int l,int r,int rt)
     23 {
     24     int m=(l+r)>>1;
     25     if(tree[rt].lazy){
     26         if(tree[rt].lazy==1){//空房
     27             tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
     28             tree[rt<<1].lch=tree[rt<<1].rch=tree[rt<<1].val=m-l+1;
     29             tree[rt<<1|1].lch=tree[rt<<1|1].rch=tree[rt<<1|1].val=r-m;
     30         }
     31         else if(tree[rt].lazy==2){//住满
     32             tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
     33             tree[rt<<1].lch=tree[rt<<1].rch=tree[rt<<1].val=0;
     34             tree[rt<<1|1].lch=tree[rt<<1|1].rch=tree[rt<<1|1].val=0;
     35         }
     36         tree[rt].lazy=0;
     37     }
     38 }
     39 
     40 //两种build的方式,直接在判断前面写,就不需要pushup,写在判断里面就需要pushup,上传到爸爸
     41 void build(int l,int r,int rt)
     42 {
     43     tree[rt].lazy=0;
     44     if(l==r){
     45         tree[rt].lch=tree[rt].rch=tree[rt].val=1;
     46         return ;
     47     }
     48 
     49     int m=(l+r)>>1;
     50     build(lson);
     51     build(rson);
     52     pushup(l,r,rt);
     53 }
     54 
     55 //void build(int l,int r,int rt)
     56 //{
     57 //    tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+1;
     58 //    tree[rt].lazy=0;
     59 //    if(l==r){
     60 //        return ;
     61 //    }
     62 //
     63 //    int m=(l+r)>>1;
     64 //    build(lson);
     65 //    build(rson);
     66 //}
     67 
     68 void update(int L,int R,int c,int l,int r,int rt)//更新,节点标记已经下传了,所以下面判断就不对了
     69 {
     70     if(tree[rt].lazy!=0){
     71         pushdown(l,r,rt);
     72     }
     73 
     74     if(L<=l&&r<=R){
     75         if(c==1){
     76             tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+1;
     77         }
     78         else if(c==2){
     79             tree[rt].lch=tree[rt].rch=tree[rt].val=0;
     80         }
     81         tree[rt].lazy=c;
     82         return ;
     83     }
     84 
     85     int m=(l+r)>>1;
     86     if(L<=m) update(L,R,c,lson);
     87     if(R> m) update(L,R,c,rson);
     88     pushup(l,r,rt);
     89 }
     90 
     91 int query(int c,int l,int r,int rt)
     92 {
     93     if(tree[rt].lazy!=0){
     94         pushdown(l,r,rt);
     95     }
     96 
     97     if(l==r){
     98         return l;
     99     }
    100 
    101     int m=(l+r)>>1;
    102     if(tree[rt<<1].val>=c) return query(c,lson);
    103     else if(tree[rt<<1].rch+tree[rt<<1|1].lch>=c) return m-tree[rt<<1].rch+1;
    104     else return query(c,rson);
    105 }
    106 
    107 int main()
    108 {
    109     int n,m;
    110     scanf("%d%d",&n,&m);
    111     build(1,n,1);
    112 //    for(int i=1;i<=40;i++)
    113 //        cout<<i<<" "<<tree[i].val<<endl;
    114     for(int i=1;i<=m;i++){
    115         int op;
    116         scanf("%d",&op);
    117         if(op==1){
    118             int x;
    119             scanf("%d",&x);
    120             if(tree[1].val>=x){
    121                 int ans=query(x,1,n,1);
    122                 printf("%d
    ",ans);
    123                 update(ans,ans+x-1,2,1,n,1);//住满
    124             }
    125             else{
    126                 printf("0
    ");
    127             }
    128         }
    129         else{
    130             int x,y;
    131             scanf("%d%d",&x,&y);
    132             update(x,x+y-1,1,1,n,1);//清空
    133         }
    134     }
    135     return 0;
    136 }
  • 相关阅读:
    sourceTree push提交代码
    sourcetree 创建分支
    sourcetree 删除分支
    php 根据经纬度计算距离
    git linux服务器拉取代码sh脚本,批量拉取git代码
    百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 531人阅读 评论(0) 收藏
    2020企业数字化转型的思考
    数据治理与企业数字化转型
    亚信科技入围中国大数据领域三大重磅榜单
    大数据智能分析的“六个特征”和“六个能力”
  • 原文地址:https://www.cnblogs.com/ZERO-/p/10733414.html
Copyright © 2011-2022 走看看