正文
增加 4 行删除 4 行
12 行未改动13141516171819202116 行未改动38394041424344454610 行未改动5758596061626364656667686970717212 行未改动对于第一部分,注意 $h_v$ 函数定义域在自然数上,左半边有限为 $c_v$,而右半边是无限长。所以我们找到 $f_v$ 斜率为 $0$ 的部分 $[L,R]$。那么变换到 $g_v$,就在 $L$ 左边接一个 $c_v$ 长的斜率为 $-1$ 的线段,$R$ 右边的部分推平为斜率为 $1$。上述过程可以这样实现:* 删去 $f_v$ 前 $|son_v|-1$ 大的拐点,此时右边两个拐点即为 $L,R$。这是因为 $f_v$ 的 $|son_v|$ 个儿子每个都会增加一条斜率 $1$ 的射线。* 让 $L,R$ 均增大 $c_v$。于是用可并堆维护即可,$O(n\log n)$。16 行未改动inline int newnode(int x) {++tot; t[tot] = {0, 0, 0, x}; return tot;}inline void psup(int u){if(t[t[u].ls].d < t[t[u].rs].d) swap(t[u].ls, t[u].rs);t[u].d = t[t[u].rs].d + 1;}inline int mer(int x, int y){if(!x || !y) return x + y;if(t[x].x < t[y].x) swap(x, y);10 行未改动int L = 0, R = 0;if(i <= n){while(--sn[i]) rot[i] = del(rot[i]);R = t[rot[i]].x, rot[i] = del(rot[i]);L = t[rot[i]].x, rot[i] = del(rot[i]);}rot[i] = mer(rot[i], newnode(L + c[i]));rot[i] = mer(rot[i], newnode(R + c[i]));rot[p[i]] = mer(rot[p[i]], rot[i]);}while(sn[1]--) rot[1] = del(rot[1]);while(rot[1]) ans -= t[rot[1]].x, rot[1] = del(rot[1]);cout << ans;return 0;}```12 行未改动13141516171819202116 行未改动38394041424344454610 行未改动5758596061626364656667686970717212 行未改动对于第一部分,注意 $h_v$ 函数定义域在自然数上,左半边有限为 $c_v$,而右半边是无限长。所以我们找到 $f_v$ 斜率为 $0$ 的部分 $[L,R]$。那么变换到 $g_v$,就在 $L$ 左边接一个 $c_v$ 长的斜率为 $-1$ 的线段,$R$ 右边的部分推平为斜率为 $1$。上述过程可以这样实现:* 删去 $f_v$ 前 $|son_v|-1$ 大的拐点,此时右边两个拐点即为 $L,R$。这是因为 $f_v$ 的 $|son_v|$ 个儿子每个都会增加一条斜率 $1$ 的射线。* 让 $L,R$ 均增大 $c_v$。于是用可并堆维护即可,$O(n\log n)$。16 行未改动inline int newnode(int x) {++tot; t[tot] = {0, 0, 0, x}; return tot;}inline void psup(int u){if(t[t[u].ls].d < t[t[u].rs].d) swap(t[u].ls, t[u].rs);t[u].d = t[t[u].rs].d + 1;}inline int mer(int x, int y){if(!x || !y) return x + y;if(t[x].x < t[y].x) swap(x, y);10 行未改动int L = 0, R = 0;if(i <= n){while(--sn[i]) rot[i] = del(rot[i]);R = t[rot[i]].x, rot[i] = del(rot[i]);L = t[rot[i]].x, rot[i] = del(rot[i]);}rot[i] = mer(rot[i], newnode(L + c[i]));rot[i] = mer(rot[i], newnode(R + c[i]));rot[p[i]] = mer(rot[p[i]], rot[i]);}while(sn[1]--) rot[1] = del(rot[1]);while(rot[1]) ans -= t[rot[1]].x, rot[1] = del(rot[1]);cout << ans;return 0;}```