# POJ 3667 - Hotel

64人阅读 评论(0)

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 6
1 3
1 3
1 3
1 3
2 5 5
1 6


Sample Output

1
4
7
0
5


#include <stdio.h>

const int maxn = 50000;
int sum[maxn*3], lsum[maxn*3], rsum[maxn*3], sym[maxn*3];

void BuildTree(int left, int right, int root);
void UpData(int L, int R, int c, int left, int right, int root);
void PushDown(int root, int k);
void PushUp(int root, int k);
int query(int w, int left, int right, int root);
int Max(int x, int y)
{
return x>y ? x:y;
}

int main()
{
int n, mNum, op, a, b;
scanf("%d %d", &n, &mNum);

BuildTree(1, n, 1);
for (int i=1; i<=mNum; ++i)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%d", &a);
if (sum[1] < a)
printf("0\n");
else
{
int pos = query(a, 1, n, 1);
printf("%d\n", pos);
UpData(pos, pos+a-1, 1, 1, n, 1);
}
}
else
{
scanf("%d %d", &a, &b);
UpData(a, a+b-1, 0, 1, n, 1);
}
}
return 0;
}

void BuildTree(int left, int right, int root)
{
sym[root] = -1;
lsum[root] = rsum[root] = sum[root] = right-left+1;
if (left == right)
return;

int mid = (left+right)>>1;
BuildTree(left, mid, root << 1);
BuildTree(mid+1, right, root << 1|1);
}

int query(int w, int left, int right, int root)
{
if (left == right)
return 1;

PushDown(root, right-left+1);

int mid = (left+right)>>1;
if (w <= sum[root<<1])
return query(w, left, mid, root << 1);
else if (rsum[root<<1] + lsum[root<<1|1] >= w)
return mid - rsum[root<<1]+1;
else
return query(w, mid+1, right, root << 1|1);
}

void UpData(int L, int R, int c, int left, int right, int root)
{
if (L <= left && right <= R)
{
lsum[root] = rsum[root] = sum[root] = c ? 0 : right-left+1;
sym[root] = c;
return;
}

PushDown(root, right-left+1);

int mid = (right+left)>>1;
if (L <= mid)
UpData(L, R, c, left, mid, root << 1);
if (mid < R)
UpData(L, R, c, mid+1, right, root << 1|1);

PushUp(root, right-left+1);
}

void PushUp(int root, int k)
{
lsum[root] = lsum[root << 1];
rsum[root] = rsum[root << 1|1];

if (lsum[root] == k-(k>>1))
lsum[root] += lsum[root << 1|1];
if (rsum[root] == k>>1)
rsum[root] += rsum[root << 1];

sum[root] = Max(rsum[root << 1] + lsum[root << 1|1], Max(sum[root << 1], sum[root << 1|1]));
}

void PushDown(int root, int k)
{
if (sym[root] != -1)
{
sym[root << 1] = sym[root << 1|1] = sym[root];
lsum[root << 1] = rsum[root << 1] = sum[root << 1] = sym[root] ? 0:(k-(k>>1));
lsum[root << 1|1] = rsum[root << 1|1] = sum[root << 1|1] = sym[root] ? 0:(k>>1);
sym[root] = -1;
}
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：24999次
• 积分：1752
• 等级：
• 排名：千里之外
• 原创：157篇
• 转载：2篇
• 译文：0篇
• 评论：7条
文章分类
最新评论