poj3667:http://poj.org/problem?id=3667

题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,要求房间的编号为连续的r..r+Di-1并且r是最小的;visitors同样可能check out,并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式:
    1  a     :  groups需要check in  a间编号连续的房间
    2  a   b : visitors  check out 房间,其中房间编号是 a…a+b-1要求对于每次request,输出为groups分配数目为a的房间中编号最小的房间编号
题解:用 线段树维护区间的连续最大值,最大左连续,最大右连续。查询时,如果当前节点的sum值大等于val,则看该区间的左连续,若左连续大等于val,直接返回left;否则,看左二子,如果左儿子的sum值大于等于val,则进入左儿子进行查询,如果不满足,再看左儿子的右连续+右儿子的左连续是否大等于val,若大于则直接返回左儿子右连续的起点值,否则进入右儿子进行查询。否则,返回0.如果返回的不是0,就进行区间更新。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int u,v,w;
int n,m;
struct Node{
int left;
int right;
int lsum,rsum,sum;
int lazy;
}node[*];
void build(int l,int r,int idx){
node[idx].left=l;
node[idx].right=r;
node[idx].lazy=;//lazy标记
if(l==r){
node[idx].sum=node[idx].lsum=node[idx].rsum=;
return;
}
int mid=(l+r)/;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
node[idx].sum=node[idx].lsum=node[idx].rsum=r-l+;
}
void pushup(int idx){
if(node[idx<<].lsum==node[idx<<].right-node[idx<<].left+)
node[idx].lsum=node[idx<<].lsum+node[idx<<|].lsum;
else
node[idx].lsum=node[idx<<].lsum;
if(node[idx<<|].rsum==node[idx<<|].right-node[idx<<|].left+)
node[idx].rsum=node[idx<<].rsum+node[idx<<|].rsum;
else
node[idx].rsum=node[idx<<|].rsum;
node[idx].sum=node[idx<<].rsum+node[idx<<|].lsum;
node[idx].sum=max(node[idx<<].sum,max(node[idx].sum,node[idx<<|].sum));
}
void pushdown(int idx){
if(node[idx].lazy==){
node[idx<<].sum=node[idx<<|].sum=;
node[idx<<].lsum=node[idx<<].rsum=;
node[idx<<|].lsum=node[idx<<|].rsum=;
node[idx<<].lazy=node[idx<<|].lazy=;//很重要,开始忘记往下传递问题
node[idx].lazy=;
}
if(node[idx].lazy==){
node[idx<<].lsum=node[idx<<].rsum=node[idx<<].sum=node[idx<<].right-node[idx<<].left+;
node[idx<<|].lsum=node[idx<<|].rsum=node[idx<<|].sum=node[idx<<|].right-node[idx<<|].left+;
node[idx<<].lazy=node[idx<<|].lazy=;
node[idx].lazy=;
}
}
void update1(int l,int r,int val,int idx){
if(node[idx].left==l&&node[idx].right==r&&val==){
node[idx].sum=;
node[idx].lsum=node[idx].rsum=;
node[idx].lazy=;
return;
}
if(node[idx].left==l&&node[idx].right==r&&val==){
node[idx].sum=node[idx].right-node[idx].left+;
node[idx].lsum=node[idx].rsum=node[idx].right-node[idx].left+;
node[idx].lazy=;
return;
}
pushdown(idx);
int mid=(node[idx].right+node[idx].left)/;
if(mid>=r)update1(l,r,val,idx<<);
else if(mid<l)update1(l,r,val,idx<<|);
else{
update1(l,mid,val,idx<<);
update1(mid+,r,val,idx<<|);
}
pushup(idx);
}
int update2(int val,int idx){
if(node[idx].sum>=val){
pushdown(idx);
if(node[idx].lsum>=val){
return node[idx].left;
}
else if(node[idx<<].sum>=val)return update2(val,idx<<);
else if(node[idx<<].rsum+node[idx<<|].lsum>=val)
return node[idx<<].right-node[idx<<].rsum+;
else
return update2(val,idx<<|);
}
else {
return ;
}
}
int main(){
scanf("%d%d",&n,&m);
build(,n,);
for(int i=;i<=m;i++){
cin>>u;
if(u==){
cin>>v;
int ans=update2(v,);
printf("%d\n",ans);
if(ans>)//这里不要有分好,我几次就是死在了这里
update1(ans,ans+v-,,);
}
else{
cin>>v>>w;
update1(v,v+w-,,);
}
}
}

Hotel的更多相关文章

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

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

  2. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  3. HDU - Hotel

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

  4. 【POJ3667】Hotel

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

  5. POJ-2726-Holiday Hotel

    Holiday Hotel   Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8302   Accepted: 3249 D ...

  6. Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot evaluate com.hotel.Object_$$_jvst485_15.toString()

    数据库字段和类Object属性不匹配,Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot eval ...

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

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

  8. [POJ3667]Hotel(线段树,区间合并)

    题目链接:http://poj.org/problem?id=3667 题意:有一个hotel有n间房子,现在有2种操作: 1 a,check in,表示入住.需要a间连续的房子.返回尽量靠左的房间编 ...

  9. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

  10. Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟

    A. Saitama Destroys Hotel   Saitama accidentally destroyed a hotel again. To repay the hotel company ...

随机推荐

  1. Mysql重要配置参数的整理2

    http://ssydxa219.iteye.com/category/209848 下面开始优化下my.conf文件(这里的优化只是在mysql本身的优化,之前安装的时候也要有优化) cat /et ...

  2. Qt 学习之路 :文本文件读写

    上一章我们介绍了有关二进制文件的读写.二进制文件比较小巧,却不是人可读的格式.而文本文件是一种人可读的文件.为了操作这种文件,我们需要使用QTextStream类.QTextStream和QDataS ...

  3. AS【常用插件】

    安装插件,Settings -->[Plugins]-->搜索-->点击install-->重启AS 禁用插件,右侧面板会显示出已经安装的插件列表,取消勾选即可禁用插件 AS插 ...

  4. WebForm开发常用代码

    1.获取服务器绝对路径: public static string GetMapPath(string strPath) { if (HttpContext.Current != null) { re ...

  5. Canvas模糊化处理图片、毛玻璃处理图片之stackblur.js

    Canvas实现毛玻璃效果解决方式1:使用stackblur.js 在Android系统中实现图片的毛玻璃效果比较好用的类库是:Android StackBlur 官方Git地址:https://gi ...

  6. .Net实现IO操作

    IO操作需要的web.config里的节点配置 <configuration>  <appSettings>    <!--上传文件类型要求-->    <a ...

  7. TSQL Beginners Challenge 1 - Find the second highest salary for each department

    很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...

  8. 使用ng-if,获取不到里面的ng-model值,解决方案

    当使用ng-if时,是会把默认作用域删除的,当其为true时,只是增加了其界面元素,为最原始状态,控制器在其上是不起作用的,要想获取ng-if中的值,可以用$scope.$$childTail.lay ...

  9. 使用soapUI代替WSDL2JAVA生成cxf HTTPS 客户端调用代码

    如果直接用cxf下面的wsdl2java生成https服务调用代码,会报https证书的错误.在你不想导入证书的情况下,可以使用soapUI进行客户端代码的生成,步骤如下: 1.设置CXF,如下图: ...

  10. 【转载】【挖掘Treap的潜力】

    原帖: http://fanhq666.blog.163.com/blog/static/819434262011021105212299/ 你的Treap能支持以下操作吗?1.区间增减 2.区间求最 ...