题解:P11993 [JOIST 2025] 迁移计划 / Migration Plan
xuyifei1599 · · 题解
闲话:本来想写树套树的来着,后来在优化的过程中整出来的。
我们考虑题目中海狸移动的本质。由于树上的单向道路均指向父亲节点,任意海狸的所有迁移操作都只能沿着通往根节点的路径向上进行。因此,一只出生在城市
由此可得,对于任意时刻在深度为
- 该海狸在当前时刻所处的深度恰好为
d_B ; - 城市
A 位于城市B 的子树内,即在原树的 DFS 序上满足\text{dfn}_B \le \text{dfn}_A \le \text{dfn}_B + \text{siz}_B - 1 。
所以,原树的拓扑结构仅用于判定上述第二个空间范围条件,而海狸在各个时刻的深度转移则完全独立于树的具体形态,仅与全局深度的合并历史有关。对于每一个深度,我们可以维护当前驻留在该深度的海狸集合。初始时,每个深度各有一个初始集合;当发生迁移事件使得深度
我们可以建立一棵“集合合并树”。我们为每个深度维护当前集合的代表节点编号。当深度
在这棵合并树中,任意时刻查询深度
这样我们就把题目变成了一个经典的二维数点问题。将时间轴差分后离线处理,将所有事件与查询挂载到时间轴节点上,沿时间轴顺序扫描,遇到点时在树状数组的对应
时间复杂度
#include<bits/stdc++.h>
// #define int long long
using namespace std;
namespace FastIO {
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], obuf[BUFSIZE];
int ipos = 0, ilen = 0, opos = 0;
inline char gc() {
if (ipos == ilen) {
ilen = fread(ibuf, 1, BUFSIZE, stdin);
ipos = 0;
if (ilen == 0) {
return EOF;
}
}
return ibuf[ipos++];
}
template<typename T>
inline void read(T &x) {
x = 0;
int f = 1;
char c = gc();
while (c != '-' && (c < '0' || c > '9')) {
c = gc();
}
if (c == '-') {
f = -1;
c = gc();
}
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = gc();
}
x *= f;
}
inline void write(long long x) {
if (x == 0) {
obuf[opos++] = '0';
obuf[opos++] = '\n';
} else {
if (x < 0) {
obuf[opos++] = '-';
x = -x;
}
char tmp[25];
int t = 0;
while (x > 0) {
tmp[t++] = '0' + x % 10;
x /= 10;
}
while (t > 0) {
obuf[opos++] = tmp[--t];
}
obuf[opos++] = '\n';
}
if (opos > BUFSIZE - 100) {
fwrite(obuf, 1, opos, stdout);
opos = 0;
}
}
inline void flush() {
fwrite(obuf, 1, opos, stdout);
opos = 0;
}
}
int n, q, dfsid, setcnt, mtimer, addcnt, q3cnt, evcnt, fa[2000005], head[2000005], nxt[2000005], deep[2000005], dfn[2000005], siz[2000005], it[2000005], sta[2000005], act[2000005], qu[2000005], qdfl[2000005], qdfr[2000005], lc[8000005], rc[8000005], tin[8000005], tout[8000005], rtin[8000005], mst[8000005], msta[8000005], addhd[8000005], addnxt[5000005], adddfn[5000005], evhd[8000005], evnxt[5000005], evl[5000005], evr[5000005], evid[5000005], evsign[5000005];
long long num[2000005], ans[2000005], bit[2000005], addval[5000005];
bool isch[8000005];
inline void bitadd(int p, long long v) {
for (; p <= n; p += p & -p) {
bit[p] += v;
}
}
inline long long bitquery(int p) {
long long res = 0;
for (; p > 0; p -= p & -p) {
res += bit[p];
}
return res;
}
void dfs() {
int top = 0;
sta[top++] = 1;
deep[1] = 0;
dfn[1] = ++dfsid;
siz[1] = 1;
it[1] = head[1];
while (top > 0) {
int u = sta[top - 1];
int child = it[u];
if (child) {
it[u] = nxt[child];
dfn[child] = ++dfsid;
siz[child] = 1;
deep[child] = deep[u] + 1;
it[child] = head[child];
sta[top++] = child;
} else {
top--;
if (top > 0) {
siz[sta[top - 1]] += siz[u];
}
}
}
}
signed main() {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
FastIO::read(n);
for (int i = 2; i <= n; i ++) {
FastIO::read(fa[i]);
nxt[i] = head[fa[i]];
head[fa[i]] = i;
}
for (int i = 1; i <= n; i ++) {
FastIO::read(num[i]);
}
dfs();
for (int i = 0; i <= n; i ++) {
act[i] = ++setcnt;
}
for (int i = 1; i <= n; i ++) {
if (num[i] > 0) {
++addcnt;
adddfn[addcnt] = dfn[i];
addval[addcnt] = num[i];
int u = act[deep[i]];
addnxt[addcnt] = addhd[u];
addhd[u] = addcnt;
}
}
FastIO::read(q);
for (int i = 1; i <= q; i ++) {
int op;
FastIO::read(op);
if (op == 1) {
int x, y;
FastIO::read(x);
FastIO::read(y);
int u = act[x], v = act[y], w = ++setcnt;
lc[w] = u;
rc[w] = v;
isch[u] = true;
isch[v] = true;
act[y] = w;
act[x] = ++setcnt;
} else if (op == 2) {
int a;
long long l;
FastIO::read(a);
FastIO::read(l);
int d = deep[a], old = act[d], w = ++setcnt;
lc[w] = old;
isch[old] = true;
act[d] = w;
++addcnt;
adddfn[addcnt] = dfn[a];
addval[addcnt] = l;
addnxt[addcnt] = addhd[w];
addhd[w] = addcnt;
} else if (op == 3) {
int b;
FastIO::read(b);
++q3cnt;
qu[q3cnt] = act[deep[b]];
qdfl[q3cnt] = dfn[b];
qdfr[q3cnt] = dfn[b] + siz[b] - 1;
}
}
for (int i = 1; i <= setcnt; i ++) {
if (!isch[i]) {
int top = 0;
mst[++top] = i;
msta[top] = 0;
while (top > 0) {
int u = mst[top];
int state = msta[top]++;
if (state == 0) {
tin[u] = ++mtimer;
rtin[mtimer] = u;
if (lc[u]) {
mst[++top] = lc[u];
msta[top] = 0;
}
} else if (state == 1) {
if (rc[u]) {
mst[++top] = rc[u];
msta[top] = 0;
}
} else {
tout[u] = mtimer;
top--;
}
}
}
}
for (int i = 1; i <= q3cnt; i ++) {
int u = qu[i], t1 = tin[u] - 1, t2 = tout[u];
if (t1 >= 0) {
++evcnt;
evl[evcnt] = qdfl[i];
evr[evcnt] = qdfr[i];
evid[evcnt] = i;
evsign[evcnt] = -1;
evnxt[evcnt] = evhd[t1];
evhd[t1] = evcnt;
}
if (t2 >= 0) {
++evcnt;
evl[evcnt] = qdfl[i];
evr[evcnt] = qdfr[i];
evid[evcnt] = i;
evsign[evcnt] = 1;
evnxt[evcnt] = evhd[t2];
evhd[t2] = evcnt;
}
}
for (int t = 0; t <= mtimer; t ++) {
if (t > 0) {
int u = rtin[t];
for (int i = addhd[u]; i; i = addnxt[i]) {
bitadd(adddfn[i], addval[i]);
}
}
for (int i = evhd[t]; i; i = evnxt[i]) {
long long val = bitquery(evr[i]) - bitquery(evl[i] - 1);
ans[evid[i]] += evsign[i] * val;
}
}
for (int i = 1; i <= q3cnt; i ++) {
FastIO::write(ans[i]);
}
FastIO::flush();
return 0;
}