POJ - 3667 Hotel (线段树 + 区间合并)

来源:互联网 发布:maya mac 室内设计 编辑:程序博客网 时间:2024/04/28 23:09
POJ - 3667
Hotel
Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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 andD(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 61 31 31 31 32 5 51 6

Sample Output

14705
/*Author: 2486Memory: 2464 KBTime: 875 MSLanguage: G++Result: Accepted*/#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;#define lson rt << 1, l, mid#define rson rt << 1|1, mid + 1, r#define root 1, 1, nconst int MAXN = 50000 + 5;//(0)代表着空区间 //msum代表着区域内的最长连续空区间, //lsum代表着从左端点L开始最长连续空区间, //rsum代表着从右端点R向前数的最长连续空区间。 int lsum[MAXN << 2], rsum[MAXN << 2], msum[MAXN << 2];int col[MAXN << 2];void pushdown(int rt, int m) {    if(col[rt] != -1) {        col[rt << 1] = col[rt << 1|1] = col[rt];        msum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = col[rt] ? 0 : m - (m >> 1);        msum[rt << 1|1] = lsum[rt << 1|1] = rsum[rt << 1|1] = col[rt] ? 0 : (m >> 1);        col[rt] = -1;    }}void pushup(int rt, int m) {    lsum[rt] = lsum[rt << 1];    rsum[rt] = rsum[rt << 1|1];    if(lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt << 1|1];    if(rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1];    msum[rt] = max(lsum[rt << 1|1] + rsum[rt << 1], max(msum[rt << 1], msum[rt << 1|1]));}void build(int rt,int l,int r) {    msum[rt] = lsum[rt] = rsum[rt] = r - l + 1;    col[rt] = -1;    if(l == r) return;    int mid = (l + r) >> 1;    build(lson);    build(rson);}void update(int L, int R, int c, int rt, int l, int r) {    if(L <= l && r <= R) {        msum[rt] = rsum[rt] = lsum[rt] = c ? 0 : r - l + 1;        col[rt] = c;        return ;    }    pushdown(rt, r - l + 1);    int mid = (l + r) >> 1;    if(L <= mid) update(L, R, c, lson);    if(R > mid) update(L, R, c, rson);    pushup(rt, r - l + 1);}int query(int wid, int rt, int l, int r) {    if(l == r) return l;    pushdown(rt, r - l + 1);    int mid = (l + r) >> 1;    if(msum[rt << 1] >= wid) return query(wid, lson);    else if(rsum[rt << 1] + lsum[rt << 1|1] >= wid) return mid - rsum[rt << 1] + 1;    return query(w,rson);    //先在左子区间找是否有容纳长度为wid的空区间存在。     //如果没有,就在他们的接壤的地方找,如果还是没有找到的话,就在右子区间寻}int main() {    int n , m;    scanf("%d%d",&n,&m);    build(root);    while (m --) {        int op , a , b;        scanf("%d",&op);        if (op == 1) {            scanf("%d",&a);            if (msum[1] < a) puts("0");            else {                int p = query(a , root);                printf("%d\n",p);                update(p , p + a - 1 , 1 , root);            }        } else {            scanf("%d%d",&a,&b);            update(a , a + b - 1 , 0 , root);        }    }    return 0;}


1 0
原创粉丝点击