【bzoj1593/Usaco2008 Feb】Hotel 旅馆——线段树

Description

奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。

Input

* 第1行: 2个用空格隔开的整数:N、M

* 第2..M+1行: 第i+1描述了第i个请求,如果它是一个订房请求,则用2个数字 1、D_i描述,数字间用空格隔开;如果它是一个退房请求,用3 个以空格隔开的数字2、X_i、D_i描述

Output

* 第1..??行: 对于每个订房请求,输出1个独占1行的数字:如果请求能被满足 ,输出满足条件的最小的r;如果请求无法被满足,输出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
 

 线段树的每个点维护左端点起空房长度,右端点起长度,跨越中点长度以及区间内最大长度。
订房操作相当于询问最左端的大于请求长度的段的左端点;而退房操作相当于把一段区间set为空。
细节比较多,建议看代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 const int N=5e4+10;
 5 int n,m,a,b;
 6 using std::max;
 7 struct node{
 8     int len,l,r,se,lenl,lenr;
 9 }e[N*4];
10 int read(){
11     int ans=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){ans=ans*10+c-48;c=getchar();}
14     return ans*f;
15 }
16 void build(int rt,int l,int r){
17     e[rt].lenl=e[rt].lenr=e[rt].len=r-l+1;e[rt].se=0;e[rt].l=l;e[rt].r=r;
18     if(l==r)return;
19     int mid=(l+r)>>1;
20     build(rt<<1,l,mid);
21     build((rt<<1)+1,mid+1,r);
22 }
23 void down(int x){
24     int l=e[x].l,r=e[x].r;int p=e[x].se;e[x].se=0;
25     if(l==r)return;
26     if(p==2){
27         e[(x<<1)].lenl=e[x<<1].lenr=e[x<<1].len=e[x<<1].r-e[x<<1].l+1;
28         e[(x<<1)+1].lenl=e[(x<<1)+1].lenr=e[(x<<1)+1].len=e[(x<<1)+1].r-e[(x<<1)+1].l+1;
29         e[x<<1].se=e[(x<<1)+1].se=2;
30     }
31     else if(p){
32         e[x<<1].lenl=e[x<<1].lenr=e[x<<1].len=0;
33         e[(x<<1)+1].lenl=e[(x<<1)+1].lenr=e[(x<<1)+1].len=0;
34         e[x<<1].se=e[(x<<1)+1].se=1;
35     }
36 }
37 void up(int rt){
38     if(e[rt<<1].len==e[rt<<1].r-e[rt<<1].l+1)
39         e[rt].lenl=e[rt<<1].len+e[(rt<<1)+1].lenl;
40     else e[rt].lenl=e[rt<<1].lenl;
41     if(e[(rt<<1)+1].len==e[(rt<<1)+1].r-e[(rt<<1)+1].l+1)
42         e[rt].lenr=e[(rt<<1)+1].len+e[rt<<1].lenr;
43     else e[rt].lenr=e[(rt<<1)+1].lenr;
44     e[rt].len=max(e[rt<<1].lenr+e[(rt<<1)+1].lenl,max(e[rt<<1].len,e[(rt<<1)+1].len));
45 }
46 void set(int rt,int p){
47     down(rt);
48     int l=e[rt].l,r=e[rt].r;
49     if(a<=l&&b>=r){
50         e[rt].se=p;e[rt].len=e[rt].lenl=e[rt].lenr=p==2?r-l+1:0;
51         return;
52     }
53     if(a>r||b<l)return;
54     set(rt<<1,p);set((rt<<1)+1,p);
55     up(rt);
56 }
57 int ask(int k,int x){
58     down(k);
59     int l=e[k].l,r=e[k].r,mid=(l+r)>>1;
60     if(l==r)return l;
61     if(e[k<<1].len>=x)return ask(k<<1,x);
62     if(e[k<<1].lenr+e[(k<<1)+1].lenl>=x)return mid-e[k<<1].lenr+1;
63     return ask((k<<1)+1,x);
64 }
65 int main(){
66     n=read();m=read();
67     build(1,1,n);
68     while(m--){
69         int p=read();
70         if(p-1){
71             a=read(),b=read();
72             b=a+b-1;set(1,2);
73         }
74         else{
75             int x=read();if(e[1].len<x){printf("0\n");continue;}
76             int pp=ask(1,x);a=pp;b=pp+x-1;printf("%d\n",pp);
77             set(1,1);
78         }
79     }
80     return 0;
81 }
82 
bzoj1593

 

 
posted @ 2017-10-14 08:00  Child-Single  阅读(223)  评论(0编辑  收藏  举报