POJ - 3667 Hotel 线段树区间合并

200th

Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 19431   Accepted: 8455

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

题解: 本题是线段树区间合并的模板题

什么是区间合并

寻找区间中满足条件的最长连续区间

本题要解决什么问题?

对于一个区间来说1表示被使用(覆盖),0表示未被使用.我们要找出区间中的最靠左的连续k个未被使用的子区间

线段树结构

sum[rt] 表示区间[l,r]中未被使用的最长连续区间长度

lcov[rt] 表示从左端点l开始向右的能达到的未被使用的最长连续区间长度 (如果l点被使用则lcov[rt] = 0)

rcov[rt] 表示从右端点r开始向左的能达到的未被使用的最长连续区间长度(如果r点被使用则rcov[rt] = 0)

为什么要设置两个端点的数组呢 ?

因为记录下左右端点的最长区间长度后,可以用来更新父亲节点的sum[rt] lcov[rt] 和 rcov[rt]

怎么更新?

void push_up(rt) 函数

void push_up(int rt) {
        cnt[rt] = cnt[rt<<1] + cnt[rt<<1|1]; /// 记录节点区间大小
        lcov[rt] = lcov[rt<<1];              /// 显而易见   
        rcov[rt] = rcov[rt<<1|1];
        if(lcov[rt] == cnt[rt<<1]) lcov[rt] += lcov[rt<<1|1]; /// 左端点向右的长度也许还有更长
        if(rcov[rt] == cnt[rt<<1|1]) rcov[rt] += rcov[rt<<1]; /// 右端点向左的长度也许还有更长
        /// 整个区间的满足条件的长度是这三个的最大值
        sum[rt] = max(max(sum[rt<<1],sum[rt<<1|1]),rcov[rt<<1]+lcov[rt<<1|1]);
    }

区间更新的push_down(rt) 延迟标记更新函数

void push_down(int rt) {
        if(lz[rt] == -1) return ;
        lz[rt<<1] = lz[rt<<1|1] = lz[rt];
        if(lz[rt]) { /// 置满
            lcov[rt<<1] = rcov[rt<<1] = sum[rt<<1] = 0;
            lcov[rt<<1|1] = rcov[rt<<1|1] = sum[rt<<1|1] = 0;
        }
        else {  ///置空 
            lcov[rt<<1] = rcov[rt<<1] = sum[rt<<1] = cnt[rt<<1];
            lcov[rt<<1|1] = rcov[rt<<1|1] = sum[rt<<1|1] = cnt[rt<<1|1];
        }
        lz[rt] = -1;
    }

怎么查询最靠左的可满足区间呢?

首先查询左区间,之后左区间和右区间交接,再是右区间

int query(int val,int l,int r,int rt) {
        if(sum[rt] < val) return -1;
        if(l == r) return l;
        push_down(rt);
        int mid = (l + r) >> 1;
        if(sum[rt<<1] >= val) return query(val,l,mid,rt<<1);
        if(rcov[rt<<1] + lcov[rt<<1|1] >= val) return mid - rcov[rt<<1] + 1;
        return query(val,mid+1,r,rt<<1|1);
    }

本题代码:

//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;
const int maxn = 5e4+10;
struct SegTree
{
    int sum[maxn<<2],lz[maxn<<2],lcov[maxn<<2],rcov[maxn<<2],cnt[maxn<<2];
    void init() {
        memset(sum,0,sizeof(sum));
        memset(lz,-1,sizeof(lz));
        memset(lcov,0,sizeof(lcov));
        memset(rcov,0,sizeof(rcov));
    }
    void push_down(int rt) {
        if(lz[rt] == -1) return ;
        lz[rt<<1] = lz[rt<<1|1] = lz[rt];
        if(lz[rt]) { /// 置满
            lcov[rt<<1] = rcov[rt<<1] = sum[rt<<1] = 0;
            lcov[rt<<1|1] = rcov[rt<<1|1] = sum[rt<<1|1] = 0;
        }
        else {  ///置空 
            lcov[rt<<1] = rcov[rt<<1] = sum[rt<<1] = cnt[rt<<1];
            lcov[rt<<1|1] = rcov[rt<<1|1] = sum[rt<<1|1] = cnt[rt<<1|1];
        }
        lz[rt] = -1;
    }
    void push_up(int rt) {
        cnt[rt] = cnt[rt<<1] + cnt[rt<<1|1]; /// 记录节点区间大小
        lcov[rt] = lcov[rt<<1];              /// 显而易见   
        rcov[rt] = rcov[rt<<1|1];
        if(lcov[rt] == cnt[rt<<1]) lcov[rt] += lcov[rt<<1|1]; /// 左端点向右的长度也许还有更长
        if(rcov[rt] == cnt[rt<<1|1]) rcov[rt] += rcov[rt<<1]; /// 右端点向左的长度也许还有更长
        /// 整个区间的满足条件的长度是这三个的最大值
        sum[rt] = max(max(sum[rt<<1],sum[rt<<1|1]),rcov[rt<<1]+lcov[rt<<1|1]);
    }
    void build(int l,int r,int rt) {
        if(l == r) {
            sum[rt] = lcov[rt] = rcov[rt] = 1;
            lz[rt] = -1;
            cnt[rt] = 1;
            return ;
        }
        push_down(rt);
        int mid = (l + r) >> 1;
        build(l,mid,rt<<1);
        build(mid+1,r,rt<<1|1);
        push_up(rt);
    }
    void update(int ql,int qr,int val,int l,int r,int rt) {
        if(ql == l && qr == r) {
            if(val) { /// 1 置满
                sum[rt] = lcov[rt] = rcov[rt] = 0;
                lz[rt] = 1;
            }
            else { /// 0 置空
                sum[rt] = lcov[rt] = rcov[rt] = cnt[rt];
                lz[rt] = 0;
            }
            return ;
        }
        push_down(rt);
        int mid = (l + r) >> 1;
        if(qr <= mid) update(ql,qr,val,l,mid,rt<<1);
        else if(ql > mid) update(ql,qr,val,mid+1,r,rt<<1|1);
        else {
            update(ql,mid,val,l,mid,rt<<1);
            update(mid+1,qr,val,mid+1,r,rt<<1|1);
        }
        push_up(rt);
    }
    int query(int val,int l,int r,int rt) {
        if(sum[rt] < val) return -1;
        if(l == r) return l;
        push_down(rt);
        int mid = (l + r) >> 1;
        if(sum[rt<<1] >= val) return query(val,l,mid,rt<<1);
        if(rcov[rt<<1] + lcov[rt<<1|1] >= val) return mid - rcov[rt<<1] + 1;
        return query(val,mid+1,r,rt<<1|1);
    }
}seg;
int n,q; 
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        seg.build(1,n,1);
        int op,x,len;
        while(q--)
        {
            scanf("%d%d",&op,&x);
            if(op == 1) {
                int pos = seg.query(x,1,n,1);
                if(pos == -1) { printf("0\n");continue; }
                printf("%d\n",pos);
                seg.update(pos,pos+x-1,1,1,n,1);
            }
            else {
                scanf("%d",&len);
                seg.update(x,x+len-1,0,1,n,1); 
            }
        }
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/m0_38013346/article/details/80512990