POJ 3667 Hotel(线段树)
POJ 3667 Hotel
题意:有n个房间,如今有两个操作
1、找到连续长度a的空房间。入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0
2、清空[a, a + b - 1]的房间
思路:线段树的区间合并。记录下左边连续最长和右边连续最长空房间。和每一段的最大值。这样pushup的时候就是进行区间合并,注意查询的时候因为是要尽量左,所以先查左孩子,在查横跨左右的,在查右孩子
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) const int N = 50005; int n, m; struct Node {
int l, r, lsum, rsum, sum, sumv, lazy;
int size() {return r - l + 1;}
void gao(int v) {
lazy = v;
if (v) lsum = rsum = sum = 0;
else lsum = rsum = sum = r - l + 1;
sumv = l;
}
} node[N * 4]; void pushup(int x) {
if (node[lson(x)].lsum == node[lson(x)].size()) node[x].lsum = node[lson(x)].lsum + node[rson(x)].lsum;
else node[x].lsum = node[lson(x)].lsum;
if (node[rson(x)].rsum == node[rson(x)].size()) node[x].rsum = node[lson(x)].rsum + node[rson(x)].rsum;
else node[x].rsum = node[rson(x)].rsum;
node[x].sum = node[lson(x)].sum;
node[x].sumv = node[lson(x)].sumv;
if (node[x].sum < node[lson(x)].rsum + node[rson(x)].lsum) {
node[x].sum = node[lson(x)].rsum + node[rson(x)].lsum;
node[x].sumv = node[lson(x)].r - node[lson(x)].rsum + 1;
}
if (node[x].sum < node[rson(x)].sum) {
node[x].sum = node[rson(x)].sum;
node[x].sumv = node[rson(x)].sumv;
}
} void pushdown(int x) {
if (node[x].lazy != -1) {
node[lson(x)].gao(node[x].lazy);
node[rson(x)].gao(node[x].lazy);
node[x].lazy = -1;
}
} void build(int l, int r, int x = 0) {
node[x].l = l; node[x].r = r; node[x].lazy = -1;
if (l == r) {
node[x].lsum = node[x].rsum = node[x].sum = 1;
return;
}
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
pushup(x);
} void add(int l, int r, int v, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
node[x].gao(v);
return;
}
int mid = (node[x].l + node[x].r) / 2;
pushdown(x);
if (l <= mid) add(l, r, v, lson(x));
if (r > mid) add(l, r, v, rson(x));
pushup(x);
} int query(int v, int x = 0) {
if (node[x].l == node[x].r) {
if (node[x].sum >= v) return node[x].sumv;
return 0;
}
pushdown(x);
int ans = 0;
if (node[lson(x)].sum >= v)
ans = query(v, lson(x));
else if (node[lson(x)].rsum + node[rson(x)].lsum >= v) ans = node[lson(x)].r - node[lson(x)].rsum + 1;
else if (node[rson(x)].sum >= v)
ans = query(v, rson(x));
pushup(x);
return ans;
} int main() {
while (~scanf("%d%d", &n, &m)) {
build(1, n);
int op, a, b;
while (m--) {
scanf("%d%d", &op, &a);
if (op == 2) {
scanf("%d", &b);
add(a, a + b - 1, 0);
} else {
int tmp = query(a);
printf("%d\n", tmp);
if (tmp == 0) continue;
add(tmp, tmp + a - 1, 1);
}
}
}
return 0;
}
POJ 3667 Hotel(线段树)的更多相关文章
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...
- poj 3667 Hotel(线段树,区间合并)
Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...
- PKU 3667 Hotel(线段树)
Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- POJ 1823 Hotel 线段树
题目链接 线段树的区间合并. 和上一题差不多....第三种操作只需要输出maxx[1]的值就可以. #include <iostream> #include <vector> ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- [USACO08FEB]酒店Hotel 线段树
[USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...
随机推荐
- 微信小程序开发教程(三)项目目录及文件构成
第二章我们已经创建了一个Hello WXapplet示例小程序.我们从文件目录结构来了解Hello WXapplet项目的构成. 目录结构显示,在小程序项目的根目录下面包含3个app开头的文件(app ...
- 【分类讨论】bzoj3856 Monster
#include<cstdio> using namespace std; int T=0; long long h,a,b,k; int main() { freopen("b ...
- python3-开发面试题(python)6.22基础篇(1)
1.为什么学习Python? 1.语言排行榜 2.语言本身简洁,优美,功能超级强大的 3.跨平台 4.非常火爆的社区 5.用的公司的多 2.通过什么途径学习的Python? 某宝2.8就搞定了,跟着视 ...
- python3开发进阶-Django框架的起飞加速一(ORM)
阅读目录 ORM介绍 Django中的ORM ORM中的Model ORM的操作 一.ORM介绍 1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一 ...
- 生成唯一code
private String getCode() { List<String> ptypeCodeList = mapper.findCodeList(); String code = & ...
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...
- Ghost下的gho镜像分区工具
直接gho镜像还原,有fat32和ntfs 链接: https://pan.baidu.com/s/1dGl6v65 密码: tjuz
- C#将json字符串解析成对象
首先我们在客户端生成json字符串,通过ajax把该字符串传到服务器端 //这是一个以id,email,age的json字符串 var jdata="[{\"id\&quo ...
- 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架之工作流开发随笔三
前言 “厂长,APP的那几个功能都差不多了,接下来要做工作流,工作流这东西我完全没概念啊.” “查尔斯,一般来说工作流就是指将指定的数据.文件.任务按照预定的规则进行传递流转.比如说你要请假,拿个请假 ...
- md5代码实现
参考: 1.http://blog.csdn.net/iaccepted/article/details/8722444 2.http://hi.baidu.com/gh0st_lover/item/ ...