您的位置:首页 > 其它

poj3667(线段树,区间合并)

2013-08-03 11:05 232 查看
地址:http://poj.org/problem?id=3667

Hotel

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9478 Accepted: 4061
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 ≤
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

利用区间合并,记录每段区间左边连续最大,右边连续最大以及区间内连续最大,利用pushup函数实现。
cover是用来记录处理情况,然后在下一次处理数据时先行将数据赋值,延迟思想。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define M 50050
#define LL L,m,c<<1
#define RR m+1,R,c<<1|1
#define max(a,b) a>b?a:b
int lm[M<<2],rm[M<<2],sm[M<<2],cover[M<<2];
void pushup(int L,int R,int c) //区间合并核心代码
{
int m=(L+R)/2;
lm[c]=lm[c<<1];rm[c]=rm[c<<1|1];
sm[c]=max(sm[c<<1],sm[c<<1|1]);
if(lm[c<<1]==m-L+1) lm[c]+=lm[c<<1|1];
if(rm[c<<1|1]==R-m) rm[c]+=rm[c<<1];
sm[c]=max(rm[c<<1]+lm[c<<1|1],sm[c]);
}
void pushdown(int c,int w) //延迟思想代码
{
if(cover[c]!=-1)
{
cover[c<<1]=cover[c<<1|1]=cover[c];
lm[c<<1]=rm[c<<1]=sm[c<<1]=cover[c]?0:w-(w>>1); //这句代码的意思是若cover[c]=1则lm[c<<1],rm[c<<1]和sm[c<<1]为0;反之为w-(w>>1)。
lm[c<<1|1]=rm[c<<1|1]=sm[c<<1|1]=cover[c]?0:w>>1;
cover[c]=-1;
}
}
void ori_tree(int L,int R,int c)
{
lm[c]=rm[c]=sm[c]=R-L+1;
cover[c]=-1;
if(L!=R)
{
int m=(L+R)>>1;
ori_tree(LL);
ori_tree(RR);
}
}
int query(int i,int L,int R,int c)
{
if(L==R) return 1;
pushdown(c,R-L+1);
int m=(L+R)>>1;
if(sm[c<<1]>=i) return query(i,LL);
else if(rm[c<<1]+lm[c<<1|1]>=i) return m-rm[c<<1]+1;
else return query(i,RR);
}
void updata(int l,int r,int n,int L,int R,int c)
{
if(l<=L&&R<=r)
{
lm[c]=rm[c]=sm[c]=n?0:R-L+1;
cover[c]=n;
return ; //找到答案后直接输出此次结果,当下一次运行到这里时再将数据赋值到下一层
}
pushdown(c,R-L+1); //将数据赋值到下一层
int m=(L+R)>>1;
if(l<=m) updata(l,r,n,LL);
if(r>m) updata(l,r,n,RR);
pushup(L,R,c);
}
int main()
{
int m,n,t,l,r;
scanf("%d%d",&m,&t);
ori_tree(1,m,1);
while(t--)
{
scanf("%d",&n);
if(n==1)
{
scanf("%d",&r);
if(sm[1]<r)
{
puts("0");
continue;
}
l=query(r,1,m,1);
printf("%d\n",l);
updata(l,l+r-1,1,1,m,1);
}
else
{
scanf("%d%d",&l,&r);
updata(l,l+r-1,0,1,m,1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj