【题目链接】

点击打开链接

【算法】

线段树

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

空房间个数

【代码】

#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. Qt 多语言支持

    简介 Qt 多语言支持很强大,很好用. 首先要强调的是程序中需要翻译的字符串最好都用 tr("message") 这种形式,这里的 "message" 就是需要 ...

  2. [luoguP2709] 小B的询问(莫队)

    传送门 个数 1 2 3 4 5 答案 1 4 9  16 25 做差 1 3 5 7 9 显然增加一个数只需要增加 ton[a[x]] << 1 | 1 即可 减去一个数也减去这个 注意 ...

  3. SOJ 2785_Binary Partitions

    [题意]将一个数用二进制数表示,求一共有多少种表示方法. [分析]思路一:完全背包 [代码] #include <iostream> #include <cstdio> #in ...

  4. jsp内置对象之response、out、config、exception、pageContext。

    本文是对Jsp内置对象的response.out.config.exception.pageContext知识点的详细总结. response对象 Response内置对象和request内置对象是相 ...

  5. 有用的 SystemTap 脚本

    https://segmentfault.com/a/1190000000680628 https://github.com/posulliv/stap

  6. 记一次调试python内存泄露的问题

    转载:http://www.jianshu.com/p/2d06a1a01cc3 这两天由于公司需要, 自己编写了一个用于接收dicom文件(医学图像文件)的server. 经过各种coding-de ...

  7. 踩坑录-mysql不允许远程连接(错误码:1130) Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“

    每次搭建mysql环境都会遇见同样的问题,在此分享一下踩坑笔录. 一.问题描述 安装成功后,本地直接链接远程mysql,默认为不允许远程访问,则客户端提示1130 - Host'xxx.xxx.xxx ...

  8. 字符串处理(正则表达式、NSScanner扫描、CoreParse解析器)-b

    搜索 在一个字符串中搜索子字符串 最灵活的方法 1 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptio ...

  9. _DataStructure_C_Impl:图的遍历

    #include<stdio.h> #include<stdlib.h> #include<string.h> //图的邻接表类型定义 typedef char V ...

  10. “取出数据表中第10条到第20条记录”的sql语句+select top 使用方法

    1.首先.select top使用方法: 參考问题  select top n * from和select * from的差别 select * from table --  取全部数据.返回无序集合 ...