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. cocos2d-x创建精灵动画

    创建动画一般过程: 1.创建精灵框架缓存,并向其中添加相应的动画文件(plist),最后,通过动画集缓存生产动画 CCSpriteFrameCache *cache = CCSpriteFrameCa ...

  2. [Reactive Programming] Async requests and responses in RxJS

    We will learn how to perform network requests to a backend using RxJS Observables. A example of basi ...

  3. [AngularJS + Webpack] Production Setup

    Using Angular with webpack makes the production build a breeze. Simply alter your webpack configurat ...

  4. 【Spring五】AOP之使用注解配置

    AOP使用注解配置流程: 1.当spring容器启动时候.    < context:component- scan base-package= "cn.itheima03.sprin ...

  5. 【Android】Android的优点和不足之处

    随着Android的越来越红火,不少应聘Android开发的人,难免会被问到这样的问题,就是这个平台的优点,当然有优点也会有缺点的, 下面是我从网上总结出来的,希望对大家应聘Android开发有所帮助 ...

  6. Bash函数使用

    #!/bin/bash function Fun_Name() { #function here echo "this is a function" } Fun_Name

  7. MVC4将Controller与views分开

    最近自己在要着手从头做一个项目,自己想把mvc里的view和controller文件分别写在不同的项目里,刚开始在网上找了下,可是不尽理想,最后翻了一下自己以前参加的项目找到了这个做法. 这个需要在G ...

  8. WPF 媒体播放器(MediaElement)实例,实现进度和音量控制

    WPF 视频音频播放控件MediaElement实现进度控制,音量控制实例 说明: 1.Volume控制音量的大小,double类型,并且实现了属性依赖,可以用来双向绑定:在 0 和 1. 之间的线性 ...

  9. [o] SQLite数据库报错: Invalid column C

    向SQLite数据库内新增列,之前出现过报错为提示no such column,通过删除并重建数据库文件解决,这次报错为无效的数据列: java.lang.IllegalArgumentExcepti ...

  10. Android开发手记(27) Java多线程的操作

    Java中常用的有关线程的操作有,判断线程是否启动.线程强制执行.线程休眠.线程中断.线程让步.线程同步等.下面就一一举例. 首先,我们新建一个MyThread类实现Runnable接口.基于此接口进 ...