【USACO 2008FEB】 旅馆
【题目链接】
【算法】
线段树
对于一个节点,记录它从左端点延伸的最多的空房间的个数,从右端点延伸的最多的空房间个数,和该区间最多的连续
空房间个数
【代码】
#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】 旅馆的更多相关文章
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- 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 ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
- USACO翻译:USACO 2012 JAN三题(2)
USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...
随机推荐
- 有向图连通分量SCC
在无向图中,如果从顶点vi到顶点vj有路径,则称vi和vj连通.如果图中任意两个顶点之间都连通,则称该图为连通图,否则,称该图为非连通图,则其中的极大连通子图称为连通分量,这里所谓的极大是指子图中包含 ...
- 语法,if,while循环,for循环
目录 一.语法 二.while循环 三.for循环 一.语法 if: if判断其实是在模拟人做判断.就是说如果这样干什么,如果那样干什么.对于ATM系统而言,则需要判断你的账号密码的正确性. if 条 ...
- Spider-Python爬虫之PyQuery基本用法
1.安装方法 pip install pyquery 2.引用方法 from pyquery import PyQuery as pq 3.简介 pyquery 是类型jquery 的一个专供pyth ...
- Courses on Turbulence
Courses on Turbulence Table of Contents 1. Lecture 1.1. UIUC Renewable energy and turbulent environm ...
- source insight中的快捷键总结
1.快捷键 1,Shift+F8高亮显示指定字符. 2,Ctrl+F找出来的结果用F4,F3前进后退查找. 3,Alt+,后退alt+.前进查找关键字. 4,Alt+G或者F5跳转到某个固定的行号. ...
- 关于SELECT 逻辑的执行顺序问题
不会有大多数人都和我一样的认为,是先进行的Where 剔除结果集,再进行Join的吧 SQL server 2014 逻辑执行标准: https://msdn.microsoft.com/en-us/ ...
- C51 定时器/计数器 个人笔记
C51的周期 结构图 两个功能寄存器 51单片机定时/计数器的工作由两个特殊功能寄存器控制.TMOD用于设置其工作方式:TCON用于控制其启动和中断申请. 工作方式寄存器TMOD 其中方式一和方式二常 ...
- CSU1217
就跟数字出现奇数次道理是一样的,将一个数转化为2进制后找出现奇数次个1的位置,最后将其输出来便是出现奇数次的数 #include <cstdio> int main() { int n,a ...
- MVC view页面需要多个model,复杂网页的处理
需求描述 一个比较复杂的页面,界面中包含的元素数据来自于许多个有关联或者无关联的表,然后我们要做的就是将数据呈现在界面上. 10年前大概都是这么干的 直接写一个复杂的SQL语句,返回一个包含所需数据的 ...
- js的声明与引入
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...