【题目链接】

点击打开链接

【算法】

线段树

对于一个节点,记录它从左端点延伸的最多的空房间的个数,从右端点延伸的最多的空房间个数,和该区间最多的连续

空房间个数

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 50010 int n,m,opt,pos,x,d; struct SegmentTree
{
struct Node
{
int l,r,lm,rm,mx,tag;
} Tree[MAXN*];
inline void build(int index,int l,int r)
{
int mid;
Tree[index].l = l;
Tree[index].r = r;
Tree[index].lm = Tree[index].rm = Tree[index].mx = r - l + ;
Tree[index].tag = -;
if (l == r) return;
mid = (l + r) >> ;
build(index<<,l,mid);
build(index<<|,mid+,r);
}
inline void pushdown(int index)
{
int ql = Tree[index].l,qr = Tree[index].r;
int mid = (ql + qr) >> ;
if (ql == qr) return;
if (Tree[index].tag == )
{
Tree[index<<].lm = Tree[index<<].rm = Tree[index<<].mx = mid - ql + ;
Tree[index<<|].lm = Tree[index<<|].rm = Tree[index<<|].mx = qr - mid;
Tree[index<<].tag = Tree[index<<|].tag = ;
Tree[index].tag = -;
}
if (Tree[index].tag == )
{
Tree[index<<].lm = Tree[index<<].rm = Tree[index<<].mx = ;
Tree[index<<|].lm = Tree[index<<|].rm = Tree[index<<|].mx = ;
Tree[index<<].tag = Tree[index<<|].tag = ;
Tree[index].tag = -;
}
}
inline void update(int index)
{
int ql = Tree[index].l,qr = Tree[index].r;
int mid = (ql + qr) >> ;
if (Tree[index<<].lm == mid - ql + ) Tree[index].lm = Tree[index<<].lm + Tree[index<<|].lm;
else Tree[index].lm = Tree[index<<].lm;
if (Tree[index<<|].rm == qr - mid) Tree[index].rm = Tree[index<<|].rm + Tree[index<<].rm;
else Tree[index].rm = Tree[index<<|].rm;
Tree[index].mx = max(max(Tree[index<<].mx,Tree[index<<|].mx),Tree[index<<].rm+Tree[index<<|].lm);
}
inline void modify(int index,int l,int r,int val)
{
int mid,ql,qr;
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].lm = Tree[index].rm = Tree[index].mx = (val ^ ) * (r - l + );
Tree[index].tag = val;
} else
{
pushdown(index);
ql = Tree[index].l;
qr = Tree[index].r;
mid = (ql + qr) >> ;
if (mid >= r) modify(index<<,l,r,val);
else if (mid + <= l) modify(index<<|,l,r,val);
else
{
modify(index<<,l,mid,val);
modify(index<<|,mid+,r,val);
}
update(index);
}
}
inline int query_pos(int index,int d)
{
int mid,ql,qr;
ql = Tree[index].l; qr = Tree[index].r;
mid = (ql + qr) >> ;
if (ql == qr) return ql;
pushdown(index);
if (Tree[index<<].mx >= d) return query_pos(index<<,d);
else if (Tree[index<<].rm + Tree[index<<|].lm >= d) return mid - Tree[index<<].rm + ;
else return query_pos(index<<|,d);
}
inline int query()
{
return Tree[].mx;
}
} T; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
} int main() { read(n); read(m);
T.build(,,n);
while (m--)
{
read(opt);
if (opt == )
{
read(d);
if (T.query() < d) writeln();
else
{
pos = T.query_pos(,d);
writeln(pos);
T.modify(,pos,pos+d-,);
}
} else
{
read(x); read(d);
T.modify(,x,x+d-,);
}
} return ; }

【USACO 2008FEB】 旅馆的更多相关文章

  1. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  2. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  3. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  4. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  5. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  6. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  7. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  8. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

  9. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

随机推荐

  1. buf.toJSON()

    buf.toJSON() 返回:{Object} 返回该 Buffer 实例的 JSON 表达式.当字符串化一个 Buffer 实例时会隐式调用 JSON.stringify() 这个函数. 例子: ...

  2. 22Spring基于配置文件的方式配置AOP

    直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...

  3. MySQL-----查

    数据库在我眼中就是增删改查,而查,我觉得是数据库最费劲的,数据库的花式查,各种查.下面咱们不废话,就是干. 查: **查数据库版本** select version(); **查登录用户** sele ...

  4. 使用C++调用pytorch模型(Linux)

    前言 模型转换思路通常为: Pytorch -> ONNX -> TensorRT Pytorch -> ONNX -> TVM Pytorch -> 转换工具 -> ...

  5. CSU 1214 找最大异或值

    题目大意: 给定一堆数,从中找2个数异或得到的最大值 直接暴力会超时,我们要考虑对于每一个数去匹配找到异或的最大值,我们希望2进制越前面的数尽可能都为1 所以我们用 0-1 字典树保存这些数,因为一个 ...

  6. 【bzoj2527】[Poi2011]Meteors(树状数组(单点查询,区间修改)+整体二分)

    [bzoj2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...

  7. Jquery EasyUI动态生成Tab

    function addTab(title, url) { if ($('#tt').tabs('exists', title)) { $('#tt').tabs('select', title); ...

  8. Jquery那些事

    Jquery选择器介绍: 我们可以通过Jquery选择器从网页文档中找到我们需要的DOM节点: 主要还时看文档!! (1)基本选择器 属性id    类别class       文档标签 (2)属性选 ...

  9. 洛谷 P3984 高兴的津津

    P3984 高兴的津津 题目描述 津津上高中了.她在自己的妈妈的魔鬼训练下,成为了一个神犇,每次参加一次OI比赛必拿Au虐全场.每次她拿到一个Au后就很高兴.假设津津不会因为其它事高兴,并且她的高兴会 ...

  10. 应用CLR的线程池

    大家都知道这个线程的建立和销毁都需要很大的性能开销,当有比较多且不同的任务需要完成时,可以考虑使用线程池来管理这些线程.在以windows NT为内核的操作系统上每个进程都包含一个线程池,在线程池中存 ...