CF-46D-Parking Lot(線段樹),cf-46d-parkinglot

來源:互聯網
上載者:User

CF-46D-Parking Lot(線段樹),cf-46d-parkinglot

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

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 261 51 41 52 21 51 4
output
06111723
input
30 1 161 51 41 52 21 51 4
output
0611176
input
10 1 111 12
output
-1


思路:維護區間左邊和右邊的空地長度還有區間最大的連續空地長度即可。


#include <stdio.h>#define max(A,B)(A>B?A:B)struct{    int l,r;}qur[100];int mx[400005],ls[400005],rs[400005],att[400005];void build(int idx,int s,int e){    if(s!=e)    {        int mid=(s+e)>>1;        build(idx<<1,s,mid);        build(idx<<1|1,mid+1,e);    }    mx[idx]=ls[idx]=rs[idx]=e-s+1;    att[idx]=-1;}void update(int idx,int s,int e,int l,int r,int val){    if(l==s && e==r)    {        att[idx]=val;        mx[idx]=ls[idx]=rs[idx]=(e-s+1)*val;        return;    }    int mid=(s+e)>>1;    if(att[idx]!=-1)    {        mx[idx<<1]=ls[idx<<1]=rs[idx<<1]=(mid-s+1)*att[idx];        mx[idx<<1|1]=ls[idx<<1|1]=rs[idx<<1|1]=(e-mid)*att[idx];        att[idx<<1]=att[idx<<1|1]=att[idx];        att[idx]=-1;    }    if(r<=mid) update(idx<<1,s,mid,l,r,val);    else if(l>mid) update(idx<<1|1,mid+1,e,l,r,val);    else    {        update(idx<<1,s,mid,l,mid,val);        update(idx<<1|1,mid+1,e,mid+1,r,val);    }    ls[idx]=ls[idx<<1];    rs[idx]=rs[idx<<1|1];    mx[idx]=max(mx[idx<<1],mx[idx<<1|1]);    if(ls[idx]==mid-s+1) ls[idx]+=ls[idx<<1|1];    if(rs[idx]==e-mid) rs[idx]+=rs[idx<<1];    mx[idx]=max(rs[idx<<1]+ls[idx<<1|1],mx[idx]);}int query(int idx,int s,int e,int val){    int mid=(s+e)>>1;    if(att[idx]!=-1)    {        mx[idx<<1]=ls[idx<<1]=rs[idx<<1]=(mid-s+1)*att[idx];        mx[idx<<1|1]=ls[idx<<1|1]=rs[idx<<1|1]=(e-mid)*att[idx];        att[idx<<1]=att[idx<<1|1]=att[idx];        att[idx]=-1;    }    if(mx[idx<<1]>=val) return query(idx<<1,s,mid,val);    else if(rs[idx<<1]+ls[idx<<1|1]>=val) return mid-rs[idx<<1]+1;    else if(mx[idx<<1|1]>=val) return query(idx<<1|1,mid+1,e,val);    else return -1;}int main(){    int n,b,f,q,i,type,val,ans;    while(~scanf("%d%d%d%d",&n,&b,&f,&q))    {        n+=b+f-1;        build(1,0,n);        for(i=1;i<=q;i++)        {            scanf("%d%d",&type,&val);            if(type==1)            {                ans=query(1,0,n,val+b+f);                printf("%d\n",ans);                if(ans==-1) continue;                qur[i].l=ans+b;                qur[i].r=ans+b+val-1;                update(1,0,n,qur[i].l,qur[i].r,0);            }            else update(1,0,n,qur[val].l,qur[val].r,1);        }    }}


parking lot中的lot何意

parking lot,什麼意思,先要看這詞是出自哪裡,在美國,還是在英國。

在美國,parking lot是停車場。
在英國,parking lot是停車位。

lot是指一片地方,也可以是劃分出來的地方,可以是大的,也可以是小的。
 
Parking Lot 歌詞

歌名:Parking
演唱:Lee Ryan
Waiting, watching the clock
Breathing, like I'm gonna stop.
Knowing, this door's gonna knock
And there you'll be
And I pick you up
In my car
Take you away somewhere
We'll sit, parked in my car
Minutes, turning to hours
We'll watch the bright lights of the city
Just you and me
Dreaming
Your lips dance across mine
Our hands, fingers entwined
There we'll fulfill our desires
Set me free
Ooooh
Ooooh yeah
And I pick you up
When you're down
Take you away………
Oooooooooooooh
Yes we'll sit, parked in my car
Minutes, turning to hours
We'll watch the bright lights of the city
Just you and me
Dreaming.

music.baidu.com/song/7436858
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.