博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3667 Hotel(此题跟poj 1823有共同点,都属于区间合并问题)
阅读量:4035 次
发布时间:2019-05-24

本文共 4173 字,大约阅读时间需要 13 分钟。

1、

2、题目大意:

一个hotel住旅客和走旅客,现在有N间房子,操作命令有两种,1代表要找一个连续空间长度就是1后边跟着的数字,此时要输出这个连续空间的首位置,2代表清空部分房间,2后边跟着两个数n,c,b就代表起始位置,c代表连续c个房子都要清空,详细与poj 1823相同,就是求的有些不同,多了一个查询

3、题目:

Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10380   Accepted: 4456

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 61 31 31 31 32 5 51 6

Sample Output

14705

Source

 

4、AC代码;

#include
#include
using namespace std;#define N 50005struct node{ int l; int r; int lsum,rsum,msum; int flag; void cal() { lsum=rsum=msum=(r-l+1)*flag; }} a[N*4];void build(int l,int r,int rt){ a[rt].l=l; a[rt].r=r; a[rt].flag=1; a[rt].cal(); if(l==r) return ; int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1);}void update(int L,int R,int rt,int flag){ //printf("******%d %d %d %d %d\n",L,R,rt,a[rt].l,a[rt].r); if(L<=a[rt].l && R>=a[rt].r) { a[rt].flag=flag; a[rt].cal(); return ; } if(a[rt].flag!=-1) { a[rt<<1].flag=a[rt<<1|1].flag=a[rt].flag; a[rt<<1].cal(); a[rt<<1|1].cal(); a[rt].flag=-1; } int m=(a[rt].l+a[rt].r)>>1; if(R<=m) update(L,R,rt<<1,flag); else if(L>m) update(L,R,rt<<1|1,flag); else { update(L,m,rt<<1,flag); update(m+1,R,rt<<1|1,flag); } //如果左孩子的左边空的等于线段长度,说明整个左孩子都是空的, if(a[rt<<1].lsum==a[rt<<1].r-a[rt<<1].l+1) a[rt].lsum=a[rt<<1].lsum+a[rt<<1|1].lsum; else a[rt].lsum=a[rt<<1].lsum; //如果右孩子的右边空的等于线段长度,说明整个右孩子都是空的, if(a[rt<<1|1].rsum==a[rt<<1|1].r-a[rt<<1|1].l+1) a[rt].rsum=a[rt<<1|1].rsum+a[rt<<1].rsum; else a[rt].rsum=a[rt<<1|1].rsum; //更新中间的值 a[rt].msum=max(max(a[rt<<1].msum,a[rt<<1|1].msum),a[rt<<1].rsum+a[rt<<1|1].lsum);}int query(int rt,int num){ if(a[rt].l==a[rt].r && num==1) return a[rt].l; if(a[rt].flag!=-1) { a[rt<<1].flag=a[rt<<1|1].flag=a[rt].flag; a[rt<<1].cal(); a[rt<<1|1].cal(); a[rt].flag=-1; } if(a[rt<<1].msum>=num) return query(rt<<1,num); else if(a[rt<<1].rsum+a[rt<<1|1].lsum>=num) return a[rt<<1].r-a[rt<<1].rsum+1; else if(a[rt<<1|1].msum>=num) return query(rt<<1|1,num); else return 0;}int main(){ int n,p,x,b,c; scanf("%d%d",&n,&p); build(1,n,1); for(int i=1; i<=p; i++) { scanf("%d",&x); if(x==1) { scanf("%d",&c); //这里得注意先判断能不能安排,如果不能安排,就不要更新 if(a[1].msum

 

转载地址:http://wwcdi.baihongyu.com/

你可能感兴趣的文章
Android系统构架
查看>>
Android 跨应用程序访问窗口知识点总结
查看>>
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux config
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>