(线段树)POJ 3667-Hotel

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
  1 #include <iostream>
  2 //#include<bits/stdc++.h>
  3 #include <stack>
  4 #include <queue>
  5 #include <map>
  6 #include <set>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <math.h>
 11 using namespace std;
 12 //typedef long long ll;
 13 typedef unsigned long long ull;
 14 const int MAX=5e4+5;
 15 int N,M;
 16 struct node
 17 {
 18     int left,right,mid;//分别表示从左开始,区间内,从右开始最大长度
 19     int status;
 20     int ll,rr;//该区间为[ll,rr)
 21     int len()
 22     {
 23         return rr-ll;
 24     }
 25     void actlen()
 26     {
 27              left=right=mid=(status? 0:len() );
 28     }
 29 
 30 }st[10*MAX];
 31 void init(int l,int r,int k)
 32 {
 33     st[k].left=st[k].right=st[k].mid=r-l;
 34     st[k].status=0;
 35     st[k].ll=l;
 36     st[k].rr=r;
 37     if(l+1==r)
 38         return;
 39     init(l,(l+r)/2,2*k);
 40     init((l+r)/2,r,2*k+1);
 41 }
 42 void pushdown(int k)
 43 {
 44     if(st[k].status!=-1)
 45     {
 46         st[2*k].status=st[2*k+1].status=st[k].status;
 47         st[k].status=-1;
 48         st[2*k].actlen();
 49         st[2*k+1].actlen();
 50     }
 51 }
 52 int query(int ch,int k)//寻找长度大于等于ch的区间
 53 {
 54     if(st[k].ll+1==st[k].rr&&ch==1)
 55         return st[k].ll;
 56     if(st[k].mid<ch)//就算是叶子结点ch!-1的情况也一样被排除了
 57         return 0;
 58     pushdown(k);
 59     if(st[2*k].mid>=ch)//如果左结点大于
 60     {
 61         return query(ch,2*k);
 62     }
 63     else if(st[2*k].right+st[2*k+1].left>=ch)
 64     {
 65         return st[2*k].rr-st[2*k].right;
 66     }
 67     else if(st[2*k+1].mid>=ch)
 68         return query(ch,2*k+1);
 69     return 0;
 70 }
 71 void update(int l,int r,int sc,int k)//将[l,r)区间的值更新为sc
 72 {
 73     if(st[k].ll==l&&st[k].rr==r)
 74     {
 75         st[k].status=sc;
 76         st[k].actlen();
 77         return;
 78     }
 79     pushdown(k);
 80     if(st[k].ll+1==st[k].rr)
 81         return;
 82     int mid=(st[k].ll+st[k].rr)/2;
 83     if(mid<=l)
 84         update(l,r,sc,2*k+1);
 85     else
 86     {
 87         if(mid>=r)
 88             update(l,r,sc,2*k);
 89         else
 90         {
 91             update(l,mid,sc,2*k);
 92             update(mid,r,sc,2*k+1);
 93         }
 94     }
 95     st[k].mid=max(st[2*k].mid,st[2*k+1].mid);
 96     st[k].left=st[2*k].left;
 97     st[k].right=st[2*k+1].right;
 98     st[k].mid=max(st[k].mid,st[2*k].right+st[2*k+1].left);
 99     if(st[2*k].left==st[2*k].len())
100         st[k].left+=st[2*k+1].left;
101     if(st[2*k+1].right==st[2*k+1].len())
102         st[k].right+=st[2*k].right;
103     return;
104 }
105 int main()
106 {
107     scanf("%d %d",&N,&M);
108     int demand;
109     int xx,yy;
110     init(1,N+1,1);
111     for(int i=1;i<=M;i++)
112     {
113         scanf("%d",&demand);
114         if(demand==1)
115         {
116             scanf("%d",&xx);
117             yy=query(xx,1);
118             printf("%d
",yy);
119             if(yy)
120                 update(yy,yy+xx,1,1);
121         }
122         else
123         {
124             scanf("%d %d",&xx,&yy);
125             update(xx,xx+yy,0,1);
126         }
127 
128     }
129 }
原文地址:https://www.cnblogs.com/quintessence/p/6431243.html