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

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

抓紧时间学习一下;

代码:

 #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. android 70 使用ListView把数据显示至屏幕

    使用单元测试添加数据: package com.itheima.showdata; import java.sql.ResultSet; import android.content.Context; ...

  2. 使用Java辅助类(CountDownLatch、CyclicBarrier、Semaphore)并发编程

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法 一.C ...

  3. C#绘制圆形时钟

    本文由作者参考部分案例后加以修改完成: 参考链接如下: http://blog.csdn.net/xuemoyao/article/details/8001113 http://wenku.baidu ...

  4. JavaScript判断数据类型总结

    最近做项目中遇到了一些关于javascript数据类型的判断处理,上网找了一下资料,并且亲自验证了各种数据类型的判断网页特效,在此做一个总结吧! 一.JS中的数据类型  1.数值型(Number):包 ...

  5. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  6. Library中的title与Name

    在Library中新增Title字段,其中文件夹的title字段与Name相同,并且默认生成:但是文件的Title字段为空.

  7. EF收集

    http://www.cnblogs.com/end/archive/2011/08/18/2144250.html http://www.cnblogs.com/zzdfc/archive/2009 ...

  8. .NET中的消息队列

    下文参考:http://hi.baidu.com/21tian/blog/item/ce5464097ddf10cb3ac76335.html为何使用消息队列 您可能认为您能够通过一个简单的数据库表( ...

  9. linunx 定位最耗资源的进程

    [oracle@topbox bdump]$ ps -ef|grep “(LOCAL=NO)”|sort -rn -k 8,8|head -10oracle    9402     1 67 09:1 ...

  10. Lucene 排序 Sort与SortField

    在sql语句中,有升序和降序排列.在Lucene中,同样也有. Sort里的属性 SortField里的属性 含义 Sort.INDEXORDER SortField.FIELD_DOC 按照索引的顺 ...