Hotel
Time Limit: 3000MS |
Memory Limit: 65536K |
|
|
|
Description
The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agent, has named theBullmoose Hotel on famed Cumberland Street as their vacation residence. Thisimmense hotel has N (1 ≤ N ≤ 50,000) roomsall located on the same side of an extremely long hallway (all the better tosee the lake, of course).
The cows and other visitors arrive ingroups of size Di (1 ≤ Di ≤N) and approach the front desk to check in. Each group i requestsa set of Di contiguous rooms from Canmuu, the moosestaffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1if they are available or, if no contiguous set of rooms is available, politely suggestsalternate lodging. Canmuu always chooses the value of r to bethe smallest possible.
Visitors also depart the hotel from groupsof contiguous rooms. Checkout i has the parameters Xi and Di whichspecify 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 isinitially unoccupied.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as oneof two possible formats: (a) Two space separated integers representing acheck-in request: 1 and Di (b) Threespace-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 thecontiguous sequence of rooms to be occupied. If the request cannot besatisfied, 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
有一个Hotel,有N个连续的房间,开始的时候没有人住,下面有M次‘1’或‘2’操作。操作“1 D”表示有D个人进来选房间,他们必须是住在连号的房间,如果不满足这个条件,输出“0”,否则,输出他们这几个人中房间号码最小的一个;操作“2 X D”表示Hotel从房间号X开始D个房间被清空。
其实操作‘1’可以看成是两步,首先,我得找到[1,N]区间中是否包含有长度为D连续的未被占用的房间的区间,并且返回区间的首元素。
我的方法是线段树区间合并,由于在我另外一篇博客《hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】》中有讲到类似的做法,我这里不累赘太多,只不过这个题目相对复杂一点,主要是多了一个Lazy-tag的思想。
线段树题目的类型大致可以分为四种:单点更新、成段增减或更新、区间合并和扫描线
成段更新和区间合并都需要用到Lazy-tag思想。
扫描线就是求矩形面积和周长的题目,需要用到离散化。
本篇讲解区间合并,区间合并肯定是从子节点向上才能用着合并,这类题目都是求最长连续区间的,主要在PushUp的时候需要对左右儿子的区间进行合并。
设对于每个节点rt,它所管辖的范围是 [l,r],我这里用相似的对每一个节点用ln,rn,mn 分别来表示在[l,r] 中以l 开始的连续未被占用的区间长度,以 r 结尾的连续且未被占用的区间长度,以及[l,r]这个区间最大的连续未被占用的区间长度。注意,我这里都是指未被占用哦~ 【具体区间合并的讲解,请看《hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】》中的讲解~~~
这个题目,需要对区间进行更新,区间更新 <--> 延迟更新(Lazy-tag);那么我这里的tag数组就是用来延迟更新的,tag[rt]= -1 表示清除标记时的状态, tag[rt] =0 表示将区间全部占用时的状态,tag[rt] = 1 表示区间为空的状态,一般来说,更新的状态有i种,就对应着tag[rt] 的值就有i+1 种,因为其中一种是用来清除标记的~ O(∩_∩)O哈哈~
/****************************>>>>HEADFILES<<<<****************************/ #include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <sstream> #include <algorithm> using namespace std; /****************************>>>>>DEFINE<<<<<*****************************/ #define fst first #define snd second #define root 1,N,1 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define PB(a) push_back(a) #define MP(a,b) make_pair(a,b) #define CASE(T) for(scanf("%d",&T);T--;) #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) //#pragma comment(linker, "/STACK:1024000000,1024000000") typedef __int64 LL; const int INF = 0x3f3f3f3f; /****************************>>>>SEPARATOR<<<<****************************/ const int maxn = 50000 + 5; int N, M; struct Node { int ln, rn, mn; } segtree[maxn << 2];// Have not been occupied int tag[maxn << 2];//-1 without tag 0 FULL 1 Empty inline void PushUp(const int& rt, const int& l, const int& r) { segtree[rt].ln = segtree[rt << 1].ln; segtree[rt].rn = segtree[rt << 1 | 1].rn; segtree[rt].mn = max(segtree[rt << 1].rn + segtree[rt << 1 | 1].ln, max(segtree[rt << 1].mn, segtree[rt << 1 | 1].mn)); int mid = (l + r) >> 1; if(segtree[rt << 1].mn == mid - l + 1) segtree[rt].ln += segtree[rt << 1 | 1].ln; if(segtree[rt << 1 | 1].mn == r - (mid + 1) + 1) segtree[rt].rn += segtree[rt << 1].rn; } inline void PushDown(const int& rt, const int& l, const int& r) { if(tag[rt] != -1) { tag[rt << 1] = tag[rt << 1 | 1] = tag[rt]; int mid = (l + r) >> 1; segtree[rt << 1].ln = segtree[rt << 1].rn = segtree[rt << 1].mn = tag[rt] * (mid - l + 1); segtree[rt << 1 | 1].ln = segtree[rt << 1 | 1].rn = segtree[rt << 1 | 1].mn = tag[rt] * (r - (mid + 1) + 1); tag[rt] = -1; } } void Build(int l, int r, int rt) { segtree[rt].ln = segtree[rt].rn = segtree[rt].mn = r - l + 1; tag[rt] = 1; if(l == r) return ; int mid = (l + r) >> 1; Build(lson); Build(rson); } void Update(int L, int R, const bool& isClear, int l, int r, int rt) { if(L <= l && r <= R) { if(isClear) { segtree[rt].ln = segtree[rt].rn = segtree[rt].mn = r - l + 1; tag[rt] = 1; } else { segtree[rt].ln = segtree[rt].rn = segtree[rt].mn = 0; tag[rt] = 0; } return; } PushDown(rt, l, r); int mid = (l + r) >> 1; if(L <= mid) Update(L, R, isClear, lson); if(mid < R) Update(L, R, isClear, rson); PushUp(rt, l, r); } int Query(const int& Len, int l, int r, int rt) { if(segtree[rt].ln >= Len) return l; int mid = (l + r) >> 1; if(segtree[rt << 1].mn >= Len) return Query(Len, lson); else if(segtree[rt << 1].rn + segtree[rt << 1 | 1].ln >= Len) return mid - segtree[rt << 1].rn + 1; else return Query(Len, rson); } int Op, X, D, ans; int main() { // FIN; while(~scanf("%d %d", &N, &M)) { Build(root); while(M--) { scanf("%d", &Op); if(Op & 1) { scanf("%d", &D); if(segtree[1].mn < D) { printf("0\n"); continue; } ans = Query(D, root); printf("%d\n", ans); Update(ans, ans + D - 1, false, root); } else { scanf("%d %d", &X, &D); Update(X, X + D - 1, true, root); } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】
原文地址:http://blog.csdn.net/acmore_xiong/article/details/47403479