题目描述

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.

参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

输入输出格式

输入格式:

  • 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

输出格式:

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

输入输出样例

输入样例#1:

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:

1
4
7
0
5

线段树维护向左连续空的最大值,向右连续空的最大值,以前最长的空的

屠龙宝刀点击就送

#include <ctype.h>
#include <cstdio>
#define M 50005 void read(int &x)
{
x=;
bool f=;
register char ch=getchar();
for(; !isdigit(ch); ch=getchar()) if(ch=='-') f=;
for(; isdigit(ch); ch=getchar()) x=x*+ch-'';
x=f?(~x)+:x;
}
int n,m;
struct treetype
{
int sum,l,r,Max,mid,flag;
treetype *left,*right;
treetype ()
{
left=right=NULL;
l=r=sum=Max=flag=;
}
}*root=new treetype;
class typetree
{
private:
int max(int a,int b) {return a<b?b:a;}
public:
void pushup(treetype *&k)
{
if(k->left->sum==k->left->Max) k->l=k->left->Max+k->right->l;
else k->l=k->left->l;
if(k->right->sum==k->right->Max) k->r=k->right->Max+k->left->r;
else k->r=k->right->r;
k->Max=max(max(k->left->Max,k->right->Max),k->left->r+k->right->l);
}
void build(treetype *&k,int l,int r)
{
k=new treetype;
k->l=k->r=k->Max=k->sum=r-l+;
if(l==r) return;
k->mid=(l+r)>>;
build(k->left,l,k->mid);
build(k->right,k->mid+,r);
}
void pushdown(treetype *&k)
{
k->left->flag=k->right->flag=k->flag;
if(k->flag==)
{
k->left->r=k->left->l=k->left->Max=;
k->right->r=k->right->l=k->right->Max=;
}
else
{
k->left->Max=k->left->l=k->left->r=k->left->sum;
k->right->Max=k->right->l=k->right->r=k->right->sum;
}
k->flag=;
}
void change(treetype *&k,int l,int r,int opt,int L,int R)
{
if(l>=L&&r<=R)
{
k->flag=opt;
if(opt==) k->l=k->r=k->Max=;
else k->Max=k->l=k->r=k->sum;
return;
}
if(k->flag) pushdown(k);
if(L<=k->mid) change(k->left,l,k->mid,opt,L,R);
if(R>k->mid) change(k->right,k->mid+,r,opt,L,R);
pushup(k);
}
int Query(treetype *&k,int l,int r,int len)
{
if(l==r) return r;
if(k->flag) pushdown(k);
if(k->left->Max>=len) return Query(k->left,l,k->mid,len);
if(k->left->r+k->right->l>=len) return k->mid-k->left->r+;
else return Query(k->right,k->mid+,r,len);
}
};
class typetree *abc;
int main()
{
read(n);
read(m);
abc->build(root,,n);
for(int opt,x,y;m--;)
{
read(opt);
if(opt==)
{
read(x);
if(root->Max<x) {printf("0\n");continue;}
int pos=abc->Query(root,,n,x);
printf("%d\n",pos);
abc->change(root,,n,opt,pos,pos+x-);
}
else
{
read(x);
read(y);
abc->change(root,,n,opt,x,x+y-);
}
}
return ;
}

洛谷 P2894 [USACO08FEB]酒店Hotel的更多相关文章

  1. 洛谷P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...

  2. 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  3. 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]

    题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...

  4. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

  5. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  6. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  7. 洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...

  8. 洛谷 P2894 [USACO08FEB]酒店

    题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...

  9. 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel

    题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...

随机推荐

  1. POJ-3352 Redundant Paths

    In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) t ...

  2. I.MX6 Android 5.1.1 下载、编译

    /************************************************************************* * I.MX6 Android 5.1.1 下载. ...

  3. Java泛型和反射总结

    A a = (A)Class.forName(“pacage.A”).newInstance(); 这和你 A a = new A(): 是一样的效果. String className = “Exa ...

  4. 开启sqlplus中执行计划

    在sqlplus中我们一般用Autotrace来查看执行计划,从而对于一些语句执行过程分析,开展优化工作.这里就演示一下如何将autotrace权限授予给普通的用户,以scott用户为例(set au ...

  5. bzoj1038&&500AC!

    序列dp 先开始想了一个类似区间dp的东西...少了一维 然后发现似乎不太对,因为女生的最大差和男生的最大差并不相等 dp[i][j][x][y]表示当前有i个人,j个男生,男生和女生的后缀最大差是x ...

  6. LeNet-5结构分析及caffe实现————卷积部分

    占坑,记录 1.lenet-5的结构以及部分原理 2.caffe对于lenet-5的代码结构 图一 图一是整个LeNet-5的结构图,要点有:convolutions.subsampling.full ...

  7. 【202】ThinkPad手势&快捷键

    快捷键: Ctrl + Alt + ↑:正常屏幕Ctrl + Alt + ↓:翻转屏幕Ctrl + Alt + →:向左侧翻转90°Ctrl + Alt + ←:向右侧翻转90° 首先看下 Esc 键 ...

  8. 从零开始构建一个Reactor模式的网络库(二)线程类Thread

    线程类Thread是对POSIX线程的封装类,因为要构建的是一个Linux环境下的多线程网络库,对线程的封装是很必要的. 首先是CurrentThread命名空间,主要是获取以及缓存线程id: #if ...

  9. eclipse整合tomcat

    首先确保jdk已经安装好 步骤1 获得服务器运行环境配置,Window/Preferences/Server/Runtime Environmen l步骤2 添加服务器 步骤3 选择服务器在硬盘的地址 ...

  10. .NET Core 3.0之深入源码理解Configuration(二)

      文件型配置基本内容 上一篇文章讨论了Configuration的几个核心对象,本文继续讨论Configuration中关于文件型配置的相关内容.相比较而言,文件型配置的使用场景更加广泛,用户自定义 ...