BZOJ1593 [Usaco2008 Feb]Hotel 旅馆
裸上线段树,就是记的东西有点多。。。
每个点记区间左端最长0,右端最长0,中间最长0,和tag表示是否全为0/1
直接更新就好,查询的时候先查左儿子,然后查中间,最后查右儿子。。。
/**************************************************************
Problem: 1593
User: rausen
Language: C++
Result: Accepted
Time:388 ms
Memory:4404 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std; inline int read(); struct seg {
seg *ls, *rs;
int mxl, mxm, mxr, len, tag; #define Len (1 << 16)
void* operator new(size_t, int x) {
static seg *mempool, *c;
if (c == mempool)
mempool = (c = new seg[Len]) + Len;
c -> ls = c -> rs = NULL;
c -> mxl = c -> mxr = c -> mxm = c -> len = x, c -> tag = -;
return c++;
}
#undef Len #define mid (l + r >> 1)
#define Ls this -> ls
#define Rs this -> rs
inline void fill(int d) {
if (d) this -> mxl = this -> mxr = this -> mxm = ;
else this -> mxl = this -> mxr = this -> mxm = this -> len;
this -> tag = d;
} inline void push() {
if (~this -> tag) {
if (Ls) Ls -> fill(this -> tag);
if (Rs) Rs -> fill(this -> tag);
this -> tag = -;
}
}
inline void update() {
this -> mxl = Ls -> mxl + (Ls -> mxl == Ls -> len ? Rs -> mxl : );
this -> mxr = Rs -> mxr + (Rs -> mxr == Rs -> len ? Ls -> mxr : );
this -> mxm = max(max(Ls -> mxm, Rs -> mxm), Ls -> mxr + Rs -> mxl);
} void build(int l, int r) {
if (l == r) return;
Ls = new(mid - l + )seg, Ls -> build(l, mid);
Rs = new(r - mid)seg, Rs -> build(mid + , r);
} void modify(int l, int r, int L, int R, int d) {
if (L <= l && r <= R) {
this -> fill(d);
return;
}
this -> push();
if (L <= mid) Ls -> modify(l, mid, L, R, d);
if (mid < R) Rs -> modify(mid + , r, L, R, d);
this -> update();
} int query(int l, int r, int len) {
this -> push();
if (this -> mxm < len) return ;
if (Ls -> mxm >= len) return Ls -> query(l, mid, len);
if (Ls -> mxr + Rs -> mxl >= len) return mid - Ls -> mxr + ;
if (Rs -> mxm >= len) return Rs -> query(mid + , r, len);
}
#undef mid
#undef Ls
#undef Rs
} *segment; int n, m; int main() {
int i, oper, x, y;
n = read(), m = read();
segment = new(n)seg;
segment -> build(, n);
for (i = ; i <= m; ++i) {
oper = read();
if (oper == ) {
x = segment -> query(, n, y = read());
printf("%d\n", x);
if (x) segment -> modify(, n, x, x + y - , );
} else {
x = read(), y = read();
segment -> modify(, n, x, x + y - , );
}
}
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}
BZOJ1593 [Usaco2008 Feb]Hotel 旅馆的更多相关文章
- bzoj1593 [Usaco2008 Feb]Hotel 旅馆(线段树)
1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 758 Solved: 419[Submit ...
- 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel
题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...
- 【最长连续零 线段树】bzoj1593: [Usaco2008 Feb]Hotel 旅馆
最长连续零的线段树解法 Description 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负 责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大 ...
- 【分块】bzoj1593 [Usaco2008 Feb]Hotel 旅馆
分块,记录每个块内包括左端点的最大连续白段的长度, 整个块内的最大连续白段的长度, 和包括右端点的最大连续白段的长度. Because 是区间染色,所以要打标记. 至于怎样在O(sqrt(n))的时间 ...
- 1593: [Usaco2008 Feb]Hotel 旅馆
1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 391 Solved: 228[Submit ...
- 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并
题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- BZOJ 1593: [Usaco2008 Feb]Hotel 旅馆
Description 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 &l ...
- BZOJ 1593: [Usaco2008 Feb]Hotel 旅馆 [线段树]
传送门 题意: 操作1:找长为$len$的空区间并填满,没有输出$0$ 操作2:将$[l,r]$之间的区间置空 我真是太弱了这种线段树还写了一个半小时,中间为了查错手动模拟了$30min$线段树操作, ...
- 【BZOJ】1593: [Usaco2008 Feb]Hotel 旅馆
[算法]线段树(经典线段树上二分) [题意]n个房间,m个询问,每次订最前的连续x个的空房间,或退订从x开始y个房间,求每次订的最左房间号. [题解]关键在于找连续x个空房间,经典二分. 线段树标记s ...
随机推荐
- 【CC评网】2013.第38周 要阅读 要有好工具
要阅读,要有好工具 Reeder终于在ipad上推出了第二代版本,终于脱离了Google reader而独立存在: 自从Google reader关闭之后,我就在各种支持rss的阅读器中游荡,却总是找 ...
- css 集锦。
可以是 链接的下划线 去掉. a {text-decoration: none} position:absolute 绝对定位 position:relative 相对定位 ie 图片失真 -ms ...
- [poj2286]The Rotation Game (IDA*)
//第一次在新博客里发文章好紧张怎么办 //MD巨神早已在一个小时前做完了 The Rotation Game Time Limit: 15000MS Memory Limit: 150000K To ...
- 初始Hibernate框架技术
hibernate: 定义:ORM:Object Relational Mapping 对象 关系 映射 使用hibernate时几个必要的: 1.实体类 2.映射文件(类 -数据库表,属性-字段) ...
- iOS - OC NSSize 尺寸
前言 结构体,这个结构体用来表示事物的宽度和高度. typedef CGSize NSSize; struct CGSize { CGFloat width; CGFloat height; }; t ...
- Spring 框架的设计理念与设计模式分析
转载地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ Spring 作为现在最优秀的框架之一,已被广泛的使用,并 ...
- iOS常用define宏定义
1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([U ...
- 对SIGQUIT的实验 & Java dump
写了一个Java程序,sleep 20秒. package com.company; public class Main { public static void main(String[] args ...
- Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要
Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...
- UML中类之间的几种关系
类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition).泛化(generalizatio ...