Hotel
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 11243 | Accepted: 4868 |
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 groupi 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 roomsXi ..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, andDi
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
Source
题意:
n个房间,m个操作
两种类型:
1 a 把左边连续的a个空房间置为非空,并输出这a个房间的最左一个房间序号 如果没有a个就输出0
2 a b 把a 开始的 b个房间置为空
线段树 区间合并
lsum 表示最左最大连续长度 rsum 表示最右最大连续长度 msum表示最大连续长度
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 50000 + 20;
const int maxo = maxn << 2;
int setv[maxo];
int lsum[maxo], rsum[maxo], msum[maxo];
void pushDown(int o, int m) {
if(setv[o] != -1) {
setv[o<<1] = setv[o<<1|1] = setv[o];
lsum[o<<1] = rsum[o<<1] = msum[o<<1] = setv[o] ? 0 : m-(m>>1);
lsum[o<<1|1] = rsum[o<<1|1] = msum[o<<1|1] = setv[o] ? 0 : m>>1;
setv[o] = -1;
}
}
void pushUp(int o, int m) {
lsum[o] = lsum[o<<1];
rsum[o] = rsum[o<<1|1];
if(lsum[o<<1] == m-(m>>1)) lsum[o] += lsum[o<<1|1];
if(rsum[o<<1|1] == m>>1) rsum[o] += rsum[o<<1];
msum[o] = max(rsum[o<<1] + lsum[o<<1|1], max(msum[o<<1], msum[o<<1|1]));
}
void build(int o, int L, int R) {
lsum[o] = rsum[o] = msum[o] = R - L + 1;
setv[o] = -1;
if(L == R) return ;
int M = L + (R - L) / 2;
build(o<<1, L, M);
build(o<<1|1, M+1, R);
}
int ql, qr, qv;
void update(int o, int L, int R) {
if(ql <= L && R <= qr) {
setv[o] = qv;
lsum[o] = rsum[o] = msum[o] = qv ? 0 : R-L+1;
return ;
}
pushDown(o, R-L+1);
int M = L + (R-L) / 2;
if(ql <= M) update(o<<1, L, M);
if(M < qr) update(o<<1|1, M+1, R);
pushUp(o, R-L+1);
}
int qd;
int query(int o, int L, int R) {
if(L == R) return L;
pushDown(o, R-L+1);
int M = L + (R-L) / 2;
if(msum[o<<1] >= qd) return query(o<<1, L, M);
else if(rsum[o<<1] + lsum[o<<1|1] >= qd) return M - rsum[o<<1] + 1;
return query(o<<1|1, M+1, R);
}
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
build(1, 1, n);
for(int i=0; i<m; i++) {
int type;
scanf("%d", &type);
if(type == 1) {
scanf("%d", &qd);
if(msum[1] < qd) puts("0");
else {
int p = query(1, 1, n);
printf("%d\n", p);
ql = p, qr = p + qd - 1;
qv = 1;
update(1, 1, n);
}
} else {
scanf("%d%d", &ql, &qr);
qr = ql + qr - 1;
qv = 0;
update(1, 1, n);
}
}
}
return 0;
}