http://www.lydsy.com/JudgeOnline/problem.php?id=3672
Http://uoj.ac/problem/7
The situation on the chain can be optimized with a slope DP. When the tree is optimized for DP with slope, the complexity of the monotone queue is $o (n) $, so the complexity of the "persistent monotonic queue" on the tree is $o (n^2) $, so it cannot be optimized on the tree slope.
This problem can be divided into tree chain (time complexity $o (nlog^3n) $) or point sub-rule set CDQ (time complexity $o (NLOG^2N) $). Because the tree chain split feeling more difficult to write, and each node with a vector storage queue, appear to compare card space, and time complexity of a log, so write a point division.
For a point $i$, the path from I to the root has $j$, $k $. Assuming that the depth of the $k$ is smaller than the $j$, and $k$ to update the $i$ is better than $j$, the formula ($dis $ for the distance to the root):
$$\frac{f_j-f_k}{dis_j-dis_k}>p_i$$
For each point, the $dis$ as the horizontal axis, $f $ as the ordinate, the most advantage must be on the next convex hull.
For a tree, to find out the center of gravity, and then the center of gravity above the sub-tree division, divided after the center of gravity to the root of the tree all the points of the $f$ value are good, and then use the center of gravity to the root of all the points on the chain to update all points in the center of gravity subtree.
First of all the points in the center of gravity subtree (not including the center of gravity) by "$l $ value-to the distance of the center of gravity" sort, to ensure that the "sub-tree points" of the "center of gravity to the point" to the center of the distance monotonically increment, so that a monotonous stack to maintain a lower convex shell to update.
Time complexity $o (NLOG^2N) $.
#include <queue> #include <cstdio> #include <cstring> #include <algorithm>using namespace std; typedef long LONG ll;const int N = 200003;int in () {int k = 0, fh = 1; char c = GetChar (); c < ' 0 ' | | c > ' 9 '; c = GetChar ()) if (c = = '-') FH = -1;for (; c >= ' 0 ' && C <= ' 9 '; c = GetChar ()) K = (k << 3) + (k <&L T 1) + C-' 0 '; return k * FH; ll INLL () {ll k = 0; int fh = 1; char c = GetChar (); for (; c < ' 0 ' | | c > ' 9 '; c = GetChar ()) if (c = = '-') FH = -1;fo R (; c >= ' 0 ' && C <= ' 9 '; c = GetChar ()) K = (k << 3) + (k << 1) + C-' 0 '; return k * FH; BOOL vis[n];struct Node {int NXT, to; ll w;node (int _nxt = 0, Int. _to = 0, ll _w = 0): NXT (_NXT), to (_to), W (_w) {}} e[n << 1];int t, N, Fa[n], q[n], cnt = 0, sz[n], Point[n];ll f[n], fadis[n], p[n], q[n], l[n], longdis[n];void ins (int u , int V, ll W) {e[++cnt] = node (Point[u], V, W); Point[u] = cnt;} int FINDRT (int x) {int u, head = 0, tail = 1; Q[1] = X;while (head!)= tail) {u = Q[++head]; Sz[u] = 1;for (int i = point[u]; i; i = e[i].nxt) if (!vis[e[i].to] && e[i].to! = Fa[u]) q[++ Tail] = e[i].to;} for (int i = tail; I >= 1; i) {if ((Sz[q[i] << 1) > Tail) return Q[i];sz[fa[q[i]]] + = Sz[q[i] [}} int tot, qu[n];struct data {ll dis, line; int Id;data (ll _dis = 0, ll _line = 0, int. _id = 0): Dis (_dis), line (_line), ID (_id) {}bool operator < (const data &a) Const {return line < A.line;}} A[n];d ouble k_num (int x, int y) {return 1.0 * (F[x]-f[y])/(Longdis[x]-longdis[y]);} int find (int le, double k) {int left = 0, right = le-1, Mid;while (left < right) {mid = (left + right) >> 1;if (K_num (Qu[mid], Qu[mid + 1]) > K) left = mid + 1;else right = mid;} if (left = = le-1 && k_num (Qu[left], Qu[le]) > K) return Qu[le];return Qu[left];} int cont = 0;void CDQ (int x) {Vis[x] = True;int I, tmp, tail, head = 0, up = x; ll len = 0;for (tmp = point[x]; tmp; TMP = E[TMP].NXT) if (!vis[e[tmp].to] && e[tmp].to = = Fa[x]) {while (!vis[fa[up]] && up! = 1) up = FA[UP];CDQ (FINDRT (UP)); tot = 0;for (i = point[x]; i; i = e[i].nxt) if (!vis[e[i].to] && e[i].to! = fa[x]) A[++tot] = data (fadis[e[i].to], l[ E[I].TO]-fadis[e[i].to], e[i].to), while (head! = tot) {++head;for (i = point[a[head].id]; i; i = e[i].nxt) if (!vis[e[i].t O] && e[i].to! = Fa[a[head].id]) A[++tot] = data (fadis[e[i].to] + A[head].dis, l[e[i].to]-fadis[e[i].to]-A[head ].dis, e[i].to);} Stable_sort (A + 1, a + tot + 1), TMP = X;while (tmp! = up) {tmp = fa[tmp], if (longdis[x]-longdis[tmp] > l[x]) break;i F (F[x] = 1) f[x] = f[tmp] + (Longdis[x]-longdis[tmp]) * P[x] + q[x];else f[x] = min (f[x], f[tmp] + (Longdis[x]-long DIS[TMP]) * P[x] + q[x]);} tail = 0; TMP = x; Qu[0] = x;for (i = 1; I <= tot; ++i) {if (A[i].line < 0) Continue;while (tmp! = up && len + fadis[tmp] <= A[i].line) {len + fadis[tmp];tmp = fa[tmp];while (tail && k_num (tmp, Qu[tail]) > K_num (Qu[tail], Qu[tail-1]))--tail;qu[++tail] = tmp;} Head = Find (tail, (double) p[a[i].id]), if (f[a[i].id] = = 1) f[a[i].id] = F[head] + (Longdis[a[i].id]-longdis[head]) * p [A[i].id] + q[a[i].id];else f[a[i].id] = min (F[a[i].id], F[head] + (Longdis[a[i].id]-longdis[head]) * P[a[i].id] + q[a[i ].id]);} for (i = point[x]; i; i = e[i].nxt) if (!vis[e[i].to] && e[i].to! = fa[x]) CDQ (FINDRT (e[i].to));} int main () {n = in (); t = in (); for (int i = 2; I <= n; ++i) {Fa[i] = in (); Fadis[i] = INLL (); Ins (Fa[i], I, fadis[i]); ins (I, Fa[i], fadis[i]);p [i] = INLL (); Q[i] = INLL (); L[i] = INLL ();} Memset (F,-1, sizeof (LL) * (n + 1)); f[1] = 0;int u, head = 0, tail = 1; Q[1] = 1; LONGDIS[1] = 0;while (head! = tail) {u = q[++head];for (int i = point[u]; i; i = e[i].nxt) if (e[i].to! = Fa[u]) {longdis[e[ I].to] = Longdis[u] + fadis[e[i].to]; Q[++tail] = e[i].to;}} CDQ (FINDRT (1)); for (int i = 2; I <= n; ++i) printf ("%lld\n", F[i]); return 0;}
Finally finished, after writing a lot of wrong qaq
"Bzoj 3672" "Uoj #7" "NOI 2014" ticket purchase