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. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  2. js获取键盘的keyCode-------Day42

    济南今天是大雨倾盆啊,这闷热一扫而空,只是有些电闪雷鸣的,原想在公司里就完毕今天的博客记录的,只是不知道为什么怎么也登不上博客,预计是CSDN当时的server出问题了吧,好在到了晚上,这雷声小了也少 ...

  3. git学习 #2:git基本操作

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  4. android 52 粘滞广播

    粘滞广播:广播发送出去以后,广播接收者还没有创建,当广播接收者注册的时候就可以接收,如果不是粘滞广播则如果没有广播接收者就以后不能再接收了. mainActivity: package com.sxt ...

  5. Java中获得程序当前路径的4中方法

    Java中获得程序当前路径的4中方法: 在Application中: import java.util.*; public class TestUserDir { public static void ...

  6. WebForm开发常用代码

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

  7. 动软代码生成器三层用于winform

    DBUtility项目中的DbHelperSQL.cs (找自己对应的数据库类型) 修改前20行中的数据库连接字符串获取方式为: //数据库连接字符串(web.config来配置),多数据库可使用Db ...

  8. express不是内部命令解决办法

    安装nodejs后,安装express,cmd下命令:npm install express -g;之后敲入express -V会提示不是内部命令,是因为, 4.0版本中将命令工具分家出来了,所以我们 ...

  9. 解决办法:CMake编译时出现“error in configuration process project files may be invalid”

    无论是CMake2.84 还是当前最新的CMake2.87都可能会出现这种错: 查遍国内外的网上都没有给出可行办法,结果还是自己解决了 现把出错原因和解决办法如下:出错原因:因是英文版本,通常安装没有 ...

  10. 【BZOJ3295】【块状链表+树状数组】动态逆序对

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...