zoukankan      html  css  js  c++  java
  • POJ 3667 Hotel (线段树)

    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
      1 #include<cstdio>
      2 #include<cstring>
      3 #define max(a,b) (a>b?a:b)
      4 using namespace std;
      5 struct f
      6 {
      7     int x,y,z,v;
      8 };f tree[200200];
      9 void build(int l,int r,int p)
     10 {
     11     tree[p].x=tree[p].y=tree[p].z=r-l+1;
     12     tree[p].v=-1;
     13     if (l==r) return ;
     14     int m=(l+r)/2;
     15     build(l,m,p*2);
     16     build(m+1,r,p*2+1);
     17 }
     18 void pushdown(int x,int p)
     19 {
     20     if (tree[p].v!=-1)
     21     {
     22         tree[p*2].v=tree[p*2+1].v=tree[p].v;
     23         if (tree[p].v==0)
     24         {
     25             tree[p*2].x=tree[p*2].y=tree[p*2].z=x-(x/2);
     26             tree[p*2+1].x=tree[p*2+1].y=tree[p*2+1].z=x/2;
     27         }
     28         else
     29         {
     30             tree[p*2].x=tree[p*2].y=tree[p*2].z=0;
     31             tree[p*2+1].x=tree[p*2+1].y=tree[p*2+1].z=0;
     32         }
     33         tree[p].v=-1;
     34     }
     35 }
     36 void pushup(int x,int p)
     37 {
     38     tree[p].x=tree[p*2].x;
     39     tree[p].y=tree[p*2+1].y;
     40     if (tree[p].x==x-(x/2)) tree[p].x+=tree[p*2+1].x;
     41     if (tree[p].y==x/2) tree[p].y+=tree[p*2].y;
     42     tree[p].z=max(tree[p*2].y+tree[p*2+1].x,max(tree[p*2].z,tree[p*2+1].z));
     43 }
     44 int find(int l,int r,int w,int p)
     45 {
     46     if (l==r) return l;
     47     pushdown(r-l+1,p);
     48     int m=(l+r)/2;
     49     if (tree[p*2].z>=w) return find(l,m,w,p*2);
     50     if (tree[p*2].y+tree[p*2+1].x>=w) return m-tree[p*2].y+1;
     51     return find(m+1,r,w,p*2+1);
     52 }
     53 void un(int ll,int rr,int l,int r,int p,int w)
     54 {
     55     if (ll<=l&&rr>=r)
     56     {
     57         if (w) tree[p].x=tree[p].y=tree[p].z=0;
     58         else tree[p].x=tree[p].y=tree[p].z=r-l+1;
     59         tree[p].v=w;
     60         return ;
     61     }
     62     pushdown(r-l+1,p);
     63     int m=(l+r)/2;
     64     if (m<ll) un(ll,rr,m+1,r,p*2+1,w);
     65     else if (m>=rr) un(ll,rr,l,m,p*2,w);
     66     else
     67     {
     68         un(ll,m,l,m,p*2,w);
     69         un(m+1,rr,m+1,r,p*2+1,w);
     70     }
     71     pushup(r-l+1,p);
     72 }
     73 int main()
     74 {
     75     int n,m,a,b;
     76     while (~scanf("%d%d",&n,&m))
     77     {
     78         build(1,n,1);
     79         while (m--)
     80         {
     81             scanf("%d",&a);
     82             if (a==1)
     83             {
     84                 scanf("%d",&b);
     85                 if (b>tree[1].z)
     86                 {
     87                     printf("0
    ");
     88                     continue;
     89                 }
     90                 a=find(1,n,b,1);
     91                 printf("%d
    ",a);
     92                 un(a,b+a-1,1,n,1,1);
     93             }
     94             else
     95             {
     96                 scanf("%d%d",&a,&b);
     97                 un(a,b+a-1,1,n,1,0);
     98             }
     99         }
    100     }
    101 }
  • 相关阅读:
    关于c语言的左移运算
    如何修改mac的root密码
    LaTeX语法笔记
    texlive测试是否安装成功
    latex学习(四)tlmgr
    latex学习(三)
    latex学习(二)
    Latex学习(一)
    引导修复软件boot-repair
    MySQL远程连接和备份还原
  • 原文地址:https://www.cnblogs.com/pblr/p/4732579.html
Copyright © 2011-2022 走看看