ACM: Hotel 解题报告 - 线段树-区间合并

Hotel

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

/****
思路:

线段树的区间合并问题:

要查询连续区间长度,所以要记录最长的连续区间,一段区间的连续可以分为左连续,右连续,中间连续,然后记录就可以了

父节点左连续区间为左儿子左连续,如果左儿子在整个左区间内连续则再加上右儿子左连续

父节点右连续区间为右儿子右连续,如果右儿子在整个右区间内连续则再加上左儿子右连续

父节点中间连续区间为左儿子右连续加上右儿子左连续

然后记录每个节点总的最长连续区间的值

每次向上更新或者向下延迟都要重新计算节点信息
****/
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
//#define max(a,b) a>b?a:b   //【这个地方WA了好多次。。。】
//#define min(a,b) a<b?a:b   //【这个宏定义有问题。。。以后再也不随便用宏定义了。。。WA哭了。。。】
#define MX 110000
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;

int lsum[MX<<2],rsum[MX<<2],sum[MX<<2];
int lazy[MX<<2];

//  1 a 询问是否存在a间连续的空房间,有则住进最左边   【】
//  2 a b 将 [a,a+b-1] 编号的房间清空   【成段更新】

void PushDown(int rt,int m) {
    if(lazy[rt]!=INF) {//1 ->
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];   //下移懒惰标记
        sum[rt<<1]=lsum[rt<<1]=rsum[rt<<1]=lazy[rt]*(m-(m>>1));  //因为
        sum[rt<<1|1]=lsum[rt<<1|1]=rsum[rt<<1|1]=lazy[rt]*(m>>1);
        lazy[rt]=INF;
    }
}

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];    //  这里lson[rt<<1]是表示的左区间的左最大值,lson[rt<<1|1]表示的是左区间的右最大值
    if(rsum[rt]== (m>>1) ) rsum[rt]+=rsum[rt<<1];      //  rson[。。。] 类似    而左区间的右最大值加右区间的左最大值就是中间区间的最大值
    sum[rt]=max(max(sum[rt<<1],sum[rt<<1|1]),lsum[rt<<1|1]+rsum[rt<<1]); //保存两边子节点和 中最大值 中的最大值
}

void Build(int l,int r,int rt) {
    sum[rt]=lsum[rt]=rsum[rt]=r-l+1;   //把每一个空房间标记为1,每个父节点记录为子节点最大的空区间
    lazy[rt]=INF;            //懒惰标记清空
    if(l==r) return ;
    int m=(r+l)>>1;
    Build(lson);
    Build(rson);
}

void UpData(int L,int R,int val,int l,int r,int rt) {
    if(L<=l&&r<=R) {
        sum[rt]=lsum[rt]=rsum[rt]= val ? (r-l+1):0;
        lazy[rt]=val;
        return ;
    }
    PushDown(rt,r-l+1);
    int m=(r+l)>>1;
    if(L<=m) UpData(L,R,val,lson);
    if(R> m) UpData(L,R,val,rson);
    PushUp(rt,r-l+1);
}

int Query(int ll,int l,int r,int rt) {
    if(l==r) return l;
    PushDown(rt,r-l+1);
    int m=(r+l)>>1;
    if(sum[rt<<1]>=ll) return Query(ll,lson);    //如果左最大值满足就在往左继续搜
    else if(rsum[rt<<1]+lsum[rt<<1|1]>=ll)return m-rsum[rt<<1]+1;//如果中最大值,满足就输出左区间的右最大值的第一个位置
    return Query(ll,rson);    //否则就向右继续搜
}

int main() {
    int n,m;
    scanf("%d%d",&n,&m);
    Build(1,n,1);
    int o,a,b;
    for(int i=0; i<m; i++) {
        scanf("%d",&o) ;
        if(o==1) {
            scanf("%d",&a);
            if(sum[1]<a) printf("0\n");
            else {
                int q=Query(a,1,n,1);
                printf("%d\n",q);
                UpData(q,q+a-1,0,1,n,1);
            }
        } else if(o==2) {
            scanf("%d%d",&a,&b);
            UpData(a,a+b-1,1,1,n,1);
        }
    }
    return 0;
}
时间: 2024-10-05 23:07:09

ACM: Hotel 解题报告 - 线段树-区间合并的相关文章

POJ 3667 Hotel (初遇线段树区间合并)

题意: 有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作 1 w 表示在总区间内查询一个长度为w的可用区间并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0 2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出. 思路: 这是第一次遇到线段树区间合并的题目,写下感悟,还是对线段的更新和查询工作,但是查询的对象的性质已经不像单点那样,查询的是某个线段的最大可用区间是多少,还要一并

ACM: Billboard 解题报告-线段树

Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w.所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等.. 在9月1号这天,广告牌是空的,之后广告会被一条一条的依次贴上去. 每张广告都是高度为1宽度为wi的细长的矩形纸条. 贴广告的人总是会优先选择最上面的位置来帖,而且在所有

Poj 3667——hotel——————【线段树区间合并】

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 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

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

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 Stree

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

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

题目链接~~> 做题感悟:这题是接触线段树区间合并的第一题,做的很纠结. 解题思路: 注意线段树上节点代表的信息 : 每个节点需要维护 lc , rc , mc ,add ,见下图: add 为懒惰标记.假设 i 代表父亲节点编号,左儿子为  i * 2  ,右儿子为 i * 2  + 1 ,那么我们可以得到 : T [ i ] .lc 首先加上左儿子的左边的空格数,然后需要判断一下,如果左儿子的左节点占满了整个左区间时需要再加上右儿子的左边的空格数.同理 T [ i ] .rc 也可以这样得到

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

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt

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

Language: Default Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12417   Accepted: 5346 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lak

HDU 3308 LCIS (线段树区间合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[mid + 1] > a[mid],写的细心点就好了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN = 1