题解:P4118 [Ynoi2018] 末日时在做什么?有没有空?可以来拯救吗?

· · 题解

P4118 [Ynoi2018] 末日时在做什么?有没有空?可以来拯救吗?

前言

这题是 P5073 [Ynoi Easy Round 2015] 世上最幸福的女孩 的加强版,所以先讲弱化版。

::::info[弱化版]

不带修改的线段树做法

线段树每个节点维护四个值:

合并:

\begin{aligned} sum &= sum_L + sum_R,\\ lmax &= \max(lmax_L,\ sum_L + lmax_R),\\ rmax &= \max(rmax_R,\ sum_R + rmax_L),\\ tmax &= \max(tmax_L,\ tmax_R,\ rmax_L + lmax_R). \end{aligned}

因为可以不选数,所有 lmax,rmax,tmax 至少为 0。

全局加时发生了什么

如果整个区间都加上 x,那么 sum 变成 sum + len\cdot x。但 lmax, rmax, tmax 不是简单加一个固定值,因为最大子段和可以选择不包含某些元素。

其实,对于固定的节点,当整体加上 x 时:

这些函数都是关于 x 的凸分段线性函数。所以可以用凸包来维护:每个节点记下当前最优直线的斜率 k 和截距 b,以及下一条直线超过当前直线的阈值 t。

当整体加上 x 时:

离线处理

因为是全局加,所有修改加的是同一个值。把所有询问离线下来,记录每个询问发生时的全局加量 add,按 add 从小到大排序。

初始时把所有数加上最小可能的 add(记为 minn),然后依次处理询问:

单次查询 O(\log n),全局加均摊也是 O(\log n),总复杂度 O((n+m)\log n)。

::::info[本题]

弱化版的做法在本题上不适用,因为区间加不是全局的,不能简单离线排序。但我们可以用分块把区间加拆成“整块全局加”和“散块暴力”。

分块

把序列分成 K=\lceil n/B\rceil 个块,块大小 B\approx 250。

对每个块单独建立一棵线段树,线段树的节点维护的东西和弱化版一样:sum,lmax,rmax,tmax 以及对应的斜率、阈值、懒标记。

对每个块,遍历所有 m 个操作,只考虑与该块相交的部分:

合起来一起算

当完全覆盖的查询攒到一定数量,就一起处理:

因为 tag 递增,根节点的最优直线只会单调移动,所以可以用指针扫一遍凸包。

最后再处理剩下的。

块内线段树的凸包合并

块内线段树的 push_up 需要合并左右儿子的信息,并重新构建父节点的凸包。合并时用到 mergeh 函数:给定左右儿子的 rmax 和 lmax 凸包,以及它们各自的 tag,计算出父节点 tmax 的凸包。

具体做法就是利用凸包的性质,类似归并排序地扫描两个凸包的差分向量,同时维护当前的最优值。

复杂度

设块大小为 B,块数 K=n/B。

取 B\approx 250,n,m\le 10^5 时可以通过。

