Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 13805   Accepted: 5996

Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation
residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤
Di ≤ N) and approach the front desk to check in. Each group
i
requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers
r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of
r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and
Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤
XiN-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and
Di (b) Three space-separated integers representing a check-out: 2,
Xi, and Di

Output

* Lines 1.....: For each check-in request, output a single line with a single integer
r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

Source

USACO 2008 February Gold

题意:有一个线段,从1到n。以下m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作

1 w  表示在总区间内查询一个长度为w的可用区间,而且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0

好像n=10 , 1 3 查到的最左的长度为3的可用区间就是[1,3]。返回1,而且该区间被占用了

2 a len , 表示从单位a開始,清除一段长度为len的区间(将其变为可用。不被占用),不须要输出

ac代码

#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
struct s
{
int ll,rl,ml,cover;
}node[50050<<2];
void build(int l,int r,int tr)
{
node[tr].ll=node[tr].rl=node[tr].ml=r-l+1;
node[tr].cover=-1;
if(l==r)
return;
int mid=(l+r)>>1;
build(l,mid,tr<<1);
build(mid+1,r,tr<<1|1);
}
void pushdown(int tr,int m)
{
if(node[tr].cover!=-1)
{
node[tr<<1].cover=node[tr<<1|1].cover=node[tr].cover;
if(node[tr].cover==0)
{
node[tr<<1].ll=node[tr<<1].rl=node[tr<<1].ml=m-(m>>1);
node[tr<<1|1].ll=node[tr<<1|1].rl=node[tr<<1|1].ml=(m>>1);
}
else
{
node[tr<<1].ll=node[tr<<1].rl=node[tr<<1].ml=0;
node[tr<<1|1].ll=node[tr<<1|1].rl=node[tr<<1|1].ml=0;
}
node[tr].cover=-1;
}
}
void pushup(int tr,int m)
{
node[tr].ll=node[tr<<1].ll;
node[tr].rl=node[tr<<1|1].rl;
if(node[tr].ll==m-(m>>1))
node[tr].ll+=node[tr<<1|1].ll;
if(node[tr].rl==(m>>1))
node[tr].rl+=node[tr<<1].rl;
node[tr].ml=max(node[tr<<1].rl+node[tr<<1|1].ll,max(node[tr<<1].ml,node[tr<<1|1].ml));
}
void update(int L,int R,int l,int r,int tr,int val)
{
if(L<=l&&R>=r)
{
if(val)
{
node[tr].ll=node[tr].rl=node[tr].ml=0;
}
else
node[tr].ll=node[tr].rl=node[tr].ml=r-l+1;
node[tr].cover=val;
return;
}
pushdown(tr,r-l+1);
int mid=(l+r)>>1;
if(L>mid)
{
update(L,R,mid+1,r,tr<<1|1,val);
}
else
if(R<=mid)
{
update(L,R,l,mid,tr<<1,val);
}
else
{
update(L,mid,l,mid,tr<<1,val);
update(mid+1,R,mid+1,r,tr<<1|1,val);
}
/*if(L<=mid)
update(L,R,l,mid,tr<<1,val);
if(R>mid)
update(L,R,mid+1,r,tr<<1|1,val);*/
pushup(tr,r-l+1);
}
int query(int w,int l,int r,int tr)
{
if(l==r)
return l;
pushdown(tr,r-l+1);
int mid=(l+r)>>1;
if(node[tr<<1].ml>=w)
return query(w,l,mid,tr<<1);
else
if(node[tr<<1].rl+node[tr<<1|1].ll>=w)
return mid-node[tr<<1].rl+1;
else
return query(w,mid+1,r,tr<<1|1);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
while(m--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int a;
scanf("%d",&a);
if(a>node[1].ml)
{
printf("0\n");
continue;
}
int p=query(a,1,n,1);
printf("%d\n",p);
update(p,p+a-1,1,n,1,1);
}
else
{
int a,b;
scanf("%d%D",&a,&b);
update(a,a+b-1,1,n,1,0);
}
}
}
}

POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)的更多相关文章

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

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

  2. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  4. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  5. codevs 1299 线段树 区间更新查询

    1299 切水果  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 简单的说,一共N个水果排成 ...

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

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  7. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  9. 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!

    D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...

  10. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

随机推荐

  1. Acunetix Web Vulnerability Scanner使用和生成报告的方法

    Acunetix WVS,该扫描软件的全称Acunetix Web Vulnerability Scanner,是一个网站及服务器漏洞扫描软件.它可以检查Web应用程序中的漏洞,如SQL注入.跨站脚本 ...

  2. 配置服务器 Ubuntu 记录+踩坑

    从零开始配置服务器用于ss+站点 1. SS 首先安装pyenv,安装pyenv之前先安装必要环境,具体命令行请见: https://github.com/pyenv/pyenv/wiki/Commo ...

  3. (转)淘淘商城系列——商品搜索功能Service实现

    http://blog.csdn.net/column/details/15737.html 首先我们在taotao-search-interface工程中新建一个SearchService接口,并在 ...

  4. HDU_1561_The more, The Better_树型dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 The more, The Better Time Limit: 6000/2000 MS (J ...

  5. Deployd的使用

    deployd一个生成后台数据的软件,可以创建json格式的数据,也可以对数据进行增删改查等操作,甚至可以验证登录,简直就是自学好帮手呀,不用后台搞定后台,就用deployd 下载:链接: https ...

  6. 【原】CentOS release 6.2 安装mysql

     1.  yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (C ...

  7. XGBoost参数中文翻译以及参数调优

    XGBoost:参数解释:https://blog.csdn.net/zc02051126/article/details/46711047 机器学习系列(11)_Python中Gradient Bo ...

  8. 通过HTTP的HEADER完成各种骚操作

    作为一名专业的切图工程师,我从来不care网页的header,最多关心Status Code是不是200.但是HEADER真的很重要啊,客户端从服务器端获取内容,首先就是通过HEADER进行各种沟通! ...

  9. 洛谷——P2801 教主的魔法(线段树or分块)

    P2801 教主的魔法 (1) 若第一个字母为“M”,则紧接着有三个数字L.R.W.表示对闭区间 [L, R] 内所有英雄的身高加上W. (2) 若第一个字母为“A”,则紧接着有三个数字L.R.C.询 ...

  10. Linux---文件目录管理

    1. Linux文件目录架构 Linux的目录结构与win的目录有很大不同,首先,没有盘符的概念:然后Linux使用斜杠/标识目录,Linux首先建立一个根目录,然后将其他文件系统挂载到这个目录下. ...