Hotel poj 3667


Language:
Default

Hotel

                                              

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18020   Accepted: 7823

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 ≤ XiN-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 Di (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

[Submit]   [Go Back]   [Status]   [Discuss]

题意 :

  两种操作

    1)  选一段 长度为 D 的可用区间 ,并占用它

    2)  从 X 开始 长度为 D 的区间 标记为可用

  开始时均为可用

  区间范围 1--n

  询问次数 m

  1 ≤  n  ,m < 50,000

思路:

  考虑区间合并线段树,带lazy标记

  

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <functional>

#define lson l,mid,pos<<1
#define rson mid+1,r,pos<<1|1

using namespace std;

const int maxn = 50000+10;
int lazy[maxn<<2];
int pre[maxn<<2];
int suf[maxn<<2];
int now[maxn<<2];
int a[maxn];
void push_down(int l,int r,int pos)
{
    if (l==r){
        a[l]=lazy[pos]-1;
        pre[pos]=suf[pos]=now[pos]=(a[l]==0)*1;
        lazy[pos]=0;
        return ;
    }
    int mid=(l+r)>>1;
    int lpos=pos<<1,rpos=pos<<1|1;
    a[l]=a[r]=a[mid]=a[mid+1]=lazy[pos]-1;
    pre[pos]=suf[pos]=now[pos]=(a[l]==0)*(r-l+1);
    lazy[lpos]=lazy[rpos]=lazy[pos];
    lazy[pos]=0;
}
void push_up(int l,int r,int pos)
{
    int mid=(l+r)>>1;
    int lpos=pos<<1,rpos=pos<<1|1;
    if (lazy[lpos])push_down(lson);
    if (lazy[rpos])push_down(rson);
    if (pre[lpos]+l==mid+1)pre[pos]=pre[lpos]+pre[rpos];
    else pre[pos]=pre[lpos];
    if (r-suf[rpos]==mid)suf[pos]=suf[lpos]+suf[rpos];
    else suf[pos]=suf[rpos];
    now[pos]=max(now[lpos],now[rpos]);
    if (a[mid]==0&&a[mid+1]==0)now[pos]=max(now[pos],suf[lpos]+pre[rpos]);
}
int getit(int l,int r,int pos)
{
    if (lazy[pos])push_down(l,r,pos);
    return now[pos];
}
void build(int l,int r,int pos)
{
    lazy[pos]=0;
    if (l==r){
        a[l]=0;
        suf[pos]=pre[pos]=now[pos]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(l,r,pos);
}
void upd(int l,int r,int pos,int l1,int r1,int v)
{
    if (lazy[pos])push_down(l,r,pos);
    if (l1<=l&&r1>=r){
        lazy[pos]=v;
        a[l]=a[r]=lazy[pos]-1;
        pre[pos]=suf[pos]=now[pos]=(a[l]==0)*(r-l+1);
        return ;
    }
    int mid=(l+r)>>1;
    if (mid>=l1)upd(lson,l1,r1,v);
    if (mid+1<=r1)upd(rson,l1,r1,v);
    push_up(l,r,pos);
}
int query(int l,int r,int pos,int len)
{
    if (lazy[pos])push_down(l,r,pos);
    if (now[pos]<len)return 0;
    if (l==r)return l;
    int mid=(l+r)>>1;
    int lpos=pos<<1,rpos=pos<<1|1;
    if (getit(lson)>=len)return query(lson,len);
    int v2=getit(rson);
    int v3=suf[lpos];
    if (a[mid]==0&&a[mid+1]==0)v3=suf[lpos]+pre[rpos];
    if (v3>=len)return mid-suf[lpos]+1;
    return query(rson,len);
}
int main()
{
    int n,m;
    int tr,x,y;
    while (~scanf("%d%d",&n,&m)){
        memset(a,0,sizeof(a));
        memset(lazy,0,sizeof(lazy));
        build(1,n,1);
        for (int i=0;i<m;i++){
            scanf("%d%d",&tr,&x);
            if (tr==1){
                int ans=query(1,n,1,x);
                printf("%d\n",ans);
                if (ans)
                upd(1,n,1,ans,ans+x-1,2);
            }else {
                scanf("%d",&y);
                upd(1,n,1,x,x+y-1,1);
            }
        }
    }
    return 0;
}

在 vj 上看到了一个极快的vector暴力: 代码

* 分块这题应该也是可做的。(留坑,有空补上代码)

* 应该有瞎搞做法?(想到了补上)

时间: 2024-10-13 22:51:57

Hotel poj 3667的相关文章

Hotel - poj 3667(求连续子区间)

题意:有两种操作 1,从左往右找一个区间是 D 的连续序列,然后覆盖,返回区间最前面的数,如果没有输出0 2, 释放从L开始连续D的区间 分析:就是从左往右查找一个D的连续区间,可以使用三个值操作lsum,rsum,sum,分别是从左往右的最大连续值,从右往左的最大连续值,整个区间的最大连续区间,与(I - Tunnel Warfare - hdu 1540)有些类似,不过更新时候是不一样的,这个sum单纯的保存最大连续区间 ***********************************

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

题目传送门 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(线段树)

POJ 3667 Hotel 题目链接 题意:有n个房间,现在有两个操作 1.找到连续长度a的空房间,入住,要尽量靠左边,如果有输出最左边的房间标号,如果没有输出0 2.清空[a, a + b - 1]的房间 思路:线段树的区间合并,记录下左边连续最长和右边连续最长空房间,和每一段的最大值,这样pushup的时候就是进行区间合并,注意查询的时候由于是要尽量左,所以先查左孩子,在查横跨左右的,在查右孩子 代码: #include <cstdio> #include <cstring>

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

POJ 3667 线段树的区间合并简单问题

题目大意:有一排标号1-N的房间.操作一:询问是不是有连续长度为a的空房间,有的话住进最左边(占用a个房间)操作二:将[a,a+b-1]的房间清空(腾出b个房间)思路:记录每个区间中“靠左”“靠右”“中间”的空房间线段树操作:update:区间替换query:询问满足条件的最左端点 题目链接: http://vjudge.net/problem/viewProblem.action?id=10354 题目操作: 因为这里找从最左边住起的房间,所以这里不能像常规地写query函数那样写了,终于发现

POJ 3667 Hotel

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16782   Accepted: 7303 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 - 线段树

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

题目链接:http://poj.org/problem?id=3667 题意:宾馆有n个房间.有人来入住.共有2种操作 输入1和d,表示查询最左的连续d个空房间数的起始位置. 输入2,x和d,表示将从x开始长度为d的连续的房间清空. 思路:裸的区间合并.每个结点区间[l,r]存从左端点l开始向右最大连续空房间数lm,从右端点r开始向左最大连续空房间数rm和当前区间内最大连续空房间数. 代码: #include <iostream> #include <stdio.h> #inclu