poj 3667 Hotel (线段树 + 合并操作)

来源:互联网 发布:和女生网络聊天技巧 编辑:程序博客网 时间:2024/05/03 08:32

poj 3667  Hotel  (线段树 + 合并操作)


                                                                                                                                               
Hotel
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 13372 Accepted: 5793

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 numbersr..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 ofr to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi andDi which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤XiN-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 andDi (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 integerr, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 61 31 31 31 32 5 51 6

Sample Output

14705

Source

USACO 2008 February Gold






题意:这里有一家旅馆,房间是在单独一行。现在有队伍想住进来,同时要求房间连号,而老板总是会选择从符合条件的连续房间,号码最靠前的给他们。
最开始有n,m两个数,表示有连续的1~n个房间,有m次操作
接下来是两种操作:
如果是1,那么后面跟一个数字a,表示一个队伍要住店,要连续的a间房。
房间能连续,并且尽量靠前的房间,输出头一个房间的编号

如果是2,则表示有一个队伍离开了旅馆,后面跟两个数字a,b,表示从a房间开始,总共有b间连续的房间空了出来



题解:这是一道很经典的线段树合并操作。我们要经常进行信息更新操作,所以在每个结点要保持充足的信息,方便更新父亲结点和孩子结点。
1表示房间有人住
0表示空房间
l,r为该个结点的最左和最右
llen为从最左边开始,最长的连续房间数,如[0,0,1,1,1],则llen为2,如[1,0,0,0,0],则llen为0
rlen为从最右边开始,最长的连续房间数,与上述同理;
tlen为该范围内最长的连续房间数,[0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0],则tlen为4,中间有可能有很多段,找出其中最长的

每次入住房间和退房时,都要更新房间的信息

通过mark来标记,就是线段树的覆盖操作,不断的更新房间的最长个数


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <algorithm>#include <iostream>using namespace std;#define maxn 50005struct fun{int l, r;int mark;int llen, rlen, tlen;}tree[maxn << 2];                //四倍是很不错的长度int max (int a, int b){if (a > b) return a;return b;}void PushUp (int rt)                    //从下往上更新,从孩子结点中寻找不同的连续房间{tree[rt].llen = tree[rt << 1].llen;                     //如果左孩子的llen最长连续为整个左孩子,那么就加上右孩子的llenif (tree[rt << 1].llen == (tree[rt << 1].r - tree[rt << 1].l + 1))tree[rt].llen = (tree[rt << 1].llen + tree[rt << 1 | 1].llen);tree[rt].rlen = tree[rt << 1 | 1].rlen;              //如果右孩子的rlen最长连续为整个右孩子,那么就加上左孩子的rlenif (tree[rt << 1 | 1].rlen == (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1))tree[rt].rlen = (tree[rt << 1 | 1].rlen + tree[rt << 1].rlen);tree[rt].tlen = max(max (tree[rt << 1].tlen, tree[rt << 1 | 1].tlen), tree[rt << 1].rlen + tree[rt << 1 | 1].llen);   //整个结点最长有三种可能性,选择其中最长的一种}void PushDown (int rt)           //从上往下更新,从父亲结点的mark标记中来更新孩子{if (tree[rt].mark != -1){tree[rt << 1].mark = tree[rt << 1 | 1].mark = tree[rt].mark;       //将父亲的mark延续到孩子tree[rt].mark = -1;               // 释放父亲的标记tree[rt << 1].llen = tree[rt << 1].rlen = tree[rt << 1].tlen = tree[rt << 1].mark ? 0 : (tree[rt << 1].r - tree[rt << 1].l + 1);      //因为这个范围都在入住或者退房的范围内,所以该结点要么全空,要么全满tree[rt << 1 | 1].llen = tree[rt << 1 | 1].rlen = tree[rt << 1 | 1].tlen = tree[rt << 1 | 1].mark ? 0 : (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);      //同上述一样}}void BuildTree (int l, int r, int rt)          //建立线段树,一棵二叉树{int m = (l + r) >> 1;tree[rt].l = l;tree[rt].r = r;tree[rt].llen = tree[rt].rlen = tree[rt].tlen = r - l + 1;tree[rt].mark = -1;if (l == r) return ;BuildTree(l, m, rt << 1);BuildTree(m + 1, r, rt << 1 | 1);return ;} void UpData (int L, int R, int v, int rt)               //更新结点的房间信息{if (tree[rt].l >= L && tree[rt].r <= R)          //找到要改变信息的范围结点{ tree[rt].mark = v;                         //更新其标记mark,通过之后的PushDown来修改其它结点tree[rt].llen = tree[rt].rlen = tree[rt].tlen = tree[rt].mark ? 0:(tree[rt].r - tree[rt].l + 1);      //PushDown不会循环到,所以自己更新信息return ;}PushDown (rt);int m = (tree[rt].l + tree[rt].r) >> 1;if (L <= m) UpData (L, R, v, rt << 1);if (R > m) UpData (L, R, v, rt << 1 | 1);PushUp (rt);            //往上的更新}int Query (int rt, int len)       //查找符合连续,输出最小的编号{if (tree[rt].l == tree[rt].r)return tree[rt].l;PushDown (rt);if (tree[rt << 1].tlen >= len)        //先比较左孩子是否符合,要是符合就从进入左孩子,不断从左开始考虑Query (rt << 1, len);else if ((tree[rt << 1].rlen + tree[rt << 1 | 1].llen) >= len)      //要是左孩子的llen+rlen大于要求的长度,怎表面无法再寻找左孩子了,这个就是最前面的点了return (tree[rt << 1].r - tree[rt << 1].rlen + 1);             else Query (rt << 1|1, len);}int main (){int n, m, i, x, a, b, begin;while (scanf ("%d%d", &n, &m) != EOF){BuildTree (1, n, 1);for (i = 0; i < m; ++i){scanf ("%d", &x);if (x == 1){scanf ("%d", &a);if (tree[1].tlen < a)        //如果全部范围内最长的连续房间都小于要求的,则直接输出0 printf ("0\n");else{begin = Query (1, a);printf ("%d\n", begin);UpData (begin, begin + a - 1, 1, 1);     //查找到符合条件的房间后,要进行更新房间信息}}else{scanf ("%d%d", &a, &b);UpData (a, a + b - 1, 0, 1);}}}return 0;}


0 0
原创粉丝点击