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(线段树)的更多相关文章

  1. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

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

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

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

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

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

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

  5. PKU 3667 Hotel(线段树)

    Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  6. POJ 1823 Hotel 线段树

    题目链接 线段树的区间合并. 和上一题差不多....第三种操作只需要输出maxx[1]的值就可以. #include <iostream> #include <vector> ...

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

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  8. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  9. [USACO08FEB]酒店Hotel 线段树

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

随机推荐

  1. Python与数据结构[3] -> 树/Tree[0] -> 二叉树及遍历二叉树的 Python 实现

    二叉树 / Binary Tree 二叉树是树结构的一种,但二叉树的每一个节点都最多只能有两个子节点. Binary Tree: 00 |_____ | | 00 00 |__ |__ | | | | ...

  2. 【Docker】基本命令使用介绍

    # docker命令行学习 ## docker run- docker run --help:老实说这条最管用了- docker run -it:交互模式,允许控制台输出 - docker run - ...

  3. java中为什么要用多线程

    我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread).线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可能和其他线程共享一些资源,比 ...

  4. 【字符串哈希】【莫队算法】bzoj3207 花神的嘲讽计划Ⅰ

    既然询问的长度是确定的,那么我们可以将所有长度为K的字串弄个哈希值出来,这样字串存在性=>哈希值存在性. 自然上溢哈希,base=107比较不错. 序列长度n=>n-K+1 询问区间[x, ...

  5. 【OpenJudge9267】【递推】核电站

    核电站 总时间限制: 5000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB [描述] 一个核电站有N个放核物质的坑,坑排列在一条直线上.如果连续M个坑中放入核物质,则会发生爆 ...

  6. [转]从此爱上iOS Autolayout

    原文地址 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程.这篇文章两个月前就想写下来,但 ...

  7. awk-使用

    http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html 命令格式: awk [-F field-separator] 'pat ...

  8. django+celery+redis实现运行定时任务

    0.目的 在开发项目中,经常有一些操作时间比较长(生产环境中超过了nginx的timeout时间),或者是间隔一段时间就要执行的任务. 在这种情况下,使用celery就是一个很好的选择.   cele ...

  9. Snapdragon profiler连android手机

    oppo11 晓龙660 找一根好用的usb数据线 去设置->开发者选项->usb调试 打开(十分钟会自动关,注意再开开) 去windows cmd ===adb devices 会列出这 ...

  10. vim使用指北 ---- Multiple Windows in Vim

    多窗口打开多个文件 vim -o file1 file2 ... ---- 默认上下分割窗口 vim -0n file1 file2 ... ---- vim默认会上下等分n个窗口 分割窗口 :[v] ...