题目链接

题目

题目描述

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.

输入描述

  • 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
4
7
0
5

题解

知识点:线段树,二分。

考虑连续区间的权值最大值,需要维护三个信息,区间连续空房个数 \(emp\) 、区间从左端点开始的连续空房个数 \(lemp\) 、区间从右端点开始的连续空房的个数 \(remp\) 。

合并时, \(mx\) 为左右子区间 \(mx\) 和左子区间 \(remp\) 加右子区间 \(lemp\) 之和取最大值。 \(lemp,remp\) 需要考虑跨越左右区间的特殊情况,因此需要维护区间长度 \(len\) 。例如,若左子区间的 \(lemp\) 等于左子区间的 \(len\) ,那么区间 \(lemp\) 就是左子区间 \(len\) 加右子区间 \(lemp\) 否则继承左子区间的 \(lemp\) 即可,区间 \(remp\) 同理。

因此,区间信息需要维护 \(len,emp,lemp,remp\) 。

接下来是查找第一个连续空位大于等于 \(val\) 的位置,利用线段树上二分即可解决。首先判断是否存在,之后分三类情况:

  1. 若左子区间 \(emp\) 大于等于 \(val\) ,则查询左子区间。
  2. 否则若跨越左右子区间的空位,即左子区间的 \(remp\) 加右子区间的 \(lemp\) 之和,大于等于 \(val\) ,则直接返回位置。
  3. 否则查询右子区间。

区间修改维护一个信息,修改种类 \(upd\) , 有三个值 \(0/1/-1\) 表示未修改、全部入住、全部离开。 修改很朴素,看代码就行。

时间复杂度 \(O((n+m) \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; struct T {
int len;
int lemp, remp;
int emp;
static T e() { return{ 0,0,0,0 }; }
friend T operator+(const T &a, const T &b) {
return{
a.len + b.len,
a.lemp == a.len ? a.len + b.lemp : a.lemp,
b.remp == b.len ? a.remp + b.len : b.remp,
max({a.emp,b.emp,a.remp + b.lemp})
};
}
}; struct F {
int upd;
static F e() { return { 0 }; }
T operator()(const T &x) {
if (upd == 1)
return{
x.len,
0,0,
0
};
if (upd == -1)
return{
x.len,
x.len,x.len,
x.len
};
return x;
}
F operator()(const F &g) {
return { upd ? upd : g.upd };
}
}; class SegmentTreeLazy {
int n;
vector<T> node;
vector<F> lazy; void push_down(int rt) {
node[rt << 1] = lazy[rt](node[rt << 1]);
lazy[rt << 1] = lazy[rt](lazy[rt << 1]);
node[rt << 1 | 1] = lazy[rt](node[rt << 1 | 1]);
lazy[rt << 1 | 1] = lazy[rt](lazy[rt << 1 | 1]);
lazy[rt] = F::e();
} void update(int rt, int l, int r, int x, int y, F f) {
if (r < x || y < l) return;
if (x <= l && r <= y) return node[rt] = f(node[rt]), lazy[rt] = f(lazy[rt]), void();
push_down(rt);
int mid = l + r >> 1;
update(rt << 1, l, mid, x, y, f);
update(rt << 1 | 1, mid + 1, r, x, y, f);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
} int query(int rt, int l, int r, int val) {
if (l == r) return l;
push_down(rt);
int mid = l + r >> 1;
if (node[rt << 1].emp >= val) return query(rt << 1, l, mid, val);
if (node[rt << 1].remp + node[rt << 1 | 1].lemp >= val) return mid - node[rt << 1].remp + 1;
return query(rt << 1 | 1, mid + 1, r, val);
} public:
SegmentTreeLazy(int _n = 0) { init(_n); } void init(int _n) {
n = _n;
node.assign(n << 2, T::e());
lazy.assign(n << 2, F::e()); function<void(int, int, int)> build = [&](int rt, int l, int r) {
if (l == r) return node[rt] = { 1,1,1,1 }, void();
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
};
build(1, 1, n);
} void update(int x, int y, F f) { update(1, 1, n, x, y, f); } int query(int val) {
if (node[1].emp < val) return 0;
return query(1, 1, n, val);
}
}; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
SegmentTreeLazy sgt(n);
while (m--) {
int op;
cin >> op;
if (op == 1) {
int d;
cin >> d;
int pos = sgt.query(d);
if (pos) sgt.update(pos, pos + d - 1, { 1 });
cout << pos << '\n';
}
else {
int x, d;
cin >> x >> d;
sgt.update(x, x + d - 1, { -1 });
}
}
return 0;
}

NC24961 Hotel的更多相关文章

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

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

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

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

  3. HDU - Hotel

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  4. 【POJ3667】Hotel

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  5. POJ-2726-Holiday Hotel

    Holiday Hotel   Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8302   Accepted: 3249 D ...

  6. Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot evaluate com.hotel.Object_$$_jvst485_15.toString()

    数据库字段和类Object属性不匹配,Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot eval ...

  7. poj 3667 Hotel(线段树,区间合并)

    Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...

  8. [POJ3667]Hotel(线段树,区间合并)

    题目链接:http://poj.org/problem?id=3667 题意:有一个hotel有n间房子,现在有2种操作: 1 a,check in,表示入住.需要a间连续的房子.返回尽量靠左的房间编 ...

  9. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

  10. Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟

    A. Saitama Destroys Hotel   Saitama accidentally destroyed a hotel again. To repay the hotel company ...

随机推荐

  1. 【SI】source insight4 添加指定类型的文件

    Options->File Type Options 红框可选择是否将指定类型的文件添加到工程 绿框可添加自定义文件类型,如汇编*.s;*.S 蓝框可新增文件类别,用于自定义文件类型 如不需将t ...

  2. STM32F429 实测基本数据类型占用空间

    实测代码 1 void CalculateDataTypeSize(void) 2 { 3 printf("sizeof(char} = %u\r\n", sizeof(char) ...

  3. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.12.14)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  4. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.11.22)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  5. [转帖]Harbor:修改默认的172网段

    背景: harbor 默认启动会随机创建 172 网段的ip地址,跟集群规划的网段冲突 Harbor 网段修改步骤 0. 原来Harbor占用的网段 # 网桥名:harbor_harbor [root ...

  6. [转帖]Jmeter中如何读取MYSQL数据作为请求参数

    在项目测试过程中,我经常需要将数据库中的数据作为参数传递到请求中.Jmeter中MYSQL数据库连接操作过程如下: 1.下载/n导入mysql的jdbc驱动包 下载mysql驱动包地址: http:/ ...

  7. [转帖]Web性能优化工具WebPageTest(二)——性能数据

    Web性能优化工具WebPageTest(二)--性能数据 https://www.cnblogs.com/strick/p/6681692.html 在前一篇<配置>完成后,点击&quo ...

  8. [转帖]jumpserver (Linux资产管理快速入门)

    准备工作 准备三台虚拟机,一台作为jumpserver的服务端,两台作为测试端. 一.安装好jump server后,输入IP地址登录 [192.168.2.111为本机测试地址] 二.创建用户组 这 ...

  9. [转帖]【JVM】GC算法与垃圾收集器

    引入 java 语言中一个显著的特点就是引入了java回收机制,是c++程序员最头疼的内存管理的问题迎刃而解,它使得java程序员在编写程序的时候不在考虑内存管理.由于有个垃圾回收机制,可以有效的防止 ...

  10. 你不知道的<input type="file">的小秘密

    限制file上传类型 很多时候,我们都需要使用 <input type="file"> 进行文件上传. 在上传的时候,我们需要对文件类型进行限制. 如果上传图片的时候. ...