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 slave状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们能够通过在slave上运行show slave status来查看slave的一些状态信息,当中有一个比較重要的參数Seconds_Behind_Master.那么你是否 ...

  2. android 03 TableLayout

    MainActivity.java(默认的,什么都没有) package com.sxt.day02_02; import android.os.Bundle; import android.app. ...

  3. Qt 学习之路:模型-视图高级技术

    PathView PathView是 QtQuick 中最强大的视图,同时也是最复杂的.PathView允许创建一种更灵活的视图.在这种视图中,数据项并不是方方正正,而是可以沿着任意路径布局.沿着同一 ...

  4. IOS-NSDateFormatter使用介绍

    IOS-NSDateFormatter使用介绍 NSDateFormatter的使用: NSDate *nowDate = [[NSDate alloc] init]; NSDateFormatter ...

  5. Elasticsearch .Net Client NEST 多条件查询示例

    Elasticsearch .Net Client NEST 多条件查询示例 /// <summary> /// 多条件搜索例子 /// </summary> public c ...

  6. Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题

    系统版本:Win7 64位 Matlab版本:R2015b 问题描述:使用excel的操作函数,比如xlsread,xlswrite,导致excel进程无法终止,任务管理器中仍残留excel进程,打开 ...

  7. How to customize authentication to my own set of tables in asp.net web api 2?

    ssuming your table is called AppUser, convert your own AppUser domain object to IUser(using Microsof ...

  8. 334. Increasing Triplet Subsequence My Submissions Question--Avota

    问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...

  9. 【HDU4366】【DFS序+分块】Successor

    Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st ...

  10. javascript——归并方法

    <script type="text/javascript"> //ECMAScript5 还新增了2个归并数组的方法:reduce()和reduceRight(). ...