Hotel---poj3667(线段树区间问题)

题目链接:http://poj.org/problem?id=3667

题意:酒店有n个房间,现有m个团队,每个团队需要连续 d 个房间,现在有两个操作,1:需要 d 个房间,2:从 x 开始连续 d 个房间退房;

当是1的时候,需要d个房间时, 我们尽可能的找到最靠左的房间给客人,输出最左边房间的编号;

简化一下就是当是1时,让连续的最左边的区间覆盖,2时让对应的区间释放;

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define INF 0xfffffff
#define N 50050
#define Lson r<<1
#define Rson r<<1|1

struct SegmentTree
{
    int L, R, flag;///flag 表示整个操作的是覆盖1还是释放覆盖2,还是保持原样0;
    int lsum, rsum, sum;
    ///lsum区间左边(从L开始)连续最大不被覆盖的值
    ///rsum区间右边(到R介绍)连续最大不被覆盖的值
    ///sum整个区间连续最大不被覆盖的值
    int Mid() { return (L+R)>>1;}
    int len() { return R-L+1; }

} a[N<<2];

void Build(int r, int L, int R)
{
    a[r].L = L, a[r].R = R, a[r].flag = 0;
    a[r].lsum = a[r].rsum = a[r].sum = a[r].len();

    if(L == R)return ;

    Build(Lson, L, a[r].Mid());
    Build(Rson, a[r].Mid()+1, R);
}
void Up(int r)
{
    a[r].lsum = a[Lson].lsum;
    a[r].rsum = a[Rson].rsum;///左右区间由下面的左右区间决定;

    if(a[r].lsum == a[Lson].len())///当超过区间所在长度时,加上相邻的那部分;
        a[r].lsum += a[Rson].lsum;
    if(a[r].rsum == a[Rson].len())
        a[r].rsum += a[Lson].rsum;

    a[r].sum = max(a[Lson].rsum + a[Rson].lsum, max(a[Lson].sum, a[Rson].sum));///总的最大值是这三部分的最大值;
}

void Down(int r)
{
    if(a[r].L != a[r].R && a[r].flag)
    {
        a[Lson].lsum = a[Lson].rsum = a[Lson].sum = (a[r].flag == 1 ? 0 : a[Lson].len());
        a[Rson].lsum = a[Rson].rsum = a[Rson].sum = (a[r].flag == 1 ? 0 : a[Rson].len());

        a[Lson].flag = a[Rson].flag = a[r].flag;
        a[r].flag = 0;
    }
}
void Update(int r, int L, int R, int flag)
{
    Down(r);///向下更新;

    if(a[r].L == L && a[r].R == R)
    {
        a[r].lsum = a[r].rsum = a[r].sum = (flag == 1 ? 0 : a[r].len());
        a[r].flag = flag;
        return ;
    }

    ///更新子区间;
    if(R <= a[r].Mid())
        Update(Lson, L, R, flag);
    else if( L > a[r].Mid())
        Update(Rson, L, R, flag);
    else
    {
        Update(Lson, L, a[r].Mid(), flag);
        Update(Rson, a[r].Mid()+1, R, flag);
    }
    Up(r);///当下面发生改变改变的时候需要更新上面区间的;
}

int Query(int r, int num)
{
    ///查询是按照从左到右的顺序来的
    if(a[r].lsum >= num) return a[r].L;///最左边的;
    if(a[Lson].sum >= num) return Query(Lson, num);///左区间;
    if(a[Lson].rsum + a[Rson].lsum >= num)///中间区间;
        return a[Lson].R-a[Lson].rsum+1;
    return Query(Rson, num);///右区间;
}

int main()
{
    int n, m, op, L, d;
    while(scanf("%d %d", &n, &m) != EOF)
    {
        Build(1, 1, n);
        while(m--)
        {
            scanf("%d", &op);
            if(op == 1)
            {
                scanf("%d", &d);
                if(a[1].sum < d)
                {
                    printf("0\n");
                    continue;
                }
                L = Query(1, d);
                printf("%d\n", L);

                Update(1, L, L+d-1, 1);///更新从L开始的连续d个点让他们被覆盖;
            }
            else
            {
                scanf("%d %d", &L, &d);
                Update(1, L, L+d-1, 2);///更新从L开始的连续d个点让他们不被覆盖;
            }
        }
    }
    return 0;
}

  

时间: 2024-12-10 20:01:01

Hotel---poj3667(线段树区间问题)的相关文章

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——————【线段树区间合并】

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 (线段树区间合并 )

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

poj3667 线段树 区间合并

1 //Accepted 3728 KB 1079 ms 2 //线段树 区间合并 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 /** 11 * This is a document

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

题目链接 题意: 给定一个有$N$个车位的停车场(都在一条直线上),现在有有两种操作 $1.x $     要停连续的停$x$辆车,输出第一辆车停的位置(尽量靠前),不能就输出$0$: $2.x,d$ 从x位置开始开走连续的$d$辆车. 思路: 一个线段树区间和问题,而且满足区间可加性,就要用到区间合并. 1 /* 2 * Author: windystreet 3 * Date : 2018-08-15 10:29:55 4 * Motto : Think twice, code once.

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 (线段树 - 区间合并)

题目传送:Hotel 思路:线段树,区间合并,区间替换,查询最左断点,看胡浩版本的线段树好几天了,今天看这个看了好久,慢慢来吧,具体都写在注释里了 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #inc

POJ-3667 线段树区间合并入门题

题意:长度为n的区间,m个操作,一开始都是0 1 x表示求出长度为x的0的连续区间的最左端,并把这个区间变成1 2 x y表示将区间[x,y]变成0 线段树的区间合并第一题: 每次维护左端连续区间长度ls.右端连续区间长度rs,最大连续长度ms 区间合并的注意点主要在push up操作: 每次更新了一段区间之后向上更新,首先,父区间的ls继承左子树的ls,父区间的rs继承右子树的rs 然后就是重点:左右区间合并之后中间部分可能是连续的!!! 所以:如果整个左.右子区间都是“满的”,父区间的ls和

POJ3667——线段树区间合并(未搞透)——Hotel

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 v

(简单) 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