李超线段树和斜率优化求助!!!

P4655 [CEOI2017] Building Bridges

cjwdyzxfblzs @ 2023-07-14 10:47:31

不理解为什么一直 WA, 一直都是 80pts


#include <bits/stdc++.h>
#define int long long
const int N = 4e6;
const int M = 1e6 + 100;
const int INF = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f;
struct node
{
    int k, b;
    node () {}
    node (int k, int b) : k(k), b(b) {}
    inline int calc(int x) { return k * x + b; }
}tr[N];
inline void modify(int u, int l, int r, node x)
{
    if (x.calc(l) <= tr[u].calc(l) and x.calc(r) <= tr[u].calc(r))
        { tr[u] = x; return; }
    if (x.calc(l) >= tr[u].calc(l) and x.calc(r) >= tr[u].calc(r))
        return;
    if (l == r) 
    {
        if (tr[u].calc(l) > x.calc(l))
            tr[u] = x;
        return;
    }
    int mid = (l + r) >> 1;
    if (x.k <= tr[u].k)
        if (tr[u].calc(mid) > x.calc(mid))
            modify(u << 1, l, mid, tr[u]),
            tr[u] = x;
        else modify(u << 1 | 1, mid + 1, r, x);
    else if (tr[u].calc(mid) > x.calc(mid))
        modify(u << 1 | 1, mid + 1, r, tr[u]),
        tr[u] = x;
    else modify(u << 1, l, mid, x);
}
inline int query(int u, int l, int r, int x)
{
    if (!u) return INF; 
    if (l == r) return tr[u].calc(x);
    int mid = (l + r) >> 1;
    if (x <= mid) return std::min(tr[u].calc(x), query(u << 1, l, mid, x));
    return std::min(tr[u].calc(x), query(u << 1 | 1, mid + 1, r, x));
}
int n, h[N], w[N], f[N];
signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin >> n;
    for (int i = 1; i <= n; i ++ ) std::cin >> h[i];
    for (int i = 1; i <= n; i ++ ) std::cin >> w[i], w[i] += w[i - 1];
    node x; x.k = -2 * h[1], x.b = h[1] * h[1] - w[1];
    modify(1, 0, M, x);
    for (int i = 2; i <= n; i ++ )
    {
        f[i] = h[i] * h[i] + w[i - 1] + query(1, 0, M, h[i]);
        modify(1, 0, M, (node){-2 * h[i], f[i] + h[i] * h[i] - w[i]});
    }
    std::cout << f[n] << std::endl;
    return 0;
}

by cjwdyzxfblzs @ 2023-07-14 10:50:20

将query里面改了一下这个,变成90了,WA #10 #16 #19

 if (!u || (tr[u].k == 0 and tr[u].b == 0)) return INF; 

by 轩尘熙 @ 2023-07-19 18:22:35

应该是modify的l=r那里


by Otue @ 2024-03-07 22:34:45

@sjzez__chess 要将0号点的b值设为正无穷。


|