昨天学习了线段树的延迟标记;

发现自己还有区间合并没有学;

抓紧时间学习一下;

代码:

 #include<cstdio>
#include<algorithm>
#define maxn 200005
using namespace std; struct tree
{
int l,r,msum,lsum,rsum,flag;
tree *right,*left;
}tr[maxn];
int nonocount; void build(tree *root,int l,int r)
{
root->l=l,root->r=r,root->flag=-;
root->msum=root->lsum=root->rsum=r-l+;
if(l==r)return;
int mid=(l+r)>>;
nonocount++;
root->left=tr+nonocount;
nonocount++;
root->right=tr+nonocount;
build(root->left,l,mid);
build(root->right,mid+,r);
} void pushdown(tree *root,int mid)
{
if(root->flag!=-)
{
root->left->flag=root->right->flag=root->flag;
int x=root->flag?:mid-(mid>>);
root->left->lsum=root->left->msum=root->left->rsum=x;
int y=root->flag?:(mid>>);
root->right->lsum=root->right->msum=root->right->rsum=y;
root->flag=-;
}
} void pushup(tree *root,int mid)
{
root->lsum=root->left->lsum;
root->rsum=root->right->rsum;
if(root->lsum==mid-(mid>>))root->lsum+=root->right->lsum;
if(root->rsum==(mid>>))root->rsum+=root->left->rsum;
root->msum=max(root->right->lsum+root->left->rsum,max(root->left->msum,root->right->msum));
} void update(tree *root,int l,int r,int c)
{
int x=root->r-root->l+;
if(l<=root->l&&root->r<=r)
{
root->msum=root->lsum=root->rsum=c?:x;
root->flag=c;
return;
}
pushdown(root,x);
int mid=(root->l+root->r)>>;
if(l<=mid)update(root->left,l,r,c);
if(r>mid) update(root->right,l,r,c);
pushup(root,x);
} int query(tree *root,int w)
{
int x=root->r-root->l+;
if(root->l==root->r)return root->l;
pushdown(root,x);
int mid=(root->l+root->r)>>;
if(root->left->msum>=w)return query(root->left,w);
else if(root->left->rsum+root->right->lsum>=w)return mid-root->left->rsum+;
return query(root->right,w);
}
int n,m,a,b,c;
int main()
{
scanf("%d%d",&n,&m);
build(tr,,n);
while(m--)
{
scanf("%d",&a);
if(a==)
{
scanf("%d",&b);
if(tr->msum<b)puts("");
else
{
int p=query(tr,b);
printf("%d\n",p);
update(tr,p,p+b-,);
}
}
else
{
scanf("%d%d",&b,&c);
update(tr,b,b+c-,);
}
}
return ;
}

poj 3667 Hotel的更多相关文章

  1. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  2. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

  3. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  4. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  5. POJ 3667 Hotel(线段树+区间合并)

    http://poj.org/problem?id=3667 题意: 有N个房间,M次操作.有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示 ...

  6. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 题目大意:一共有n个房间,初始时都是空的,现在有m个操作,操作有以下两种: 1.1 d :询问是否有连续d个空的房间,若有则输出 ...

  7. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  8. poj 3667 Hotel(线段树,区间合并)

    Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...

  9. (简单) POJ 3667 Hotel,线段树+区间合并。

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  10. Poj 3667——hotel——————【线段树区间合并】

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 Descriptio ...

随机推荐

  1. 基于heartbeat的单播方式实现tomcat高可用

    1.节点规划 在master.backup节点上添加eth0.eth1两网卡,具体添加过程,参考“基于VMware为CentOS 6.5配置两个网卡” 2.IP规划   master backup e ...

  2. cocos2d-x 2.1.4学习笔记之HelloWorld分析

    下面截图是HelloWorld项目下的文件夹结构 这是用python命令生成的项目,在创建过程中默认生成多个平台的程序文件. 1.“resource”文件夹 该文件夹主要用于存放游戏中需要的图片.音频 ...

  3. kafka Windows客户端Linux服务器---转

    原文:http://blog.csdn.net/jingshuigg/article/details/25001979 一.对于服务器端的搭建可以参考上一篇文章:kafka单机版环境搭建与测试 服务器 ...

  4. Redis Windows版安装及简单使用

    1.Redis简介及优势 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次 ...

  5. mysql锁表和解锁语句分享

    对于MySQL来说,有三种锁的级别:页级.表级.行级   页级的典型代表引擎为BDB.  表级的典型代表引擎为MyISAM,MEMORY以及很久以前的ISAM.  行级的典型代表引擎为INNODB.  ...

  6. CI框架篇之模型篇--AR操作(2)

    CodeIgniter 和众多的框架一样,有属于自己的一套对数据库的操作方式,本框架更是如此 有属于自己的一套对数据库的安全并且简单的操作, 成为AR操作:下面来对AR操作进行介绍: 首先,确定要启动 ...

  7. ==和equals

  8. 导出csv文件

    protected void Button1_Click(object sender, EventArgs e)     {         DataTable dt = new DataTable( ...

  9. sql Server 触发器 调用java.

    在这里,通过 xp_cmdshell 调用java程序. 步骤: 1)开启 xp_cmdshell(sql Server 2008): 通过如下命令开启: -- To allow advanced o ...

  10. C#中volatile的用法

    恐怕比较一下volatile和synchronized的不同是最容易解释清楚的.volatile是变量修饰符,而synchronized则作用于一段代码或方法:看如下三句get代码: int i1;  ...