P6794 [SNOI2020] 水池 题解

· · 题解

好厉害的题。

为了方便,规定新的 h_i 是原来的 h_{i - 1},h_1 = h_{n + 1} = +\infty,然后令 n \leftarrow n + 1。设 a_i 表示第 i 格的水深。

对于灌水操作,二分找到 x 左边第一个满足 h_l \geqslant h 的位置 l,x 右边第一个满足 h_{r-1} \geqslant h 的位置 r,区间 [l, r] 的 a_i 对 h 取最大值即可。

考虑排水操作,若 i \leqslant x,则 a_i = \min(a_i, \max\limits_{i < j \leqslant x}(h_j)),称为一次 i 的右侧排水,权值为 \max\limits_{i \leqslant j < x}(h_j);i > x 是对称的,称为一次 i 的左侧排水。

可持久化线段树维护除掉最左侧挡板外的最大挡板高度 \text h、除掉最右侧挡板外的最大挡板高度 \text{rh},维护灌水标记 \text{fd}(flood)、左侧排水标记 \text{ld}(left side drain)、右侧排水标记 \text{rd}(right side drain)。灌水标记可以覆盖其他标记。两种排水标记表示对区间执行对应排水操作的权值。

设当前节点为 \text p,下放标记:

最终格子的高度为 \min(\text{p.fd}, \text{p.ld}, \text{p.rd})。

信息合并是容易的,增加挡板高度和查询也是容易的。时间复杂度 O(q \log n)。

#include <iostream>

using namespace std;

const int inf = 2e9;

int n, q;
int h[200005];

