# [POJ 3667] Hotel【线段树区间合并】

• 2018-01-21
• 0
• 0

## Problem:

 Time Limit: 3000MS Memory Limit: 65536K

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 rto 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 D(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
```

Source

USACO 2008 February Gold

## Code: O((N+M)logN) [2716K, 610MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline int getint(){
char ch; int num;
while(!isdigit(ch = getchar()));
num = ch - '0';
while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
return num;
}

int N, M;

struct Node{
int cov;  // Lazy Tag: -1 means no operation, 0 means to set empty, and 1 means to set full
int tlen, llen, rlen;
};

struct Segment_Tree{
#define MAXNODE 300005
#define lch (u << 1)
#define rch (u << 1 | 1)
#define smid (l + r >> 1)
Node node[MAXNODE];

inline void build(int u, int l, int r){
node[u].cov = -1;
node[u].tlen = node[u].llen = node[u].rlen = r - l + 1;
if(l == r) return;  // Or the function will fall into an endless loop
if(l <= smid) build(lch, l, smid);
if(smid < r) build(rch, smid + 1, r);
}

inline void pushdown(int u, int l, int r){
if(node[u].cov != -1){
node[lch].cov = node[rch].cov = node[u].cov;
node[lch].tlen = node[lch].llen = node[lch].rlen = (node[u].cov) ? 0 : smid - l + 1;
node[rch].tlen = node[rch].llen = node[rch].rlen = (node[u].cov) ? 0 : r - smid;  // Empty or fill the sub-region
node[u].cov = -1;
}
}

inline void update(int u, int l, int r){
node[u].llen = node[lch].llen;
if(node[u].llen == smid - l + 1) node[u].llen += node[rch].llen;  // llen exceeds half of total length

node[u].rlen = node[rch].rlen;
if(node[u].rlen == r - smid) node[u].rlen += node[lch].rlen;  // rlen exceeds half of total length

node[u].tlen = max(max(node[lch].tlen, node[rch].tlen), node[lch].rlen + node[rch].llen);
// Beware of this expression, which is not "max(max(node[u].llen, node[u].rlen), node[lch].rlen + node[rch].llen)" !!!
}

inline void modify(int u, int l, int r, int ml, int mr, int cov){
if(l == ml && r == mr){
node[u].tlen = node[u].llen = node[u].rlen = (node[u].cov = cov) ? 0 : r - l + 1;
return;
}
pushdown(u, l, r);
if(mr <= smid) modify(lch, l, smid, ml, mr, cov);
else if(ml > smid) modify(rch, smid + 1, r, ml, mr, cov);
else modify(lch, l, smid, ml, smid, cov), modify(rch, smid + 1, r, smid + 1, mr, cov);
update(u, l, r);
}

inline int query(int u, int l, int r, int len){
if(l == r) return l;
pushdown(u, l, r);
if(node[lch].tlen >= len) return query(lch, l, smid, len);
else if(node[lch].rlen + node[rch].llen >= len) return smid - node[lch].rlen + 1;  // Return the start position immediately
else return query(rch, smid + 1, r, len);
}
} sgt;

int main(){
N = getint(), M = getint();
sgt.build(1, 1, N);
for(register int i = 1; i <= M; i++){
int op = getint();
if(op == 1){
int D = getint();
if(sgt.node[1].tlen < D){
puts("0");
continue;
}
int pos = sgt.query(1, 1, N, D);
printf("%d\n", pos);
sgt.modify(1, 1, N, pos, pos + D - 1, 1);
}
else{
int X = getint(), D = getint();
sgt.modify(1, 1, N, X, X + D - 1, 0);
}
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai