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 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 Di (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>
#include <cstdio>
#include <cstring>
using namespace std;
struct Node
{
int l,r;
int ls,rs,ms;
int lazy;
};
Node arr[2000006];
void pushUp(int k)
{
int li = k << 1;
int ri = (k << 1) | 1;
arr[k].ls = arr[li].ls;
arr[k].rs = arr[ri].rs;
arr[k].ms = max(arr[k << 1].ms,arr[(k << 1) | 1].ms);
if(arr[li].rs != 0 && arr[ri].ls != 0)
{
if(arr[li].ls == arr[li].r - arr[li].l + 1)
{
arr[k].ls += arr[ri].ls;
}
if(arr[ri].rs == arr[ri].r - arr[ri].l + 1)
{
arr[k].rs += arr[li].rs;
}
arr[k].ms = max(arr[k].ms,arr[li].rs + arr[ri].ls);
}
}
void pushDown(int k)
{
if(arr[k].lazy != -1)
{
int li = k << 1;
int ri = (k << 1) | 1;
int llen = arr[li].r - arr[li].l + 1;
int rlen = arr[ri].r - arr[ri].l + 1;
arr[li].lazy = arr[k].lazy;
arr[li].ls = llen * arr[k].lazy;
arr[li].rs = llen * arr[k].lazy;
arr[li].ms = llen * arr[k].lazy;
arr[ri].lazy = arr[k].lazy;
arr[ri].ls = rlen * arr[k].lazy;
arr[ri].rs = rlen * arr[k].lazy;
arr[ri].ms = rlen * arr[k].lazy;
arr[k].lazy = -1;
}
}
void segTree(int k,int l,int r)
{
arr[k].l = l;
arr[k].r = r;
arr[k].lazy = -1;
if(l == r)
{
arr[k].ls = arr[k].rs = arr[k].ms = 1;
return;
}
int mid = (l + r) >> 1;
segTree(k << 1,l,mid);
segTree((k << 1) | 1,mid + 1,r);
pushUp(k);
}
void update(int k,int x,int y,int flag)
{
if(arr[k].l == x && arr[k].r == y)
{
arr[k].lazy = flag;
arr[k].ls = (y - x + 1) * flag;
arr[k].rs = (y - x + 1) * flag;
arr[k].ms = (y - x + 1) * flag;
return;
}
pushDown(k);
int mid = (arr[k].r + arr[k].l) >> 1;
if(x > mid)
{
update((k << 1) | 1,x,y,flag);
}
else if(y <= mid)
{
update(k << 1,x,y,flag);
}
else
{
update(k << 1,x,mid,flag);
update((k << 1) | 1,mid + 1,y,flag);
}
pushUp(k);
}
int query(int k,int x)
{
if(arr[k].l == arr[k].r)
{
return arr[k].l;
}
int mid = (arr[k].l + arr[k].r) >> 1;
pushDown(k);
if(arr[k << 1].ms >= x)
{
return query(k << 1,x);
}
if(arr[k << 1].rs + arr[(k << 1) | 1].ls >= x)
{
return arr[k << 1].r - arr[k << 1].rs + 1;
}
if(arr[(k << 1) | 1].ms >= x)
{
return query((k << 1) | 1,x);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) != EOF)
{
segTree(1,1,n);
int flag;
int x,y;
while(m--)
{
scanf("%d",&flag);
if(flag == 1)
{
scanf("%d",&x);
if(arr[1].ms < x)
{
printf("0\n");
continue;
}
int t = query(1,x);
printf("%d\n",t);
update(1,t,t + x - 1,0);
}
else
{
scanf("%d%d",&x,&y);
update(1,x,x + y - 1,1);
}
}
}
return 0;
}