裸上线段树,就是记的东西有点多。。。

每个点记区间左端最长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 旅馆的更多相关文章

  1. bzoj1593 [Usaco2008 Feb]Hotel 旅馆(线段树)

    1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 758  Solved: 419[Submit ...

  2. 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel

    题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...

  3. 【最长连续零 线段树】bzoj1593: [Usaco2008 Feb]Hotel 旅馆

    最长连续零的线段树解法 Description 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负 责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大 ...

  4. 【分块】bzoj1593 [Usaco2008 Feb]Hotel 旅馆

    分块,记录每个块内包括左端点的最大连续白段的长度, 整个块内的最大连续白段的长度, 和包括右端点的最大连续白段的长度. Because 是区间染色,所以要打标记. 至于怎样在O(sqrt(n))的时间 ...

  5. 1593: [Usaco2008 Feb]Hotel 旅馆

    1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 391  Solved: 228[Submit ...

  6. 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并

    题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  7. BZOJ 1593: [Usaco2008 Feb]Hotel 旅馆

    Description 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 &l ...

  8. BZOJ 1593: [Usaco2008 Feb]Hotel 旅馆 [线段树]

    传送门 题意: 操作1:找长为$len$的空区间并填满,没有输出$0$ 操作2:将$[l,r]$之间的区间置空 我真是太弱了这种线段树还写了一个半小时,中间为了查错手动模拟了$30min$线段树操作, ...

  9. 【BZOJ】1593: [Usaco2008 Feb]Hotel 旅馆

    [算法]线段树(经典线段树上二分) [题意]n个房间,m个询问,每次订最前的连续x个的空房间,或退订从x开始y个房间,求每次订的最左房间号. [题解]关键在于找连续x个空房间,经典二分. 线段树标记s ...

随机推荐

  1. 学习Berkeley DB- 入门

    1 导言 首先,我们要了解Berkeley DB的一些基本特性,在IBM的开发网站上有篇文章对其有比较清晰的介绍: 这篇文章讲到了BDB的设计思想和核心数据结构.以及数据访问算法:并有常用函数使用范例 ...

  2. set集合的用法总结(转)

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  3. How to build a GUI in ROS with Qt / C++

    p { margin-bottom: 0.1in; direction: ltr; line-height: 120%; text-align: left; widows: 2; orphans: 2 ...

  4. python tools: iPython Notebook

    Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...

  5. php 在函数定义变量的时候,变量前加了 @ 符号是什么意思

    今天在看到一段代码,如下 <?php $test=@'kdksf?cc'; 加上@ 是 就可以不用\来表示转义字符了

  6. eclipse 设置jvm 内存

    Eclipse 中设置JVM 内存 今天在eclipse 中测试把文档转换为图片的时候,报出了下面的错误: java.lang.OutOfMemoryError: Java heap space 从上 ...

  7. iOS - OC 数据持久化

    1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...

  8. Nginx RTMP 专题

    说明: 记录器 - 记录器名称 path - 记录文件路径(recorded file path) (/tmp/rec/mystream-1389499351.flv)filename - 省略目录的 ...

  9. odoo关联表查询方法

    testinfo=self.env['product.attribute.value'].search([('product_ids.product_tmpl_id','=',2047)]) for ...

  10. 【ufldl tutorial】Convolution and Pooling

    卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...