• Codeforces Round #43 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 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 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.

    Examples
    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

    解题思路:
    与之前的HOTEL题目想法相同,每个结点记录对应区间内的位置状态。
    这里用编号表示原本的[0,1]之类的区间而不是点,方便思考。
    为了解决第一辆车不需要前空f位,最后一辆车不需后空b位,将整个区间前加f,后加b长度。每次查询,查询车长+b+f的空位,返回的值由于之前扩大区间时都已经加了f,直接输出即可
    不过更新区间时,仍需要再加上f。
      1 #include <iostream>
      2 #include<bits/stdc++.h>
      3 #include <stack>
      4 #include <queue>
      5 #include <map>
      6 #include <set>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <algorithm>
     10 #include <math.h>
     11 using namespace std;
     12 typedef long long ll;
     13 typedef unsigned long long ull;
     14 const int MAX=1e5+5;
     15 int L,b,f;
     16 struct node
     17 {
     18     int left,right,mid;
     19     int l,r;
     20     int status;
     21     int len()
     22     {
     23         return r-l+1;
     24     }
     25     void actl()
     26     {
     27         left=right=mid=(status?0:len());
     28     }
     29 }st[10*MAX];
     30 int posl[MAX],chang[MAX];
     31 void init(int il,int ir,int k)
     32 {
     33     st[k].status=0;
     34     st[k].l=il;
     35     st[k].r=ir;
     36     st[k].actl();
     37     if(il==ir)
     38         return;
     39     init(il,(il+ir)/2,2*k);
     40     init((il+ir)/2+1,ir,2*k+1);
     41 }
     42 void pushdown(int k)
     43 {
     44     if(st[k].status!=-1)
     45     {
     46         st[2*k].status=st[2*k+1].status=st[k].status;
     47         st[2*k].actl();
     48         st[2*k+1].actl();
     49         st[k].status=-1;
     50     }
     51 }
     52 int query(int length,int k)
     53 {
     54     if(length==1&&st[k].l==st[k].r)
     55         return st[k].l;
     56     if(st[k].mid<length)
     57         return -1;
     58     pushdown(k);
     59     if(st[2*k].mid>=length)
     60         return query(length,2*k);
     61     else if (st[2*k].right+st[2*k+1].left>=length)
     62         return st[2*k].r-st[2*k].right+1;
     63     else if(st[2*k+1].mid>=length)
     64         return query(length,2*k+1);
     65     return -1;
     66 }//返回恰是起始区间的编号或-1
     67 void update(int ul,int ur,int l,int r,int k,int val)
     68 {
     69     if(l==ul&&r==ur)
     70     {
     71         st[k].status=val;
     72         st[k].actl();
     73         return;
     74     }
     75     if(l==r)return;
     76     pushdown(k);
     77 
     78     int mid=(l+r)/2;
     79     if(ur<=mid)
     80         update(ul,ur,l,mid,2*k,val);
     81     else if(ul>mid)
     82         update(ul,ur,mid+1,r,2*k+1,val);
     83     else
     84     {
     85         update(ul,mid,l,mid,2*k,val);
     86         update(mid+1,ur,mid+1,r,2*k+1,val);
     87     }
     88     st[k].mid=max(st[2*k].mid,st[2*k+1].mid);
     89     st[k].left=st[2*k].left;
     90     st[k].right=st[2*k+1].right;
     91     st[k].mid=max(st[k].mid,st[2*k].right+st[2*k+1].left);
     92     if(st[2*k].left==st[2*k].len())
     93         st[k].left+=st[2*k+1].left;
     94     if(st[2*k+1].right==st[2*k+1].len())
     95         st[k].right+=st[2*k].right;
     96     return;
     97 }
     98 int main()
     99 {
    100     scanf("%d %d %d",&L,&f,&b);
    101     init(0,L+b+f-1,1);
    102     int x,y,num,an;
    103     scanf("%d",&num);
    104     for(int i=1;i<=num;i++)
    105     {
    106         scanf("%d %d",&x,&y);
    107         if(x==1)
    108         {
    109             an=query(y+b+f,1);
    110             if(an!=-1)
    111             {
    112                 posl[i]=an;
    113                 chang[i]=y;
    114                 update(posl[i]+f,posl[i]+f+y-1,0,L+b+f-1,1,1);
    115             }
    116             else
    117                 posl[i]=-1;
    118             printf("%d
    ",an);
    119         }
    120         else
    121         {
    122             update(posl[y]+f,posl[y]+f+chang[y]-1,0,L+f+b-1,1,0);
    123         }
    124     }
    125 }
     
  • 相关阅读:
    普林斯顿宣布开源 25 核处理器
    瑞芯微RK3399宣布系统开源,进入百余种行业市场!
    Qt浅谈之二十App自动重启及关闭子窗口
    学在LINUX下编程(各种情况比较详细)
    ASP.NET开发规范:OWIN
    IDEA14中安装go语言插件
    sqlserver不能直接create table as select
    表复制语句select into from 与 insert into select 区别鉴赏
    实现Asp.net Mvc分布式Session Redis群集
    大小端模式
  • 原文地址:https://www.cnblogs.com/quintessence/p/6477400.html
Copyright © 2020-2023  润新知