题目描述

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

lmax 指从左起最长的空闲房间数,同理对于rmax;
sum指一个区间的最长空闲数;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ struct node {
int lmax, rmax;
int sum;
int add;
int len;
}tree[maxn << 2];
int n, m; void pushup(int rt) {
tree[rt].lmax = (tree[rt << 1].len == tree[rt << 1].lmax) ? tree[rt << 1].len + tree[rt << 1 | 1].lmax : tree[rt << 1].lmax;
tree[rt].rmax = (tree[rt << 1 | 1].len == tree[rt << 1 | 1].rmax) ? tree[rt << 1 | 1].len + tree[rt << 1].rmax : tree[rt << 1 | 1].rmax;
tree[rt].sum = max(tree[rt << 1].rmax + tree[rt << 1 | 1].lmax, max(tree[rt << 1].sum, tree[rt << 1 | 1].sum));
}
void pushdown(int rt) {
if (tree[rt].add) {
tree[rt << 1].add = tree[rt << 1 | 1].add = tree[rt].add;
tree[rt << 1].lmax = tree[rt << 1].sum = tree[rt << 1].rmax = (tree[rt].add == 1) ? tree[rt << 1].len : 0;
tree[rt << 1 | 1].rmax = tree[rt << 1 | 1].lmax = tree[rt << 1 | 1].sum = (tree[rt].add == 1) ? tree[rt << 1 | 1].len : 0;
tree[rt].add = 0;
}
} void build(int l, int r, int rt) {
tree[rt].add = 0;
tree[rt].len = tree[rt].sum = tree[rt].lmax = tree[rt].rmax = r - l + 1;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1);
pushup(rt);
} void upd(int L, int R, int l, int r, int rt, int tg) {
if (L <= l && r <= R) {
tree[rt].add = tg;
tree[rt].sum = tree[rt].lmax = tree[rt].rmax = (tg == 1) ? tree[rt].len : 0;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid)upd(L, R, l, mid, rt << 1, tg);
if (mid < R)upd(L, R, mid + 1, r, rt << 1 | 1, tg);
pushup(rt);
} int query(int l, int r, int rt, int len) {
if (l == r)return l;
pushdown(rt);
int mid = (l + r) >> 1;
if (tree[rt << 1].sum >= len)return query(l, mid, rt << 1, len);
if (tree[rt << 1].rmax + tree[rt << 1 | 1].lmax >= len)return mid - tree[rt << 1].rmax + 1;
return query(mid + 1, r, rt << 1 | 1, len);
} int main() {
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
build(1, n, 1);
while (m--) {
int op; rdint(op);
if (op == 1) {
int x; rdint(x);
if (tree[1].sum >= x) {
int ans = query(1, n, 1, x);
cout << ans << endl;
upd(ans, ans + x - 1, 1, n, 1, 2);
}
else cout << 0 << endl;
}
else {
int x, y; rdint(x); rdint(y);
upd(x, x + y - 1, 1, n, 1, 1);
}
}
return 0;
}

[USACO08FEB]酒店Hotel 线段树 BZOJ 1593的更多相关文章

  1. [USACO08FEB]酒店Hotel 线段树

    [USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...

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

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

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

    题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...

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

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

  5. 浅谈线段树 (例题:[USACO08FEB]酒店Hotel)By cellur925

    今天我们说说线段树. 我个人还是非常欣赏这种数据结构的.(逃)因为它足够优美,有递归结构,有左子树和右子树,还有二分的思想. emm这个文章打算自用,就不写那些基本的操作了... 1° 简单的懒标记( ...

  6. 线段树【洛谷P2894】 [USACO08FEB]酒店Hotel

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

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

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

  8. P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel 简单的线段树维护区间信息. 维护三个值,一个是从左端点能拓展的长度,一个是从右端点能脱产的的长度.另一个是整个区间内的最大连续零一长度. 记录这三个 ...

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

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

随机推荐

  1. 2015.5.11 string与byte[]相互转换

    string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); 反过来,byte[]转成stri ...

  2. apache将不带www域名301重定向到带www的域名的配置方法

    #强制重定向到wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^jb51.net/ [NC]RewriteRule ^(.*)$ http://www.jb51 ...

  3. Git中远程仓库的使用

    1.查看当前的远程库 要查看当前配置有哪些远程仓库,可以用 git remote 命令,它会列出每个远程库的简短名字.在克隆完某个项目后,至少可以看到一个名为 origin 的远程库,Git 默认使用 ...

  4. mysql的安装以及简单的命令符

    在百度当中输入mySQL就可以下载了. 我们只需要一路的点击next就好了,注意,我们在安装的过程当中它会问我们是否要安装路径,我么要选择是. 在显示安装完成之后呢,我们会看到一个复选框,上面写着是否 ...

  5. 《Android安全机制解析与应用实践》笔记 第2章

    Android扩展了Linux内核安全模型的用户与权限机制,将多用户操作系统的用户隔离机制巧妙地移植为应用程序隔离.在linux中,一个用户标识(UID)识别一个给定用户:在Android上,一个UI ...

  6. 关于FILL_PARENTE和match_parent布局属性

    在观看早期的代码的时候,经常会看到FILL_PARENT属性,但是新的代码中却有了MATCH_PARENT 那么,两者有何区别呢? 答案是,没有,只是换了个名字而已,均为-1

  7. 奇葩问题 eclipse下 maven项目 java Resource报个小红叉,然而里面却没有小红叉

    之前没注意,不知是一开始就有还是这两天才有,说下解决方案: 右击项目“Properties”,在弹出的“Properties”的左侧边框,单击“Project Facets”,打开“Project F ...

  8. STM32 C++编程 003 USART(串口)类

    使用 C++ 语言给 STM32 编写一个 Usart 类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编程 的 ...

  9. hdu4283 You Are the One

    传送门 题目 The TV shows such as You Are the One has been very popular. In order to meet the need of boys ...

  10. Luogu 2149 [SDOI2009]Elaxia的路线

    感觉这题可以模板化. 听说spfa死了,所以要练堆优化dijkstra. 首先对$x_{1},y_{1},x_{2},y_{2}$各跑一遍最短路,然后扫一遍所有边看看是不是同时在两个点的最短路里面,如 ...