Codeforces 46D Parking Lot

本文探讨了线段树在区间更新问题中的应用,通过一个具体案例——直线停车场的停车和出车操作,展示了如何使用线段树进行区间查询和更新。文章详细解释了线段树的构建、下推、上拉等关键操作,并提供了完整的C++实现代码。

题意

直线停车场长度L(10≤L≤105)L(10 \le L \le 10^5)L(10L105),车长xxx,头尾还需空出b,f(1≤x≤1000,1≤b,f≤100)b,f(1\le x \le1000, 1\le b,f \le 100)b,f(1x1000,1b,f100)的距离。最多有100次操作:停车和出车操作。对于每个停车操作输出这辆车停的位置(尽量靠左的位置),对于出车操作保证这里车是在停车场内的

算法:线段树区间更新

100次操作次数似乎可以暴力模拟解决,不过对于区间操作的问题,应该交给线段树,这是带区间更新的线段树模板题。需要注意的细节是查询时区间是b+x+fb+x+fb+x+f,更新时b,fb,fb,f可以重合,边界是可以忽略b,fb,fb,f

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int maxn = 110000;
int l, b, f;
int a[110], c[110];
int vis[maxn << 2], sum[maxn << 2], lsum[maxn << 2], rsum[maxn << 2];

void build(int l, int r, int node)
{
    vis[node] = -1;
    lsum[node] = rsum[node] = sum[node] = r - l + 1;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, node << 1);
    build(mid + 1, r, node << 1 | 1);
}

void pushdown(int l, int r, int node)
{
    int len = r - l + 1;
    int lnode = node << 1;
    int rnode = lnode | 1;
    if (vis[node] != -1)
    {
        vis[lnode] = vis[rnode] = vis[node];
        lsum[lnode] = rsum[lnode] = sum[lnode] = vis[node] ? 0 : len - (len >> 1); 
        lsum[rnode] = rsum[rnode] = sum[rnode] = vis[node] ? 0 : len >> 1; 
        vis[node] = -1;
    }
}

int query(int a, int l, int r, int node)
{
    if (l == r)
        return l;
    pushdown(l, r, node);
    int mid = (l + r) >> 1;
    int lnode = node << 1;
    int rnode = lnode | 1;
    if (sum[lnode] >= a)
        return query(a, l, mid, lnode);
    else if (rsum[lnode] + lsum[rnode] >= a)
        return mid - rsum[lnode] + 1 + b;
    else
        return query(a, mid + 1, r, rnode);
}

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

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;
        vis[node] = v;
        return ;
    }
    pushdown(l, r, node);
    int mid = (l + r) >> 1;
    if (x <= mid)
        update(x, y, v, l, mid, node << 1);
    if (y > mid)
        update(x, y, v, mid + 1, r, node << 1 | 1);
    pushup(l, r, node);
}

int main()
{
    int n, opt, x, cnt = 0;
    while (scanf("%d%d%d", &l, &b, &f) != EOF)
    {
        l += b + f;
        build(1, l, 1);
        scanf("%d", &n);
        while (n--)
        {
            scanf("%d", &opt);
            cnt++;
            if (opt == 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 (opt == 2)
            {
                scanf("%d", &x);
                update(c[x], c[x] + a[x] - 1, 0, 1, l, 1);
            }
        }
    }
    
    return 0;
}
### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值