CF 46 D. Parking Lot(线段树)

D. Parking Lot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as L 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 than b meters and the distance between his car and the one in front of his will be no less than f 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 pointL. 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 Lb и f (10 ≤ L ≤ 100000, 1 ≤ b, f ≤ 100). The second line contains an integer n (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 to 2, then the second number identifies the number of such a request (starting with 1) 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 the 2 type was made. The lengths of cars are integers from 1 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 test(s)
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

题目:http://www.codeforces.com/problemset/problem/46/D

题意:就是模拟车的停靠,然后要求车前和车后要有空间。。。

分析:这个跟这题差不多,每辆车的长度变成b+f+车长,把整个停车场也加上b和f,那么就不用考虑空间了,直接做就行。。。总长度居然是0~l-1。。。我以为是到l的。。。无限的wa啊。。。


代码:

#include<cstdio>
#include<iostream>
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m+1,r,rs
#define uprt rt,m-l+1,r-m
using namespace std;
const int mm=111111;
const int mn=mm<<2;
int dly[mn],ml[mn],lm[mn],rm[mn];
int sl[mm],sr[mm];
void set(int rt,int op,int len)
{
    ml[rt]=lm[rt]=rm[rt]=op?len:0;
    dly[rt]=op;
}
void pushdown(int rt,int l1,int l2)
{
    if(dly[rt]<0)return;
    set(ls,dly[rt],l1);
    set(rs,dly[rt],l2);
    dly[rt]=-1;
}
void pushup(int rt,int l1,int l2)
{
    ml[rt]=max(rm[ls]+lm[rs],max(ml[ls],ml[rs]));
    lm[rt]=lm[ls],rm[rt]=rm[rs];
    if(lm[rt]>=l1)lm[rt]+=lm[rs];
    if(rm[rt]>=l2)rm[rt]+=rm[ls];
}
void build(int l,int r,int rt)
{
    ml[rt]=lm[rt]=rm[rt]=r-l+1;
    dly[rt]=-1;
    if(l==r)return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void updata(int L,int R,int op,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        set(rt,op,r-l+1);
        return;
    }
    int m=(l+r)>>1;
    pushdown(uprt);
    if(L<=m)updata(L,R,op,lson);
    if(R>m)updata(L,R,op,rson);
    pushup(uprt);
}
int query(int x,int l,int r,int rt)
{
    if(lm[rt]>=x)return l;
    int m=(l+r)>>1,ret;
    pushdown(uprt);
    if(ml[ls]>=x)ret=query(x,lson);
    else if(rm[ls]+lm[rs]>=x)ret=m-rm[ls]+1;
    else ret=query(x,rson);
    pushup(uprt);
    return ret;
}
int main()
{
    int i,j,k,t,l,b,f,n;
    while(~scanf("%d%d%d",&l,&b,&f))
    {
        scanf("%d",&n);
        l=b+l+f-1;
        build(0,l,1);
        for(t=1;t<=n;++t)
        {
            scanf("%d%d",&i,&j);
            if(i==1)
            {
                if(ml[1]>=b+j+f)
                {
                    k=query(b+j+f,0,l,1);
                    printf("%d\n",k);
                    sl[t]=k+b;
                    sr[t]=k+b+j-1;
                    updata(sl[t],sr[t],0,0,l,1);
                }
                else puts("-1");
            }
            else updata(sl[j],sr[j],1,0,l,1);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值