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

每个点记区间左端最长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. 3.mybatis注解

    在上篇2.mybatis入门实例(一) 连接数据库进行查询的基础上 1.添加Mapper接口:UserMapper接口,并使用mybatis的注解 import java.util.List; imp ...

  2. run a Freight robot (1)

    1. Freight robot The Fetch and Freight Research Edition Robots are indoor laboratory robots. Coordin ...

  3. C#正则表达式编程(三):Match类和Group类用法

    前面两篇讲述了正则表达式的基础和一些简单的例子,这篇将稍微深入一点探讨一下正则表达式分组,在.NET中正则表达式分组是用Match类来代表的.首先先看一段代码: /// <summary> ...

  4. 个人阅读作业 The Last

    对于软件工程M1/M2的总结: 假象-MO 在团队开发的前期,我感觉自己其实给了自己很多的期待,因为一直希望着自己可以在团队中担任一个角色,用自己的力量为团队多做事情,也给了其他人一些假象,那就是看起 ...

  5. JavaWeb 7 Servlet

    7 Servlet Servlet学习的大纲:1. servlet概念及相关接口简介2. servet 执行过程3. servlet路径映射4. 缺省servlet          --应用5. s ...

  6. 反演dp经典

    咋一看,至少要用3^n才能做到. 但. 首先定义: 可以发现只要求出a' b' 那么直接可以得出c' 那么如何求a'呢 //dp求a',其实就是分别用[0,n)来更新a' ; i < n; i+ ...

  7. linux套件安装过程中configure,make,make install的作用

    ./configure,make,make install都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤.其中: ./configure是检测程序文件,用来检测你的安装平 ...

  8. python linux 磁盘操作

    #coding:utf-8 ''' __author__ = 'similarface' connection:841196883@qq.com 磁盘操作 ''' import psutil impo ...

  9. ctrl + d 在phpstorm 和 eclipse 中的不同含义

    Ctrl + d 在phpstrom是复制一行,非常的方便,但是eclipse中却是删除一行,非常的特别.感觉上,phpstorm更注重鼠标,但eclipse貌似更多鼠标和键盘的操作, 默认情况下[p ...

  10. xcode5 python 开发环境

    在xcode5 下配置 python开发环境 1:默认mac下已经集成了python的开发库,先找到集成的python库的目录 终端 which python 记下当前的python 路径 /usr/ ...