CF 671D Roads in Yusland 線段樹維護代價合併的思想
阿新 • • 發佈:2019-01-31
題目大意
給定一顆
解題思路
這題有個特殊的性質,就是每個工人只會修一條指向祖先的路徑。我們設
我們考慮把一些工人的屬性疊加到一個工人上,那麼我們最後查詢時只要查詢某些能維修完整棵樹的最小值。還是用上面的思想,我們做到節點
因為我們要要實現對於一些工人的花費都加上某個值得操作,那麼可以先把工人按
程式
//YxuanwKeith
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 3e5 + 5;
const LL Inf = 1e15 + 7;
struct Tree {
LL Val, add;
} Tr[MAXN * 4 ];
LL F[MAXN];
int N, M, time, Val[MAXN], Ord[MAXN], L[MAXN], R[MAXN];
int tot, Last[MAXN], Out[MAXN], In[MAXN], Next[MAXN * 4], Go[MAXN * 4];
void Link(int u, int v, int *Lst) {
Next[++ tot] = Lst[u], Lst[u] = tot, Go[tot] = v;
}
void Dfs(int Now, int Pre) {
L[Now] = time + 1;
for (int p = In[Now]; p; p = Next[p])
Ord[Go[p]] = ++ time;
for (int p = Last[Now]; p; p = Next[p]) {
int v = Go[p];
if (v == Pre) continue;
Dfs(v, Now);
}
R[Now] = time;
}
void Build(int Now, int l, int r) {
Tr[Now].Val = Inf;
if (l == r) return;
int Mid = (l + r) >> 1;
Build(Now * 2, l, Mid), Build(Now * 2 + 1, Mid + 1, r);
}
void Modify(int Now, int l, int r, int Side, LL Val) {
if (l == r) {
Tr[Now].Val = Val;
return;
}
int Mid = (l + r) >> 1;
if (Side <= Mid) Modify(Now * 2, l, Mid, Side, Val); else
Modify(Now * 2 + 1, Mid + 1, r, Side, Val);
Tr[Now].Val = min(Inf, min(Tr[Now * 2].Val, Tr[Now * 2 + 1].Val) + Tr[Now].add);
}
void Add(int Now, int l, int r, int lx, int rx, LL add) {
if (lx > rx) return;
if (l == lx && r == rx) {
Tr[Now].Val = min(Tr[Now].Val + add, Inf);
Tr[Now].add = min(Tr[Now].add + add, Inf);
return;
}
int Mid = (l + r) >> 1;
if (rx <= Mid) Add(Now * 2, l, Mid, lx, rx, add); else
if (lx > Mid) Add(Now * 2 + 1, Mid + 1, r, lx, rx, add); else {
Add(Now * 2, l, Mid, lx, Mid, add);
Add(Now * 2 + 1, Mid + 1, r, Mid + 1, rx, add);
}
Tr[Now].Val = min(Inf, min(Tr[Now * 2].Val, Tr[Now * 2 + 1].Val) + Tr[Now].add);
}
LL Query(int Now, int l, int r, int lx, int rx) {
if (lx > rx) return Inf;
if (l == lx && r == rx) return Tr[Now].Val;
int Mid = (l + r) >> 1;
if (rx <= Mid) return min(Inf, Query(Now * 2, l, Mid, lx, rx) + Tr[Now].add); else
if (lx > Mid) return min(Inf, Query(Now * 2 + 1, Mid + 1, r, lx, rx) + Tr[Now].add); else
return min(Inf, min(Query(Now * 2, l, Mid, lx, Mid), Query(Now * 2 + 1, Mid + 1, r, Mid + 1, rx)) + Tr[Now].add);
}
void Solve(int Now, int Pre) {
LL Ans = 0;
for (int p = Last[Now]; p; p = Next[p]) {
int v = Go[p];
if (v == Pre) continue;
Solve(v, Now);
Ans = min(Ans + F[v], Inf);
}
if (Now == 1) {
F[1] = Ans;
return;
}
for (int p = In[Now]; p; p = Next[p])
Modify(1, 1, M, Ord[Go[p]], Ans + Val[Go[p]]);
for (int p = Out[Now]; p; p = Next[p])
Modify(1, 1, M, Ord[Go[p]], Inf);
for (int p = Last[Now]; p; p = Next[p])
if (Go[p] != Pre)
Add(1, 1, M, L[Go[p]], R[Go[p]], Ans - F[Go[p]]);
F[Now] = Query(1, 1, M, L[Now], R[Now]);
}
int main() {
scanf("%d %d", &N, &M);
for (int i = 1; i < N; i ++) {
int u, v;
scanf("%d%d", &u, &v);
Link(u, v, Last), Link(v, u, Last);
}
for (int i = 1; i <= M; i ++) {
int l, r, t;
scanf("%d%d%d", &l, &r, &Val[i]);
Link(l, i, In), Link(r, i, Out);
}
Dfs(1, 0);
Build(1, 1, M);
Solve(1, 0);
printf("%I64d", (F[1] == Inf) ? -1 : F[1]);
}