P6794 [SNOI2020] 水池 题解
bluewindde · · 题解
好厉害的题。
为了方便,规定新的
对于灌水操作,二分找到
考虑排水操作,若
可持久化线段树维护除掉最左侧挡板外的最大挡板高度
设当前节点为
- 左儿子的左侧排水标记
\text{p.ls.ld} = \min(\max(\text{p.ls.ld}, \text{p.fd}), \text{p.ld}) ; - 右儿子的左侧排水标记
\text{p.rs.ld} = \min(\max(\text{p.rs.ld}, \text{p.fd}), \min(\text{p.ld}, \text{p.ls.rh})) ,向右儿子传递的左侧标记要包含左儿子的区间; - 右侧排水标记同理;
- 灌水标记直接取最大值即可;
- 标记清空
\text{p.fd} = 0, \text{p.ld} = \text{p.rd} = +\infty 。
最终格子的高度为
信息合并是容易的,增加挡板高度和查询也是容易的。时间复杂度
#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;
}