首页 > 代码库 > CF 46 D Parking Lot(线段树区间合并)

CF 46 D Parking Lot(线段树区间合并)

Description

Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let‘s imagine a segment of a street as long asL meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of the tasks come from the driving is right side!). Every driver when parking wants to leave for themselves some extra space to move their car freely, that‘s why a driver is looking for a place where the distance between his car and the one behind his will be no less thanb meters and the distance between his car and the one in front of his will be no less thanf meters (if there‘s no car behind then the car can be parked at the parking lot segment edge; the same is true for the case when there‘re no cars parked in front of the car). Let‘s introduce an axis of coordinates along the pavement. Let the parking lot begin at point 0 and end at point L. The drivers drive in the direction of the coordinates‘ increasing and look for the earliest place (with the smallest possible coordinate) where they can park the car. In case there‘s no such place, the driver drives on searching for his perfect peaceful haven. Sometimes some cars leave the street and free some space for parking. Considering that there never are two moving cars on a street at a time write a program that can use the data on the drivers, entering the street hoping to park there and the drivers leaving it, to model the process and determine a parking lot space for each car.

Input

The first line contains three integers L,b и f (10?≤?L?≤?100000,?1?≤?b,?f?≤?100). The second line contains an integern (1?≤?n?≤?100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the request type is equal to 1, then in that case the second number indicates the length of a car (in meters) that enters the street looking for a place to park. And if the request type is equal to2, then the second number identifies the number of such a request (starting with1) that the car whose arrival to the parking lot was described by a request with this number, leaves the parking lot. It is guaranteed that that car was parked at the moment the request of the2 type was made. The lengths of cars are integers from1 to 1000.

Output

For every request of the 1 type print number -1 on the single line if the corresponding car couldn‘t find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone.

Sample Input

Input
30 1 2
6
1 5
1 4
1 5
2 2
1 5
1 4
Output
0
6
11
17
23
Input
30 1 1
6
1 5
1 4
1 5
2 2
1 5
1 4
Output
0
6
11
17
6
Input
10 1 1
1
1 12
Output
-1
线段树区间合并的题目:按照要求来模拟。线段树的扫描线和区间合并现在还写不好,参照一下别人的。点击打开链接
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
#define lson rs<<1,l,mid
#define rson rs<<1|1,mid+1,r
const int maxn=100000+100;
int lmax[maxn<<2],rmax[maxn<<2],mmax[maxn<<2];//lmax从左边连续的最大值,rmax从右边连续的最大值
int n,m,col[maxn<<2],f,b,st[110],ed[110];//col记录覆盖情况
void pushup(int rs,int l,int r)
{
    int mid=(l+r)>>1;
    lmax[rs]=lmax[rs<<1];
    rmax[rs]=rmax[rs<<1|1];
    mmax[rs]=max(mmax[rs<<1],mmax[rs<<1|1]);
    mmax[rs]=max(mmax[rs],rmax[rs<<1]+lmax[rs<<1|1]);
    if(lmax[rs<<1]==mid-l+1)  lmax[rs]+=lmax[rs<<1|1];
    if(rmax[rs<<1|1]==r-mid)  rmax[rs]+=rmax[rs<<1];
}
void pushdown(int rs,int l,int r)
{
    if(col[rs]!=-1)
    {
        int mid=(l+r)>>1;
        if(col[rs]==1)
            rmax[rs<<1]=lmax[rs<<1]=rmax[rs<<1|1]=lmax[rs<<1|1]=mmax[rs<<1]=mmax[rs<<1|1]=0;
        else
        {
            lmax[rs<<1]=rmax[rs<<1]=mmax[rs<<1]=mid-l+1;
            lmax[rs<<1|1]=rmax[rs<<1|1]=mmax[rs<<1|1]=r-mid;
        }
        col[rs]=-1;
    }
}
void build(int rs,int l,int r)
{
    lmax[rs]=rmax[rs]=mmax[rs]=r-l+1;
    col[rs]=-1;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rs,l,r);
}
void update(int rs,int l,int r,int L,int R,int val)
{
    if(L<=l&&r<=R)
    {
        col[rs]=val;
        lmax[rs]=rmax[rs]=mmax[rs]=(r-l+1)*(val^1);
        return ;
    }
    pushdown(rs,l,r);
    int mid=(l+r)>>1;
    if(mid>=L)  update(lson,L,R,val);
    if(mid<R)  update(rson,L,R,val);
    pushup(rs,l,r);
}
int query(int rs,int l,int r,int len)
{
    if(mmax[rs]<len)  return -1;
    if(lmax[rs]>=len)  return l;
    pushdown(rs,l,r);
    int mid=(l+r)>>1;
    if(mmax[rs<<1]>=len)  return query(lson,len);
    if(rmax[rs<<1]+lmax[rs<<1|1]>=len)  return mid-rmax[rs<<1]+1;
    return query(rson,len);

}
int main()
{
    int op,idx;
    while(~scanf("%d%d%d",&n,&b,&f))
    {
        n=n+b+f-1;
        build(1,0,n);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&op,&idx);
            if(op==1)
            {
                int len=idx+b+f;
                int k=query(1,0,n,len);
                printf("%d\n",k);
                if(k!=-1)
                {
                    st[i]=k+b;
                    ed[i]=k+b+idx-1;
                    update(1,0,n,st[i],ed[i],1);
                }
            }
            else
                update(1,0,n,st[idx],ed[idx],0);
        }
    }
    return 0;
}


CF 46 D Parking Lot(线段树区间合并)