题解:P7408 [JOI 2021 Final] ダンジョン 3

· · 题解

考虑单组询问,一些较为明显的观察:如果没有油量的限制,那么每一条边的油,都来自于它对应油费的前缀最小值。

加上油量也差不多,对于一个点 i,假设在这里加油,那么加到 b_j \leq b_i 的最小的 j 就不会再加了,或者油量满了也不会再加了。

可以求一个前缀和 pre_i=\sum_{j=1}^{i-1}a_j,看做每个点在由油量构成的数轴上的位置。

那么对于每个点,可以用一个区间 [l_i,r_i] 描述每个点对油量贡献了哪一段。计算方式:

现在 T=n+1 的部分分就好做了,因为每个点的 lst,nxt 只会有 O(1) 的变化量,所以可以直接从后往前对 S 扫描线,\sum (r_i-l_i)\times b_i 是一个区间加一次函数的形式,树状数组就可以了(注意 pre_{lst_i}+U\geq pre_{nxt_i} 时区间是空,而不是负数)。

而对于 T 任意的情况,发现 l_i,r_i 只和前驱后继有关,所以找到 [pre_T-U,pre_T] 内的 b 最小的 p,此时只有 p 的 nxt 变化是有影响的。所以只需要差分掉 S,p 的 T=n+1 的答案,然后再直接加上 b_p\times (pre_T-pre_p) 的贡献即可。

复杂度 O(n\log n)。

#define N 200110
int n, m;
int a[N], b[N], tmp[N], hb[N];
int l_[N], r_[N], u_[N];
struct RMQ
{
    pii ST[N][20];
    void init(int *a, int op)
    {
        for(int i = 1; i <= n + 1; i++) ST[i][0] = mkp(a[i] * op, i);
        for(int j = 1; j < 20; j++) for(int i = 1; i + (1 << j) - 1 <= n + 1; i++) ST[i][j] = std::min(ST[i][j - 1], ST[i + (1 << (j - 1))][j - 1]);
    }
    pii query(int l, int r)
    {
        int b = hb[r - l + 1];
        return std::min(ST[l][b], ST[r - (1 << b) + 1][b]);
    }
}STA, STB;
struct node { int u, i, op; };
std::vector <node> qvec[N];
int ans[N], pre[N];
int nxt[N], sta[N], top;
int trk[N], trb[N];
struct Lish
{
    int sta[N], top;
    void ins(int x) { sta[++top] = x; }
    void build() { std::sort(sta + 1, sta + 1 + top); top = std::unique(sta + 1, sta + 1 + top) - sta - 1; }
    int lower(int x) { return std::lower_bound(sta + 1, sta + 1 + top, x) - sta; }
    int upper(int x) { return std::upper_bound(sta + 1, sta + 1 + top, x) - sta; }
}X;
void ins(int x, int k, int b) { for(; x <= X.top; x += x & (-x)) trk[x] += k, trb[x] += b; }
void ins(int l, int r, int k, int b) 
{ 
    l = X.lower(l); r = X.upper(r) - 1;
    if(l > r) return;
    ins(l, k, b); ins(r + 1, -k, -b);
}
int queryk(int x) { int res = 0; for(; x; x -= x & (-x)) res += trk[x]; return res; }
int queryb(int x) { int res = 0; for(; x; x -= x & (-x)) res += trb[x]; return res; }
int query(int x) { return queryk(X.lower(x)) * x + queryb(X.lower(x)); }

void solve()
{
//  memset(h, idx = -1, sizeof(h));
    n = read(), m = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    for(int i = 1; i <= n; i++) b[i] = read();
    b[n + 1] = 0;
    pre[0] = -2000000000ll;
    for(int i = 2; i <= n + 1; i++) pre[i] = pre[i - 1] + a[i - 1];
    STA.init(a, -1); STB.init(b, 1);
    for(int i = 1, l, r, u; i <= m; i++) 
    {
        l = l_[i] = read(), r = r_[i] = read(), u = u_[i] = read();
        X.ins(u_[i]);
        if(-STA.query(l, r - 1).fi > u) { ans[i] = -1; continue; }
        int p = std::lower_bound(pre + 1, pre + 2 + n, pre[r] - u) - pre;
        ckmax(p, l); pii tmp = STB.query(p, r);
        p = tmp.se;
        ans[i] += tmp.fi * (pre[r] - pre[p]);
        qvec[l].push_back((node){u, i, 1});
        qvec[p].push_back((node){u, i, -1});
    }
    X.build();
    sta[++top] = n + 1; 
    for(int i = n; i >= 1; i--)
    {
        auto push = [&](int x, int y, int z, int op) -> void
        {
            int base = pre[y] - pre[x], c2 = base, c1 = pre[z] - pre[y], lim = pre[z] - pre[x] - 1;
            // base + min(c1, U) - max(c2, U)
            if(c1 <= c2)
            {
                ins(1, std::min(c1 - 1, lim), 1 * op, (base - c2) * op);
                ins(c1, std::min(c2, lim), 0, (base + c1 - c2) * op);
                ins(c2 + 1, lim, -1 * op, (base + c1) * op);
            }
            else
            {
                ins(1, std::min(c2 - 1, lim), 1 * op, (base - c2) * op);
                ins(c2, std::min(c1, lim), 0, (base) * op);
                ins(c1 + 1, lim, -1 * op, (base + c1) * op);
            }
        };
        for(; top && b[sta[top]] > b[i]; push(0, sta[top], nxt[sta[top]], -b[sta[top]]), push(i, sta[top], nxt[sta[top]], b[sta[top]]), top--);
        nxt[i] = sta[top]; push(0, i, sta[top], b[i]); sta[++top] = i;
        for(node t : qvec[i]) ans[t.i] += query(t.u) * t.op;
    }
    for(int i = 1; i <= m; i++) print(ans[i], '\n');
}

void init()
{
    int n = N - 10;
    hb[0] = -1;
    for(int i = 1; i <= n; i++) hb[i] = hb[i >> 1] + 1;
}