• POJ P3667 Hotel——solution


    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.

                                  --by POJ

    http://poj.org/problem?id=3667



    给定初始值全0的区间[1,n],支持两种操作:
    1 w //查询整个区间最左端长度为w的连续0区间,输出其左端点,并把她全变成1,不存在,则输出0
    2 l len //把以l为左端点长度为len的区间清零
     
    考虑线段树维护区间:
    max:区间最长连续0;
    lmax:区间左起最长连续0;
    rmax:区间右起最长连续0;
    然后把1操作拆成先查询再在此基础上修改,这样修改可与2操作共用函数
    查询时
    优先递归左半区间,
    左区间max非法,则查询左区间rmax+右区间lmax,
    再非法,则递归右半区间,
    再非法,则返回0,
    修改则走线段树正常区间修改流程,打标记什么的;
    注意如果查询失败,记得不能进行修改;
    然后,如果查到了单点,记得特判一下;
    一开始,我想当然的想为1操作定义一个函数,为2操作定义一个函数;
    这样第一个函数一边查询一边修改
    然而这样就需要打各种各样修改位置不同的标记——因为查询的递归与修改的递归流程很不相似
    废了好久,
    然后才知道可以这样定函数,
    重构,然后清晰了许多;
    代码能力不够啊;
    代码如下:
     1 #include<cstdio>
     2 using namespace std;
     3 int n,m,L,R;
     4 struct tree{
     5     int lmax,rmax,max,lz;
     6 }line[200010];
     7 void builine(int ,int ,int );
     8 void up(int ,int ,int );
     9 void down(int ,int ,int );
    10 int  search(int ,int ,int ,int );
    11 void change(int ,int ,int ,int );
    12 int main()
    13 {
    14     int i,j,k,ans;
    15     scanf("%d%d",&n,&m);
    16     builine(1,n,1);
    17     for(i=1;i<=m;i++){
    18         scanf("%d",&j);
    19         if(j==1){
    20             scanf("%d",&k);
    21             ans=search(1,n,1,k);
    22             printf("%d
    ",ans);
    23             L=ans;R=L+k-1;
    24             if(L)
    25                 change(1,n,1,1);
    26         }
    27         else{
    28             scanf("%d%d",&L,&k);
    29             R=L+k-1;
    30             change(1,n,1,0);
    31         }
    32     }
    33 }
    34 void builine(int l,int r,int nu){
    35     if(l==r){
    36         line[nu].lmax=line[nu].rmax=line[nu].max=1;
    37         return ;
    38     }
    39     int mid=(l+r)>>1;
    40     builine(l,mid,nu<<1);
    41     builine(mid+1,r,nu<<1|1);
    42     line[nu].lmax=line[nu].rmax=line[nu].max=line[nu<<1].max+line[nu<<1|1].max;
    43 }
    44 void up(int l,int r,int nu){
    45     int mid=(l+r)>>1;
    46     if(line[nu<<1].max>line[nu<<1|1].max)
    47         line[nu].max=line[nu<<1].max;
    48     else
    49         line[nu].max=line[nu<<1|1].max;
    50     if(line[nu].max<line[nu<<1].rmax+line[nu<<1|1].lmax)
    51         line[nu].max=line[nu<<1].rmax+line[nu<<1|1].lmax;
    52     line[nu].lmax=line[nu<<1].lmax==(mid-l+1)?line[nu<<1].lmax+line[nu<<1|1].lmax:line[nu<<1].lmax;
    53     line[nu].rmax=line[nu<<1|1].rmax==(r-mid)?line[nu<<1|1].rmax+line[nu<<1].rmax:line[nu<<1|1].rmax;
    54 }
    55 void down(int l,int r,int nu){
    56     int mid=(l+r)>>1;
    57     if(line[nu].lz){
    58         line[nu<<1].lmax=line[nu<<1].max=line[nu<<1].rmax=((line[nu].lz-1)^1)*(mid-l+1);
    59         line[nu<<1|1].lmax=line[nu<<1|1].max=line[nu<<1|1].rmax=((line[nu].lz-1)^1)*(r-mid);
    60         line[nu<<1].lz=line[nu<<1|1].lz=line[nu].lz;
    61         line[nu].lz=0;
    62     }
    63 }
    64 int  search(int l,int r,int nu,int len){
    65     int mid=(l+r)>>1,ans;
    66     down(l,r,nu);
    67     if(l==r)
    68         return line[nu].max*l;
    69     if(line[nu<<1].max>=len){
    70         ans=search(l,mid,nu<<1,len);
    71         return ans;
    72     }
    73     if(line[nu<<1].rmax+line[nu<<1|1].lmax>=len){
    74         ans=mid-line[nu<<1].rmax+1;
    75         return ans;
    76     }
    77     if(line[nu<<1|1].max>=len){
    78         ans=search(mid+1,r,nu<<1|1,len);
    79         return ans;
    80     }
    81     return 0;
    82 }
    83 void change(int l,int r,int nu,int x){
    84     if(L<=l&&r<=R){
    85         line[nu].lmax=line[nu].rmax=line[nu].max=(r-l+1)*(x^1);
    86         line[nu].lz=x+1;
    87         return ;
    88     }
    89     down(l,r,nu);
    90     int mid=(l+r)>>1;
    91     if(L<=mid)
    92         change(l,mid,nu<<1,x);
    93     if(R>mid)
    94         change(mid+1,r,nu<<1|1,x);
    95     up(l,r,nu);
    96 }

    祝AC

  • 相关阅读:
    redis--pipelining管道
    插入排序
    选择排序
    冒泡排序
    网页表格导入导出Excel
    easyUI的datagrid表格的使用
    软件工程实践总结(个人)
    Beta答辩总结
    Beta冲刺7
    Beta冲刺6
  • 原文地址:https://www.cnblogs.com/nietzsche-oier/p/6650168.html
Copyright © 2020-2023  润新知