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 ≤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 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
题意:在一个旅馆里申请房间,第一行两个数字 N,M。表示有 N 个房间,M 次操作。下面有 M 行,每行两个数字,前一个数字1是申请房间,后面的数字是申请的房间数目,并且返回申请房间的第一个的房间号,申请的房间必须是连续的。如果是2,后面有两个数字,表示从 a 到 b 的房间都清空。
只要设计好线段树,sum数组表示这段中最多的连续的房间,lsum表示从左边开始连续的房间有多少个,rsum则是从右边。sym用来记录一共有多少空房间。
每次申请房间时,看从左边能否直接申请到连续的房间,再看左边的右半段和右边的左半段合起来能否满足要求,最后是右边剩下的。
#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;
}
}