【题目链接】

点击打开链接

【算法】

线段树

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

空房间个数

【代码】

#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. 【makefile】symbol <函数> : can't resolve symbol 问题分析

    调试程序的时候,在linux编译器上可以编译通过,但是编译生成的firmware烧录到板子上的后出现以下异常: can't resolve symbol,无法解析元素符号. review一下code, ...

  2. 读书笔记:《人有人的用处》------N.维纳. (2016.12.28)

    读书笔记:<人有人的用处>------N.维纳 ·某些系统可以依其总能量而和其他系统区别开来. ·在某些情况下,一个系统如果保持足够长时间的运转,那它就会遍历一切与其能量相容的位置和动量的 ...

  3. 80-Force Index,强力指标.(2015.7.1)

    Force Index 强力指标 Index,强力指标.(2015.7.1)" title="80-Force Index,强力指标.(2015.7.1)"> 观井 ...

  4. C51 继电器 个人笔记

    一句话 小电流控制大电流的开关. 电路图 给J2端口一个低电平,三极管导通,线圈吸合 一般用P1^4口连接 #include <reg51.h> #define u16 unsigned ...

  5. C51 使用端口 个人笔记

    使用整个端口的8个引脚: 八个引脚,需要8位2进制,2位十六进制 #define P0 led led = 0x3f; //led = ~0x3f; 使用某个端口的某一个引脚 sbit led = P ...

  6. Fiddler基本用法:手机抓包

    from:https://blog.csdn.net/gld824125233/article/details/52588275 电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤 ...

  7. xtu summer individual 5 D - Subsequence

    Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  8. 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

  9. 在mysql数据库中,文章表设计有啥好的思路

    Q: 用mysql设计一张文章表,不知道有啥好的思路! 我是这样的,应为考虑附件和图片,所以我的文章表除了有varchar(1000)的文章内容,还设置了个Bolb接收附件和图片. 我用的是mysql ...

  10. Arctic Network POJ - 2349

    The Department of National Defence (DND) wishes to connect several northern outposts by a wireless n ...