题目描述

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 ≤ Xi ≤ N-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.

参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

输入输出格式

输入格式:

  • 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

输出格式:

  • 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.

输入输出样例

输入样例#1:

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:

1
4
7
0
5

线段树维护每个节点左边有多少连续的空的,右边有少连续的空的,

最长有多少连续的空的,最长的空位从什么地方开始

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 400010
int sum[N],lm[N],rm[N],m[N],tag[N];
int n,T;
inline void update(int rt)
{
if(m[rt<<]==sum[rt<<]){
lm[rt]=m[rt<<]+lm[rt<<|];
}else {
lm[rt]=lm[rt<<];
}
if(m[rt<<|]==sum[rt<<|]){
rm[rt]=m[rt<<|]+rm[rt<<];
}else {
rm[rt]=rm[rt<<|];
}
m[rt]=max(max(m[rt<<],m[rt<<|]),rm[rt<<]+lm[rt<<|]); }
inline void pushdown(int rt)
{
if(tag[rt]==){
tag[rt<<]=tag[rt<<|]=;
m[rt<<]=lm[rt<<]=rm[rt<<]=;
m[rt<<|]=lm[rt<<|]=rm[rt<<|]=;
}
else {
tag[rt<<]=tag[rt<<|]=;
m[rt<<]=lm[rt<<]=rm[rt<<]=sum[rt<<];
m[rt<<|]=lm[rt<<|]=rm[rt<<|]=sum[rt<<|];
}
tag[rt]=;
}
void build(int l,int r,int rt)
{
lm[rt]=rm[rt]=m[rt]=sum[rt]=r-l+;
tag[rt]=;
if(l==r)return ;
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
} int query(int l,int r,int rt,int len)
{
if(tag[rt]) pushdown(rt);
if(l==r) return l;
int mid=(l+r)>>;
if(m[rt<<]>=len) return query(l,mid,rt<<,len);
if(rm[rt<<]+lm[rt<<|]>=len)return mid-rm[rt<<]+;
else return query(mid+,r,rt<<|,len);
} void modify(int l,int r,int rt,int L,int R,int c)
{
if(tag[rt])pushdown(rt);
if(L<=l&&r<=R){
if(c==) m[rt]=lm[rt]=rm[rt]=;
else m[rt]=lm[rt]=rm[rt]=sum[rt];
tag[rt]=c;
return;
}
int mid=(l+r)>>;
if(L<=mid) modify(l,mid,rt<<,L,R,c);
if(R>mid) modify(mid+,r,rt<<|,L,R,c);
update(rt);
}
int main()
{
scanf("%d%d",&n,&T);
build(,n,);
while(T--)
{
int a,b,gg;
scanf("%d",&gg);
if(gg==)
{
scanf("%d",&a);
if(m[]<a){puts("");continue;}
int p=query(,n,,a);
printf("%d\n",p);
modify(,n,,p,p+a-,);
}else {
scanf("%d%d",&a,&b);
modify(,n,,a,a+b-,);
}
}
return ;
}

最新文章

  1. gulp(一)
  2. css书写规则总结
  3. Java语言中,类所拥有的“孩子”,他们的关系是怎样的
  4. HDU 2509 nim博弈
  5. 选择时区的命令tzselect
  6. oracle 运维基础
  7. HNOI2008题目总结
  8. hibernate 使用in方式删除数据
  9. phpcms v9 万能字段使用
  10. phoenix
  11. vim7.3中文乱码问题
  12. 用three.js创建一个简易的天空盒
  13. [论文阅读]VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION(VGGNet)
  14. Weblogic记录
  15. 将汉字转化为拼音的js插件
  16. bsdiff的编译与使用
  17. kubeadm部署kubernetes-1.12.0 HA集群-ipvs
  18. 找出n个数中重复最多的10个数
  19. Msmq设计文档(赋源代码)
  20. Spring + MyBatis 多数据源实现

热门文章

  1. python 面对对象基础
  2. JdbcTemplate实验
  3. 通过IAR工程文件查看对应IAR版本号
  4. Android后台的linux一直保持唤醒状态,不进入睡眠
  5. 关于51单片机IO引脚的驱动能力与上拉电阻
  6. Java技术——Java泛型详解
  7. Python之code对象与pyc文件(三)
  8. mysql PacketTooBigException 的处理方式
  9. Java程序的结构和执行
  10. 如何理解redo和undo的作用