# Hotel POJ – 3667 （线段树的区间更新）洛谷P2894 [USACO08FEB]

## 题目描述

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.

## 输入输出格式

• 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

• 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.

## 输入输出样例

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6


1
4
7
0
5

#include <iostream>
#include<cstring>
using namespace std;
int a[300007], lx[300007], rx[300007], lazy[300007];
void update(int n, int l, int r, int L, int R, int c);

void build(int n, int l, int r) {
if(l == r) {
a[n] = 1;
lx[n] = 1;
rx[n] = 1;
return ;
}
build(n << 1, l, (l + r) / 2);
build(n << 1 | 1, (l + r) / 2 + 1, r);
a[n] = r - l + 1;
lx[n] = r - l + 1;
rx[n] = r - l + 1;
}

int query(int n, int l, int r, int N) {
//cout<<n<<" "<<l<<" "<<r<<" "<<N<<endl;
if(lazy[n] != -1) {
update(n << 1, l, (l + r) / 2, l, (l + r) / 2, lazy[n]);
update(n << 1 | 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, r, lazy[n]);
lazy[n] = -1;
}
if(a[n] < N) {
return 0;
}
if(lx[n] >= N) {
return l;
}

if(a[n << 1] >= N) {
return query(n << 1, l, (l + r) / 2, N);
} else if(rx[n << 1] + lx[n << 1 | 1] >= N) {
return (r+l)/2 - rx[n<<1]+1;
} else{
return query(n << 1 | 1, (l + r) / 2 + 1, r, N);
}

}
void update(int n, int l, int r, int L, int R, int c) {
//cout<<n<<" "<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<c<<endl;
if(l==r){
a[n]=lx[n]=rx[n]=c;
return;
}
if(L <= l && R >= r) {
lazy[n] = c;
lx[n] = rx[n] = a[n] = (r - l + 1) * c;
return ;
}

if(lazy[n] != -1) {
update(n << 1, l, (l + r) / 2, l, (l + r) / 2, lazy[n]);
update(n << 1 | 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, r, lazy[n]);
lazy[n] = -1;
}
int mid=(l+r)/2;
if(L>mid){
update(n<<1|1,mid+1,r,L,R,c);
}else if(R<=mid){
update(n<<1,l,mid,L,R,c);

}else{
update(n<<1|1,mid+1,r,L,R,c);
update(n<<1,l,mid,L,R,c);
}

int t = max(a[n << 1], a[n << 1 | 1]);
a[n] = max(t, rx[n << 1] + lx[n << 1 | 1]);
lx[n] = lx[n << 1];
rx[n] = rx[n << 1 | 1];
if(lx[n << 1] == (r + l) / 2 - l + 1) {
lx[n] += lx[n << 1 | 1];
}
if(rx[n << 1 | 1] == (r - (l + r) / 2)) {
rx[n] += rx[n << 1];
}

}

int main(int argc, char const *argv[]) {
ios::sync_with_stdio(0);
int n, m;
memset(lazy, -1, sizeof(lazy));
cin >> n >> m;
int c, d, e;
build(1,1,n);
for (int i = 0; i < m; ++i) {
cin >> c;
if(c == 1) {
cin >> d;
int ans=query(1,1,n,d);
if(ans!=0){
update(1,1,n,ans,ans+d-1,0);
}
cout<<ans<<endl;

} else {
cin >> d >> e;
update(1,1,n,d,d+e-1,1);

}
}

return 0;
}