int rt[200005], cnt;
struct node {
    int ls, rs, h, rh, fd, ld, rd;
} d[40000005];
static inline int clone(int p) {
    d[++cnt] = d[p];
    return cnt;
}
static inline void pushup(int p) {
    d[p].h = max(d[d[p].ls].h, d[d[p].rs].h);
    d[p].rh = max(d[d[p].ls].rh, d[d[p].rs].rh);
}
static inline void pushdown(int p) {
    d[p].ls = clone(d[p].ls);
    d[p].rs = clone(d[p].rs);
    d[d[p].ls].ld = min(max(d[d[p].ls].ld, d[p].fd), d[p].ld);
    d[d[p].rs].ld = min(max(d[d[p].rs].ld, d[p].fd), max(d[p].ld, d[d[p].ls].rh));
    d[d[p].ls].rd = min(max(d[d[p].ls].rd, d[p].fd), max(d[p].rd, d[d[p].rs].h));
    d[d[p].rs].rd = min(max(d[d[p].rs].rd, d[p].fd), d[p].rd);
    d[d[p].ls].fd = max(d[d[p].ls].fd, d[p].fd);
    d[d[p].rs].fd = max(d[d[p].rs].fd, d[p].fd);
    d[p].fd = -inf;
    d[p].ld = d[p].rd = inf;
}
static inline int build(int s, int t) {
    int p = ++cnt;
    d[p].fd = -inf;
    d[p].ld = d[p].rd = inf;
    if (s == t) {
        d[p].h = h[s];
        d[p].rh = h[s + 1];
        d[p].fd = 0;
        return p;
    }
    int mid = (s + t) >> 1;
    d[p].ls = build(s, mid);
    d[p].rs = build(mid + 1, t);
    pushup(p);
    return p;
}
static inline int query1(int x, int s, int t, int c, int p) {
    if (s == t)
        return d[p].h >= c ? s : 0;
    int mid = (s + t) >> 1;
    if (x <= mid)
        return query1(x, s, mid, c, d[p].ls);
    if (t <= x) {
        if (d[d[p].rs].h >= c)
            return query1(x, mid + 1, t, c, d[p].rs);
        return query1(x, s, mid, c, d[p].ls);
    }
    int ret = query1(x, mid + 1, t, c, d[p].rs);
    if (!ret)
        ret = query1(x, s, mid, c, d[p].ls);
    return ret;
}
static inline int query2(int x, int s, int t, int c, int p) {
    if (s == t)
        return d[p].h >= c ? s : 0;
    int mid = (s + t) >> 1;
    if (x > mid)
        return query2(x, mid + 1, t, c, d[p].rs);
    if (s >= x) {
        if (d[d[p].ls].h >= c)
            return query2(x, s, mid, c, d[p].ls);
        return query2(x, mid + 1, t, c, d[p].rs);
    }
    int ret = query2(x, s, mid, c, d[p].ls);
    if (!ret)
        ret = query2(x, mid + 1, t, c, d[p].rs);
    return ret;
}
static inline void flood(int l, int r, int s, int t, int c, int p) {
    if (l <= s && r >= t) {
        d[p].fd = max(d[p].fd, c);
        d[p].ld = max(d[p].ld, c);
        d[p].rd = max(d[p].rd, c);
        return;
    }
    int mid = (s + t) >> 1;
    pushdown(p);
    if (l <= mid)
        flood(l, r, s, mid, c, d[p].ls);
    if (r > mid)
        flood(l, r, mid + 1, t, c, d[p].rs);
}
static inline int drain1(int x, int s, int t, int c, int p) {
    if (x >= t) {
        d[p].rd = min(d[p].rd, c);
        return max(c, d[p].h);
    }
    int mid = (s + t) >> 1;
    pushdown(p);
    if (x <= mid)
        return drain1(x, s, mid, c, d[p].ls);
    return drain1(x, s, mid, drain1(x, mid + 1, t, c, d[p].rs), d[p].ls);
}
static inline int drain2(int x, int s, int t, int c, int p) {
    if (x <= s) {
        d[p].ld = min(d[p].ld, c);
        return max(c, d[p].rh);
    }
    int mid = (s + t) >> 1;
    pushdown(p);
    if (x > mid)
        return drain2(x, mid + 1, t, c, d[p].rs);
    return drain2(x, mid + 1, t, drain2(x, s, mid, c, d[p].ls), d[p].rs);
}
static inline void update(int x, int s, int t, int c, int p) {
    if (s == t) {
        d[p].h = c;
        return;
    }
    int mid = (s + t) >> 1;
    pushdown(p);
    if (x <= mid)
        update(x, s, mid, c, d[p].ls);
    else
        update(x, mid + 1, t, c, d[p].rs);
    pushup(p);
}
static inline void updaterh(int x, int s, int t, int c, int p) {
    if (s == t) {
        d[p].rh = c;
        return;
    }
    int mid = (s + t) >> 1;
    pushdown(p);
    if (x <= mid)
        updaterh(x, s, mid, c, d[p].ls);
    else
        updaterh(x, mid + 1, t, c, d[p].rs);
    pushup(p);
}
static inline int query(int x, int s, int t, int p) {
    if (s == t)
        return min(d[p].fd, min(d[p].ld, d[p].rd));
    int mid = (s + t) >> 1;
    pushdown(p);
    if (x <= mid)
        return query(x, s, mid, d[p].ls);
    return query(x, mid + 1, t, d[p].rs);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (int i = 2; i <= n; ++i)
        cin >> h[i];
    h[1] = h[++n] = inf;
    rt[0] = build(1, n);
    for (int i = 1; i <= q; ++i) {
        int op, old, x;
        cin >> op >> old >> x;
        rt[i] = clone(rt[old]);
        if (op == 0) {
            int h;
            cin >> h;
            int l = query1(x, 1, n, h, rt[i]);
            int r = query2(x + 1, 1, n, h, rt[i]) - 1;
            flood(l, r, 1, n, h, rt[i]);
        } else if (op == 1) {
            drain1(x, 1, n, 0, rt[i]);
            drain2(x, 1, n, 0, rt[i]);
        } else if (op == 2) {
            int h;
            cin >> h;
            update(x + 1, 1, n, h, rt[i]);
            updaterh(x, 1, n, h, rt[i]);
        } else if (op == 3)
            cout << query(x, 1, n, rt[i]) << '\n';
    }
    return 0;
}