AC 代码

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAX = (1 << 16) - 1;
const int BLOCK = 250;
char buf[1 << 20], out[1 << 20];
int idx = 0, size = 0, outidx = 0;
inline int gc() {
    if (idx >= size) {
        size = fread(buf, 1, 1 << 20, stdin);
        idx = 0;
        if (!size) {
            return EOF;
        }
    }
    return buf[idx++];
}
template <typename T>
inline void read(T &x) {
    x = 0;
    T f = 1;
    int c = gc();
    while (c < '0' || c > '9') { 
        if (c == '-') {
            f = -1; 
        }
        c = gc(); 
    }
    while (c >= '0' && c <= '9') { 
        x = x * 10 + c - '0'; 
        c = gc(); 
    }
    x *= f;
}
inline void pc(char c) {
    if (outidx == (1 << 20)) {
        fwrite(out, 1, outidx, stdout);
        outidx = 0;
    }
    out[outidx++] = c;
}
inline void write(long long x) {
    if (x == 0) {
        pc('0'); 
        pc('\n'); 
        return; 
    }
    if (x < 0) { 
        pc('-'); 
        x = -x; 
    }
    char s[25];
    int top = 0;
    while (x) { 
        s[top++] = x % 10 + '0'; 
        x /= 10; 
    }
    while (top) {
        pc(s[--top]);
    }
    pc('\n');
}
struct Node {
    long long lmax, rmax, tmax, sum;
} ans[MAXN];
struct Point {
    int x;
    long long y;
} pl[MAXN], t1[BLOCK + 5], t2[BLOCK + 5];
inline Point operator + (const Point &a, const Point &b) {
    Point c;
    c.x = a.x + b.x;
    c.y = a.y + b.y;
    return c;
}
inline Point operator - (const Point &a, const Point &b) {
    Point c;
    c.x = a.x - b.x;
    c.y = a.y - b.y;
    return c;
}
inline long long operator * (const Point &a, const Point &b) {
    return 1LL * a.x * b.y - 1LL * a.y * b.x;
}
struct Hull {
    int len;
    Point *p;
    inline void init(Point *q) { 
        len = 0; 
        p = q; 
    }
    inline void insert(Point x) {
        while (len > 1) {
            Point a = x - p[len - 1];
            Point b = p[len] - p[len - 1];
            if (a * b <= 0) {
                --len;
            }
            else {
                break;
            }
        }
        p[++len] = x;
    }
    inline long long get(long long k) const {
        int l = 1, r = len - 1, pos = len;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (p[mid + 1].y + 1LL * p[mid + 1].x * k > p[mid].y + 1LL * p[mid].x * k) {
                l = mid + 1;
            } 
            else {
                pos = mid;
                r = mid - 1;
            }
        }
        return p[pos].y + 1LL * p[pos].x * k;
    }
} lmax[(BLOCK + 5) << 2], rmax[(BLOCK + 5) << 2], tmax[(BLOCK + 5) << 2];
struct Query {
    int op, l, r;
    long long x, k;
} q[MAXN];
long long sum[(BLOCK + 5) << 2], tag[(BLOCK + 5) << 2], best[BLOCK + 5];
int a[BLOCK + 5], b[MAXN], len[(BLOCK + 5) << 2], l[MAXN], r[MAXN], id[MAXN], tid[MAXN], cnt[1 << 16], tot, blocks, qcnt, n, m;
inline void mergeh(const Hull &L, const Hull &R, long long tagl, long long tagr) {
    for (int i = 1; i < L.len; ++i) {
        t1[i] = L.p[i + 1] - L.p[i];
        t1[i].y += 1LL * t1[i].x * tagl;
    }
    for (int i = 1; i < R.len; ++i) {
        t2[i] = R.p[i + 1] - R.p[i];
        t2[i].y += 1LL * t2[i].x * tagr;
    }
    Point cur = L.p[1] + R.p[1];
    cur.y += 1LL * cur.x * tagl + 1LL * R.p[1].x * tagr;
    best[cur.x] = max(best[cur.x], cur.y);
    int i = 1, j = 1;
    while (i < L.len && j < R.len) {
        if (t1[i] * t2[j] <= 0) {
            cur = cur + t1[i++];
        }
        else {
            cur = cur + t2[j++];
        }
        best[cur.x] = max(best[cur.x], cur.y);
    }
    while (i < L.len) { 
        cur = cur + t1[i++]; 
        best[cur.x] = max(best[cur.x], cur.y); 
    }
    while (j < R.len) { 
        cur = cur + t2[j++]; 
        best[cur.x] = max(best[cur.x], cur.y); 
    }
}
inline void push_up(int p) {
    lmax[p].len = rmax[p].len = tmax[p].len = 0;
    long long lsum = sum[p << 1] + tag[p << 1] * len[p << 1], rsum = sum[p << 1 | 1] + tag[p << 1 | 1] * len[p << 1 | 1];
    sum[p] = lsum + rsum;
    Point x;
    for (int i = 1; i <= lmax[p << 1].len; ++i) {
        x = lmax[p << 1].p[i];
        x.y += 1LL * x.x * tag[p << 1];
        lmax[p].insert(x);
    }
    for (int i = 1; i <= lmax[p << 1 | 1].len; ++i) {
        x = lmax[p << 1 | 1].p[i];
        x.y += 1LL * x.x * tag[p << 1 | 1] + lsum;
        x.x += len[p << 1];
        lmax[p].insert(x);
    }
    for (int i = 1; i <= rmax[p << 1 | 1].len; ++i) {
        x = rmax[p << 1 | 1].p[i];
        x.y += 1LL * x.x * tag[p << 1 | 1];
        rmax[p].insert(x);
    }
    for (int i = 1; i <= rmax[p << 1].len; ++i) {
        x = rmax[p << 1].p[i];
        x.y += 1LL * x.x * tag[p << 1] + rsum;
        x.x += len[p << 1 | 1];
        rmax[p].insert(x);
    }
    for (int i = 0; i <= len[p]; ++i) {
        best[i] = -0x3f3f3f3f3f3f3f3fLL;
    }
    mergeh(rmax[p << 1], lmax[p << 1 | 1], tag[p << 1], tag[p << 1 | 1]);
    for (int i = 1; i <= tmax[p << 1].len; ++i) {
        x = tmax[p << 1].p[i];
        x.y += 1LL * x.x * tag[p << 1];
        best[x.x] = max(best[x.x], x.y);
    }
    for (int i = 1; i <= tmax[p << 1 | 1].len; ++i) {
        x = tmax[p << 1 | 1].p[i];
        x.y += 1LL * x.x * tag[p << 1 | 1];
        best[x.x] = max(best[x.x], x.y);
    }
    for (int i = 0; i <= len[p]; ++i) {
        if (best[i] != -0x3f3f3f3f3f3f3f3fLL) {
            x.x = i;
            x.y = best[i];
            tmax[p].insert(x);
        }
    }
    tag[p] = 0;
}
inline void build(int p, int l, int r) {
    len[p] = r - l + 1;
    tag[p] = 0;
    lmax[p].init(pl + tot);
    tot += r - l + 2;
    rmax[p].init(pl + tot);
    tot += r - l + 2;
    tmax[p].init(pl + tot);
    tot += r - l + 2;
    if (l == r) {
        sum[p] = a[l];
        lmax[p].len = rmax[p].len = tmax[p].len = 2;
        lmax[p].p[1].x = 0; lmax[p].p[1].y = 0;
        lmax[p].p[2].x = 1; lmax[p].p[2].y = a[l];
        rmax[p].p[1].x = 0; rmax[p].p[1].y = 0;
        rmax[p].p[2].x = 1; rmax[p].p[2].y = a[l];
        tmax[p].p[1].x = 0; tmax[p].p[1].y = 0;
        tmax[p].p[2].x = 1; tmax[p].p[2].y = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    push_up(p);
}
inline void Addt(int p, long long x) {
    tag[p] += x;
}
inline void push_down(int p) {
    if (tag[p]) {
        long long x = tag[p];
        Addt(p << 1, x);
        Addt(p << 1 | 1, x);
        tag[p] = 0;
    }
}
inline void Add(int p, int l, int r, int ql, int qr, long long x) {
    if (ql <= l && r <= qr) {
        Addt(p, x);
        return;
    }
    push_down(p);
    int mid = (l + r) >> 1;
    if (ql <= mid) {
        Add(p << 1, l, mid, ql, qr, x);
    }
    if (qr > mid) {
        Add(p << 1 | 1, mid + 1, r, ql, qr, x);
    }
    push_up(p);
}
inline Node mergen(const Node &a, const Node &b) {
    Node c;
    c.lmax = max(a.lmax, a.sum + b.lmax);
    c.rmax = max(b.rmax, b.sum + a.rmax);
    c.tmax = max(max(a.tmax, b.tmax), a.rmax + b.lmax);
    c.sum = a.sum + b.sum;
    return c;
}
inline Node query(int p, int l, int r, int ql, int qr, long long x) {
    if (ql <= l && r <= qr) {
        Node res;
        long long k = x + tag[p];
        res.lmax = lmax[p].get(k);
        res.rmax = rmax[p].get(k);
        res.tmax = tmax[p].get(k);
        res.sum = sum[p] + k * len[p];
        return res;
    }
    int mid = (l + r) >> 1;
    x += tag[p];
    if (qr <= mid) {
        return query(p << 1, l, mid, ql, qr, x);
    }
    if (ql > mid) {
        return query(p << 1 | 1, mid + 1, r, ql, qr, x);
    }
    Node L = query(p << 1, l, mid, ql, qr, x), R = query(p << 1 | 1, mid + 1, r, ql, qr, x);
    return mergen(L, R);
}
inline bool cmp(int x, int y) {
    return q[x].k < q[y].k;
}
inline void rsort(int n) {
    sort(id + 1, id + n + 1, cmp);
}
inline void solvep() {
    if (!qcnt) {
        return;
    }
    rsort(qcnt);
    int pl = 1, pr = 1, pt = 1;
    for (int i = 1; i <= qcnt; ++i) {
        int cur = id[i];
        long long x = q[cur].k;
        while (pl < lmax[1].len && lmax[1].p[pl + 1].y + 1LL * lmax[1].p[pl + 1].x * x > lmax[1].p[pl].y + 1LL * lmax[1].p[pl].x * x) {
            ++pl;
        }
        while (pr < rmax[1].len && rmax[1].p[pr + 1].y + 1LL * rmax[1].p[pr + 1].x * x > rmax[1].p[pr].y + 1LL * rmax[1].p[pr].x * x) {
            ++pr;
        }
        while (pt < tmax[1].len && tmax[1].p[pt + 1].y + 1LL * tmax[1].p[pt + 1].x * x > tmax[1].p[pt].y + 1LL * tmax[1].p[pt].x * x) {
            ++pt;
        }
        Node res;
        res.lmax = lmax[1].p[pl].y + 1LL * lmax[1].p[pl].x * x;
        res.rmax = rmax[1].p[pr].y + 1LL * rmax[1].p[pr].x * x;
        res.tmax = tmax[1].p[pt].y + 1LL * tmax[1].p[pt].x * x;
        res.sum = sum[1] + x * len[1];
        ans[cur] = mergen(ans[cur], res);
    }
    qcnt = 0;
}
inline void solveb(int k) {
    tot = 0;
    qcnt = 0;
    int lpos = l[k], rpos = r[k], lenb = rpos - lpos + 1;
    for (int i = 1; i <= lenb; ++i) {
        a[i] = b[lpos + i - 1];
    }
    if (k == 1) {
        long long addtag = 0;
        for (int i = 1; i <= m; ++i) {
            if (q[i].op == 1) {
                int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
                if (ql > qr) {
                    continue;
                }
                if (q[i].l <= lpos && rpos <= q[i].r) {
                    addtag += q[i].x;
                }
                else {
                    for (int j = ql; j <= qr; ++j) {
                        a[j - lpos + 1] += q[i].x;
                    }
                }
            }
            else {
                int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
                if (ql > qr) {
                    continue;
                }
                int L = ql - lpos + 1, R = qr - lpos + 1;
                Node res;
                res.sum = 0;
                long long prefix = 0, suffix = 0, cur = 0, maxl = -0x3f3f3f3f3f3f3f3fLL, maxr = -0x3f3f3f3f3f3f3f3fLL, maxsum = -0x3f3f3f3f3f3f3f3fLL, Sum = 0;
                for (int j = L; j <= R; ++j) {
                    long long val = a[j] + addtag;
                    Sum += val;
                    prefix += val;
                    if (prefix > maxl) {
                        maxl = prefix;
                    }
                }
                suffix = 0;
                for (int j = R; j >= L; --j) {
                    long long val = a[j] + addtag;
                    suffix += val;
                    if (suffix > maxr) {
                        maxr = suffix;
                    }
                }
                cur = 0;
                for (int j = L; j <= R; ++j) {
                    long long val = a[j] + addtag;
                    cur = max(0LL, cur) + val;
                    if (cur > maxsum) {
                        maxsum = cur;
                    }
                }
                res.sum = Sum;
                res.lmax = maxl;
                res.rmax = maxr;
                res.tmax = maxsum;
                ans[i] = mergen(ans[i], res);
            }
        }
        return;
    }
    tot = 0;
    qcnt = 0;
    build(1, 1, lenb);
    long long x = 0;
    for (int i = 1; i <= m; ++i) {
        if (q[i].op == 1) {
            if (q[i].l <= lpos && rpos <= q[i].r) {
                x += q[i].x;
            } 
            else {
                int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
                if (ql > qr) {
                    continue;
                }
                if (qcnt > 0) {
                    if (qcnt < 200) {
                        for (int j = 1; j <= qcnt; ++j) {
                            int cur = id[j];
                            Node res = query(1, 1, lenb, 1, lenb, q[cur].k);
                            ans[cur] = mergen(ans[cur], res);
                        }
                        qcnt = 0;
                    } 
                    else {
                        solvep();
                    }
                }
                Add(1, 1, lenb, ql - lpos + 1, qr - lpos + 1, q[i].x);
            }
        } 
        else {
            if (q[i].l <= lpos && rpos <= q[i].r) {
                if (qcnt < 200) {
                    Node res = query(1, 1, lenb, 1, lenb, x);
                    ans[i] = mergen(ans[i], res);
                } 
                else {
                    id[++qcnt] = i;
                    q[i].k = x;
                }
            }
            else {
                int ql = max(q[i].l, lpos), qr = min(q[i].r, rpos);
                if (ql > qr) {
                    continue;
                }
                Node res = query(1, 1, lenb, ql - lpos + 1, qr - lpos + 1, x);
                ans[i] = mergen(ans[i], res);
            }
        }
    }
    if (qcnt > 0) {
        if (qcnt < 200) {
            for (int j = 1; j <= qcnt; ++j) {
                int cur = id[j];
                Node res = query(1, 1, lenb, 1, lenb, q[cur].k);
                ans[cur] = mergen(ans[cur], res);
            }
            qcnt = 0;
        } 
        else {
            solvep();
        }
    }
}

int main() {
    read(n);
    read(m);
    for (int i = 1; i <= n; ++i) {
        read(b[i]);
    }
    for (int i = 1; i <= m; ++i) {
        read(q[i].op);
        read(q[i].l);
        read(q[i].r);
        if (q[i].op == 1) {
            read(q[i].x);
        }
        else {
            ans[i] = {0, 0, 0, 0};
        }
    }
    blocks = (n + BLOCK - 1) / BLOCK;
    for (int i = 1; i <= blocks; ++i) {
        l[i] = (i - 1) * BLOCK + 1;
        r[i] = min(i * BLOCK, n);
    }
    for (int i = 1; i <= blocks; ++i) {
        solveb(i);
    }
    for (int i = 1; i <= m; ++i) {
        if (q[i].op == 2) {
            write(max(0LL, ans[i].tmax));
        }
    }
    fwrite(out, 1, outidx, stdout);
    return 0;
}