题目(poj3667)
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.
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
题意
奶牛住旅馆,一群住宿的奶牛的房间号必须连着
分析
用线段树来做,与Tunnel Warfare(地道战)有些相似,对于线段树有两种操作,一个是check in
给你的是要住的人数,你要找房间号最小的房间,而且要满足连着的,另一种操作是check out,是给你要离开的房间开始的房间号以及离开的奶牛数
对于check in,我们要在树的节点存储l到r的区间内连着的最长的序列,这就类似求地道中连续最长的序列了,的确思路也是一样的,我们要想知道每个区间的连着的最长的序列,还需要存储的节点信息有lmax,rmax。这是关键。
对于更新操作就与平常的线段树一样了。
代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<stdbool.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxn=95005;
int n,m;
struct tree{
int l_max;//从左开始连续的序列长度(严格左)
int r_max;//从右开始连续的序列长度(严格右)//左右均指l,r区间之内的左右
int all_max; // l,r区间之内的最长的连续序列
int lazy;//lazy下沉标记 -1表示不需下沉,0表示需要更新为0,1表示需要更新为1
}a[maxn<<2];
void push_up(int l,int r,int root)//这是向上更新的
{
int mid=(l+r)>>1;
a[root].l_max=a[root<<1].l_max;
a[root].r_max=a[root<<1|1].r_max;
if(a[root<<1].l_max==mid-l+1)
{
a[root].l_max+=a[root<<1|1].l_max;
}
if(a[root<<1|1].r_max==r-mid-1+1)
{
a[root].r_max+=a[root<<1].r_max;
}
a[root].all_max=max(max(a[root<<1].all_max,a[root<<1|1].all_max),a[root<<1].r_max+a[root<<1|1].l_max);
}
void push_down(int l,int r,int root)//下沉标记
{
int mid=(l+r)>>1;
if(a[root].lazy!=-1)
{
a[root<<1].lazy=a[root<<1|1].lazy=a[root].lazy;
a[root<<1].l_max=a[root<<1].r_max=a[root<<1].all_max=(mid-l+1)*a[root].lazy;
a[root<<1|1].l_max=a[root<<1|1].r_max=a[root<<1|1].all_max=(r-mid)*a[root].lazy;
a[root].lazy=-1; //莫忘掉
}
}
void build(int l,int r,int root)
{
a[root].all_max=a[root].l_max=a[root].r_max=r-l+1;
a[root].lazy=-1;
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
}
int query(int l,int r,int root,int val)
{
if(l==r)
{
return l;//这句要加上,表示若找到叶子结点的话,我们需要确定答案并返回的
} //注意一定是有答案的,在main函数里是有判断的
int mid=(l+r)>>1;
push_down(l,r,root);
if(a[root<<1].all_max>=val)//能向左找的尽量向左
{
return query(l,mid,root<<1,val);
}
else if(a[root<<1].r_max+a[root<<1|1].l_max>=val)//说明是在中间部分,好好想想树的结构,此时是能确定的
{
return mid-a[root<<1].r_max+1;
}
else{//否则向右
return query(mid+1,r,root<<1|1,val);
}
}
void update(int l,int r,int root,int la,int ra,int val)
{
if(la<=l&&ra>=r)
{
a[root].all_max=a[root].l_max=a[root].r_max=(r-l+1)*val;
a[root].lazy=val;
return ;
}
push_down(l,r,root);//是需要push_down的,因为我们的更新的话是区间上的操作,与地道是有区别的
int mid=(l+r)>>1;
if(la<=mid)
{
update(l,mid,root<<1,la,ra,val);
}
if(ra>mid)
{
update(mid+1,r,root<<1|1,la,ra,val);
}
push_up(l,r,root);
}
int main()
{
int i,j,a1,a2,a3,a4;
scanf("%d%d",&n,&m);
build(1,n,1);
for(i=0;i<m;i++)
{
scanf("%d",&a1);
if(a1==1)
{
scanf("%d",&a2);
if(a[1].all_max<a2)//事先判断一下
{
printf("0\n");
}
else{
a4=query(1,n,1,a2);
printf("%d\n",a4);
update(1,n,1,a4,a4+a2-1,0);}
}
else{
scanf("%d%d",&a2,&a3);
//printf("%d$$$\n",a2);
update(1,n,1,a2,a2+a3-1,1);
}
}
return 0;
}
易错点分析
1.当时忘了写push_down函数,因为我们修改的时候都是区间修改的,在用到树的根部的时候很多状态是没有改变的
2.对于查找是关键,先找左面,再找中间,再找右边,为什么只有中间是可以确定具体位置的,因为如果在mid的左面,他有可能在root<<1的mid的左面,也就是能继续往左找,好好想想这棵树
3.对于查找,如果已经找到叶子节点了,这时候不能再往下找了