鸿 网 互 联 www.68idc.cn

当前位置 : 主页 > 编程语言开发 > c++ > >

P2894 [USACO08FEB]酒店Hotel

来源:互联网 作者:佚名 时间:2017-09-20 09:33
题目描述 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 Cumberla

题目描述

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,被租为0
对于每一段区间l,r
1.保存从l向后的最长的为0的长度
2.保存从r向前的最长的为0的长度
3.保存整个区间里最长的为0的长度
4.保存区间的长度
对于每一次update,我们同时考虑左孩子的原空闲情况,右孩子的原空闲情况,以及他们拼在一起时中间部分的新的空闲情况

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #define lli long long int 
  6 #define ls k<<1
  7 #define rs k<<1|1
  8 using namespace std;
  9 const int MAXN=100001;
 10 inline void read(int &n)
 11 {
 12     char c='+';int x=0;bool flag=0;
 13     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
 14     while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}
 15     flag==1?n=-x:n=x;
 16 }
 17 struct node
 18 {
 19     lli l,r,w,f,lfree,rfree,allfree,chang;
 20 }tree[MAXN<<2];
 21 int n,m;
 22 inline void update(int k)
 23 {
 24     if(tree[ls].allfree==tree[ls].chang)
 25         tree[k].lfree=(tree[ls].chang+tree[rs].lfree);
 26     else
 27         tree[k].lfree=(tree[ls].lfree);
 28         
 29     if(tree[rs].allfree==tree[rs].chang)
 30         tree[k].rfree=(tree[rs].chang+tree[ls].rfree);
 31     else 
 32         tree[k].rfree=tree[rs].rfree;
 33         
 34     tree[k].allfree=max(tree[ls].allfree,tree[rs].allfree);
 35     tree[k].allfree=max(tree[k].allfree,tree[ls].rfree+tree[rs].lfree);
 36     
 37     return ;
 38 }
 39 inline void build_tree(int ll,int rr,int k)
 40 {
 41     tree[k].l=ll;tree[k].r=rr;
 42     tree[k].chang=(tree[k].r-tree[k].l+1);
 43     if(ll==rr)
 44     {    tree[k].lfree=tree[k].rfree=tree[k].allfree=(tree[k].r-tree[k].l+1);        return ;    }
 45     lli mid=(tree[k].l+tree[k].r)>>1;
 46     build_tree(ll,mid,ls);
 47     build_tree(mid+1,rr,rs);
 48     update(k);
 49 }
 50 inline void pushdown(int k,int how)
 51 {
 52     if(how==1)
 53     {
 54         tree[ls].lfree=tree[ls].rfree=tree[ls].allfree=0;
 55         tree[rs].lfree=tree[rs].rfree=tree[rs].allfree=0;
 56         tree[ls].f=tree[k].f;
 57         tree[rs].f=tree[k].f;
 58         tree[k].f=0;
 59     }
 60     else
 61     {
 62         tree[ls].lfree=tree[ls].rfree=tree[ls].allfree=tree[ls].chang;
 63         tree[rs].lfree=tree[rs].rfree=tree[rs].allfree=tree[rs].chang;
 64         tree[ls].f=tree[k].f;
 65         tree[rs].f=tree[k].f;
 66         tree[k].f=0;
 67     }
 68     return ;
 69 }
 70 inline int query(int k,int num)
 71 {
 72     if(tree[k].f)
 73         pushdown(k,tree[k].f);
 74     if(tree[k].r==tree[k].l)
 75         return tree[k].l;
 76     if(tree[ls].allfree>=num)
 77         return query(ls,num);
 78     if(tree[ls].rfree+tree[rs].lfree>=num)
 79         return tree[ls].r-tree[ls].rfree+1;
 80     else
 81         return query(rs,num);
 82 }
 83 inline void change(int k,int ll,int rr,int how)
 84 {
 85     if(ll<=tree[k].l&&tree[k].r<=rr)
 86     {
 87         if(how==1)
 88             tree[k].allfree=tree[k].lfree=tree[k].rfree=0;
 89         else
 90             tree[k].allfree=tree[k].lfree=tree[k].rfree=tree[k].chang;
 91     
 92         tree[k].f=how;
 93         return ;
 94     }
 95     int mid=(tree[k].l+tree[k].r)>>1;
 96     if(tree[k].f)
 97         pushdown(k,tree[k].f);
 98     if(ll<=mid)
 99         change(ls,ll,rr,how);
100     if(rr>mid)
101         change(rs,ll,rr,how);
102     update(k);
103 }
104 int main()
105 {
106     read(n);read(m);
107     build_tree(1,n,1);
108     for(int i=1;i<=m;i++)
109     {
110         int how;
111         read(how);
112         if(how==1)
113         {
114             int num;
115             read(num);
116             if(tree[1].allfree<num)
117             {
118                 printf("0\n");
119                 continue;
120             }
121             int pos=query(1,num);
122             change(1,pos,pos+num-1,1);// 放置 
123             printf("%d\n",pos);
124         }
125         else
126         {
127             int xx,yy;
128             read(xx);read(yy);
129             change(1,xx,xx+yy-1,2);//清空 
130         }
131     }
132     return 0;
133 }

 



网友评论
<