【POJ3667】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 D(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

Source

题目大意:有n个房间m个操作,每次操作有两中可能1.读入:1 a,查找长度为a的连续的房间(每个房间都是可入住的),输出最左端的编号,这长度为a的每个房间都入住即不能再用。2.读入2 a b,退房操作,以a为起点的b个房间都变为可入住。
做法:线段树区间合并模板,每个节点储存连续的最大值,两端区间合并时左区间的右端和右区间的左端也可以成为最大值。
注意:数据略水,可能没人入住就退房= =,然并卵。
第一次认真的对拍,对着一个ac的程序不断拍,第一次:updata时左儿子打成右儿子了,30min后,忘了把改完的程序换到对拍的文件夹了,。。。a了。
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #define N 100000
  5 using namespace std;
  6 struct data{int sl,sr,maxs,ls,rs;}seg[N*4];
  7 int lazy[N*4];
  8 int n,m;
  9 void pushsign(int now)
 10 {
 11     if (lazy[now]==1)
 12     {
 13         seg[(now<<1)].maxs=seg[(now<<1)].ls=seg[(now<<1)].rs=seg[(now<<1)].sr-seg[(now<<1)].sl+1;
 14         lazy[(now<<1)]=lazy[now];
 15         seg[(now<<1)+1].maxs=seg[(now<<1)+1].ls=seg[(now<<1)+1].rs=seg[(now<<1)+1].sr-seg[(now<<1)+1].sl+1;
 16         lazy[(now<<1)+1]=lazy[now];
 17         lazy[now]=0;
 18     }
 19     else    if (lazy[now]==2)
 20     {
 21         seg[(now<<1)].maxs=seg[(now<<1)].ls=seg[(now<<1)].rs=0;
 22         lazy[(now<<1)]=lazy[now];
 23         seg[(now<<1)+1].maxs=seg[(now<<1)+1].ls=seg[(now<<1)+1].rs=0;
 24         lazy[(now<<1)+1]=lazy[now];
 25         lazy[now]=0;
 26     }
 27 }
 28 void adddata(int now)
 29 {
 30     seg[now].maxs=seg[(now<<1)].rs+seg[(now<<1)+1].ls;
 31     seg[now].maxs=max(max(seg[(now<<1)].maxs,seg[(now<<1)+1].maxs),seg[now].maxs);
 32     seg[now].ls=seg[(now<<1)].ls;
 33     if (seg[(now<<1)].ls==seg[(now<<1)].sr-seg[(now<<1)].sl+1)    seg[now].ls+=seg[(now<<1)+1].ls;
 34     seg[now].rs=seg[(now<<1)+1].rs;
 35     if (seg[(now<<1)+1].rs==seg[(now<<1)+1].sr-seg[(now<<1)+1].sl+1)    seg[now].rs+=seg[(now<<1)].rs;
 36 }
 37 void buildtree(int now,int l,int r)
 38 {    seg[now].sl=l;    seg[now].sr=r;    seg[now].maxs=seg[now].ls=seg[now].rs=r-l+1;
 39     if (l==r)    return;
 40     int mid=(l+r)>>1;
 41     buildtree((now<<1),l,mid);
 42     buildtree((now<<1)+1,mid+1,r);
 43     adddata(now);
 44 }
 45 int query(int now,int lon)
 46 {
 47     int l=seg[now].sl,r=seg[now].sr;
 48     if (l==r)    return l;
 49     pushsign(now);
 50      int pos=0;
 51     if (seg[(now<<1)].ls>=lon)    return seg[(now<<1)].sl;
 52     else    if (seg[(now<<1)].maxs>=lon)    pos=query((now<<1),lon);
 53     else    if (seg[(now<<1)].rs+seg[(now<<1)+1].ls>=lon &&seg[(now<<1)].rs!=0)    return seg[(now<<1)].sr-seg[(now<<1)].rs+1;
 54     else    if (seg[(now<<1)+1].maxs>=lon)pos=query((now<<1)+1,lon);
 55     else    if (seg[now].maxs==r-l+1)    return l;
 56     return pos;
 57 }
 58 void ichange1(int now,int begin,int end)
 59 {
 60     int l=seg[now].sl,r=seg[now].sr;
 61     if (begin<=l && end>=r)    {seg[now].maxs=seg[now].ls=seg[now].rs=r-l+1;    lazy[now]=1;    return;}
 62     pushsign(now);
 63     int mid=(l+r)>>1;
 64     if (begin<=mid)    ichange1((now<<1),begin,end);
 65     if (end>mid)    ichange1((now<<1)+1,begin,end);
 66     adddata(now);
 67 }
 68 void ichange2(int now,int begin,int end)
 69 {
 70     int l=seg[now].sl,r=seg[now].sr;
 71     if (begin<=l && end>=r)    {seg[now].maxs=seg[now].ls=seg[now].rs=0;    lazy[now]=2;    return;}
 72     pushsign(now);
 73     int mid=(l+r)>>1;
 74     if (begin<=mid)    ichange2((now<<1),begin,end);
 75     if (end>mid)    ichange2((now<<1)+1,begin,end);
 76     adddata(now);
 77 }
 78 int main()
 79 {
 80     int i;
 81     int a,b,p;
 82     while (~scanf("%d%d",&n,&m))
 83     {
 84     buildtree(1,1,n);
 85     for (i=1;i<=m;i++)
 86     {
 87         scanf("%d",&p);
 88         if (p==1)
 89         {
 90             scanf("%d",&a);
 91             if (seg[1].maxs<a)    {printf("0
");continue;}
 92             int pos=query(1,a);
 93             printf("%d
",pos);
 94             ichange2(1,pos,pos+a-1);
 95         }
 96         else
 97         {
 98             scanf("%d%d",&a,&b);
 99             ichange1(1,a,a+b-1);
100         }
101     }    
102     }
103     //system("pause");
104     return 0;
105 }
—Anime Otaku Save The World.
原文地址:https://www.cnblogs.com/DMoon/p/5137897.html