CodeForces 46D Parking Pot(线段树区间更新)

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

题意:一段线性的停车场,长度为L,停车时从零点坐标开始停,要求保证新停入的车前后间距分别为f,b(在停车场的边缘不考虑车前间距或车后间距)。有n次操作,1 a表示停入一辆长度为a的车,要求输出车尾的坐标,2 a表示第a次操作中的那辆车移出停车场(注意:第a次操作不是以1开头的操作中的第a个,因为看错这个,我不停的RE......)。

明显的线段树区间更新,因为之前做过一道类似的题,保存了模板,所以这道题就直接套模板了。为了方便考虑车的前后间距,给停车场的长度加上b + f,这样就不用分类讨论了。

#include <cstdio>
#include <iostream>
#include <algorithm>
#define ls node << 1
#define rs node << 1 | 1
#define lson l, mid, ls
#define rson mid + 1, r, rs

using namespace std;

int l, b, f;
int a[105], c[105];
int sum[101205 << 2], lsum[101205 << 2], rsum[101205 << 2], visit[101205 << 2];

void pushup(int l, int r, int node)
{
    int len = r - l + 1;
    lsum[node] = lsum[ls];
    rsum[node] = rsum[rs];
    if(lsum[node] == len - (len >> 1))
        lsum[node] += lsum[rs];
    if(rsum[node] == (len >> 1))
        rsum[node] += rsum[ls];
    sum[node] = max(rsum[ls] + lsum[rs], max(sum[ls], sum[rs]));
}

void pushdown(int l, int r, int node)
{
    int len = r - l + 1;
    if(visit[node] != -1) {
        visit[ls] = visit[rs] = visit[node];
        lsum[ls] = rsum[ls] = sum[ls] = visit[node] ? 0 : len - (len >> 1);
        lsum[rs] = rsum[rs] = sum[rs] = visit[node] ? 0 : (len >> 1);
        visit[node] = -1;
    }
}

void build(int l, int r, int node)
{
    visit[node] = -1;
    lsum[node] = rsum[node] = sum[node] = r - l + 1;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}
void update(int x, int y, int v,int l, int r, int node)
{
    if(x <= l&&y >= r) {
        lsum[node] = rsum[node] = sum[node] = v ? 0 : r - l + 1;
        visit[node] = v;
        return;
    }
    pushdown(l, r, node);
    int mid = (l + r) >> 1;
    if(x <= mid)
        update(x, y, v, lson);
    if(y > mid)
        update(x, y, v, rson);
    pushup(l, r, node);
}
int query(int a, int l, int r, int node)
{
    if(l == r)
        return l;
    pushdown(l, r, node);
    int mid = (l + r) >> 1;
    if(sum[ls] >= a)
        return query(a, lson);
    else if(rsum[ls] + lsum[rs] >= a)
        return mid - rsum[ls] + 1 + b;
    else return query(a, rson);
}

int main()
{
    int n, choice, x, cnt = 0;
    while(scanf("%d%d%d",&l, &b, &f) != EOF) {
        l = l + b + f;
        build(1, l, 1);
        scanf("%d",&n);
        while(n--) {
            scanf("%d",&choice);
            ++cnt;
            if(choice == 1) {
                scanf("%d",&a[cnt]);
                if(sum[1] < a[cnt] + b + f)
                    printf("-1\n");
                else {
                    c[cnt] = query(a[cnt] + b + f, 1, l, 1);
                    update(c[cnt], c[cnt] + a[cnt] - 1, 1, 1, l, 1);
                    printf("%d\n", c[cnt] - 1 - b);
                }
            } else if(choice == 2) {
                scanf("%d",&x);
                update(c[x], c[x] + a[x] - 1, 0, 1, l, 1);
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
区间DP是一种动态规划的方法,用于解决区间范围内的问题。在Codeforces竞赛中,区间DP经常被用于解决一些复杂的字符串或序列相关的问题。 在区间DP中,dp[i][j]表示第一个序列前i个元素和第二个序列前j个元素的最优解。具体的转移方程会根据具体的问题而变化,但是通常会涉及到比较两个序列的元素是否相等,然后根据不同的情况进行状态转移。 对于区间长度为1的情况,可以先进行初始化,然后再通过枚举区间长度和区间左端点,计算出dp[i][j]的值。 以下是一个示例代码,展示了如何使用区间DP来解决一个字符串匹配的问题: #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; const int maxn=510; const int inf=0x3f3f3f3f; int n,dp[maxn][maxn]; char s[maxn]; int main() { scanf("%d", &n); scanf("%s", s + 1); for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int i = 1; i <= n; i++) { if(s[i] == s[i - 1]) dp[i][i - 1] = 1; else dp[i][i - 1] = 2; } for(int len = 3; len <= n; len++) { int r; for(int l = 1; l + len - 1 <= n; l++) { r = l + len - 1; dp[l][r] = inf; if(s[l] == s[r]) dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]); else { for(int k = l; k <= r; k++) { dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]); } } } } printf("%d\n", dp[n]); return 0; } 希望这个例子能帮助你理解区间DP的基本思想和应用方法。如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值