【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 ...
随机推荐
- LeetCode(50) Pow(x,n)
题目 Implement pow(x, n). Show Tags Show Similar Problems 分析 一个不利用标准幂次函数的,求幂算法实现. 参考了一个很好的解析博客:Pow(x,n ...
- C++动态申请内存 new T()与new T[]的区别
new与delete 我们知道,new和delete运算符是用于动态分配和撤销内存的运算符. new的用法 开辟单变量地址空间: i. 如 new int ; 指开辟一个存放数组的存储空间,返回一个指 ...
- web环境搭建
[服务器] 硬件设备---计算机 软件 [作用] 作为web服务器运行.可以管理web项目 [目录说明] bin :存放各类可以执行文件,如:startup.bat conf:存放各类配置文件,常用配 ...
- python接口测试之Http请求(三)
python的强大之处在于提供了很多的标准库,这些标准库可以直接调用,本节部分,重点学习和总结在 接口测试中Python的Http请求的库的学习. 首先来看httplib,官方的解释为:本模块定义了类 ...
- hihoCoder#1082 然而沼跃鱼早就看穿了一切
原题地址 字符串匹配+替换 注意替换串和原串长度是不等的,所以替换完还要进行收缩 可以顺带练习一下KMP 代码: #include <iostream> #include <cstr ...
- [POJ3463] Sightseeing(次短路 Heap + Dijkstra)
传送门 用dijkstra比较好,spfa可能有的重复 dis[x][2]:dis[x][0]表示起点到x的最短路.dis[x][1]表示起点到x的次短路: tot[x][2]:tot[x][0]表示 ...
- 网络编程进阶:并发编程之协程、IO模型
协程: 基于单线程实现并发,即只用一个主线程(此时可利用的CPU只有一个)情况下实现并发: 并发的本质:切换+保存状态 CPU正在运行一个任务,会在两种情况下切走去执行其他任务(切换有操作系统强制控制 ...
- 莎拉公主的困惑(bzoj 2186)
Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...
- 【HDOJ6148】Valley Numer(数位DP)
题意: 1≤T≤200 ● 1≤length(N)≤100 思路: 设f[i,j,k,l]为第i位为j,前i位是否贴上限(0/1),递减或递增(0/1)方案数 g[i,j,k]为不到n位,第i位为j, ...
- 【HDOJ6118】度度熊的交易计划(费用流)
题意: 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个 ...