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

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

抓紧时间学习一下;

代码:

 #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. Director Scene Layer and Sprite

    Scenes Director Layers Multiple Layers Example: Sprites References Scenes A scene (implemented with ...

  2. 深度剖析:CDN内容分发网络技术原理--转载

    1.前言 Internet的高速发展,给人们的工作和生活带来了极大的便利,对Internet的服务品质和访问速度要求越来越高,虽然带宽不断增加,用户数量也在不断增加,受Web服务器的负荷和传输距离等因 ...

  3. ubuntu常用文件搜索命令 分类: linux 学习笔记 ubuntu 2015-07-05 15:40 84人阅读 评论(0) 收藏

    1.find find [搜索路径] [搜索关键字] 比如查找/test中文件名为t5.tmp的文件: 查找根目录下大于100M的文件 注意,这里的204800单位是块,1块=512字节 在根目录下查 ...

  4. Cocopods -第三方库的管理

    前言 什么是CocoaPods? CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods ...

  5. Winform获取应用程序的当前路径

    //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Location; result: X:\xxx\xxx\xxx.exe ...

  6. 无法绑定到新的显示成员,参数名:newDisplayMember

    此问题不是网上说的 DisplayMember 等先后顺序问题,即使更换绑定数序,只是把错误覆盖而已(绑定的是对象的类名) ValueMember = "Id"; DisplayM ...

  7. tomcat的server.xml详解

    Tomcat服务器是由一系列可配置的组件构成,其核心组件是Catalina   Servlet容器,它是所有其他Tomcat组件的顶层容器.Tomcat的组件可以在<CATALINA_HOME& ...

  8. jQuery Ajax(load,post,get,ajax)用法与详解

    今天看到群里面有网友们问到Jquery Ajax的(load,post,get,ajax)之间的区别,现在整理了一篇文章出来,希望可以帮到网友们,首先我们先来看一些简单的方法, 这些方法都是对jQue ...

  9. while循环的跳出

    今天在编码时突然产生一个疑问:程序中有一个while循环,循环体执行的是某个附带条件限制的操作.我现在想达到的目的是 => 条件成立,就执行操作,并跳出循环:条件不成立就跳出当次的while循环 ...

  10. ios专题 -线程互斥与同步

    [原创]http://www.cnblogs.com/luoguoqiang1985 今天遇见了这问题,决定要需要讨论下. 线程同步的方法: @synchronized 官方文档解释:The @syn ...