Question:
A vertex-weighted tree with N points.
The n lines below give each point of privilege, indicating the price for each point to buy and sell chicken legs
The n-1 lines below give the tree edge
The following Q operations
Q row
U, V, Val
From u to V, you can buy a chicken leg and sell it later. The output is max (0, the biggest benefit)
Then, give the [U, V] path vertices + = Val
Ideas:
Tree link splitting
Diaosi question: Click to open the link
# Pragma comment (linker, "/Stack: 1024000000,1024000000 ") # include <cstdio> # include <cstring> # include <cmath> # include <queue> # include <iostream> # include <algorithm> using namespace STD; typedef long ll; const ll INF = 1e16; inline void RD (Int & N) {n = 0; char c = getchar (); while (C <'0' | C> '9') C = getchar (); While (C> = '0' & C <= '9 ') N * = 10, N + = (c-'0'), c = getchar ();} int data [30]; void pt64 (L L num) {If (Num <0) {putchar ('-'); num =-num;} int ans [20], Top = 0; while (num! = 0) {ans [top ++] = num % 10; num/= 10;} If (Top = 0) putchar ('0 '); for (INT I = Top-1; I> = 0; I --) {char CH = ans [I] + '0'; putchar (CH );} putchar ('\ n') ;}# define n 50010 struct edge {int to, NEX;} edge [N * 2]; int head [N], edgenum; void add (int u, int v) {edge e = {v, head [u]}; edge [edgenum] = E; head [u] = edgenum ++ ;} void Init () {memset (Head,-1, sizeof head); edgenum = 0;} int Fa [N], son [N], siz [N], dep [N], tree_id; // parent node weight Son subtree node number depth line segment tree number int W [N], FW [N], p [N]; // void DFS (int u, int father, int deep) {Fa [u] = Father; son [u] = 0; dep [u] = deep; siz [u] = 1; for (INT I = head [u]; ~ I; I = edge [I]. NEX) {int v = edge [I]. to; If (V = Father) continue; DFS (v, U, deep + 1); siz [u] + = siz [v]; if (siz [v]> siz [son [u]) Son [u] = V ;}} void have_p (int u, int father) {W [u] = ++ tree_id; FW [tree_id] = u; P [u] = Father; If (son [u]) have_p (son [u], father); else return; For (INT I = head [u]; ~ I; I = edge [I]. NEX) {int v = edge [I]. to; If (V = Fa [u] | V = Son [u]) continue; have_p (V, V) ;}# define lson (X) tree [X]. L # define rson (x) tree [X]. R # define L (x) (x <1) # define R (x) (x <1 | 1) # define lazy (x) tree [X]. lazy # define ans0 (x) tree [X]. ans [0] # define ans1 (x) tree [X]. ans [1] # define min (x) tree [X]. min # define max (x) tree [X]. maxstruct node {int L, R; ll ans [2], Min, Max, lazy;} tree [n <2]; int A [n]; void pus H_down (int id) {If (lazy (ID) {min (L (ID) + = lazy (ID); max (L (ID )) + = lazy (ID); min (R (ID) + = lazy (ID); max (R (ID) + = lazy (ID ); lazy (L (ID) + = lazy (ID); lazy (R (ID) + = lazy (ID); lazy (ID) = 0 ;}} void push_up (int id) {min (ID) = min (L (ID), min (R (ID); max (ID) = max (L (ID), max (R (ID); ans0 (ID) = max (ans0 (L (ID )), ans0 (R (ID); ans0 (ID) = max (ans0 (ID), max (R (ID)-min (L (ID ))); ans0 (ID) = max (ans0 (ID), 0ll); Ans1 (ID) = max (ans1 (L (ID), ans1 (R (ID); ans1 (ID) = max (ans1 (ID ), max (L (ID)-min (R (ID); ans1 (ID) = max (ans1 (ID), 0ll);} void build (INT l, int R, int ID) {lson (ID) = L; rson (ID) = r; lazy (ID) = 0; If (L = r) {ans0 (ID) = ans1 (ID) = 0; min (ID) = max (ID) = (LL) A [FW [l]; return ;} int mid = (L + r)> 1; build (L, mid, L (ID); Build (Mid + 1, R, R (ID )); push_up (ID);} void updata (int l, int R, ll Val, int ID) {If (L = Lson (ID) & rson (ID) = r) {min (ID) + = val; max (ID) + = val; lazy (ID) + = val; return;} push_down (ID); int mid = (lson (ID) + rson (ID)> 1; if (mid <L) updata (L, r, Val, R (ID); else if (r <= mid) updata (L, R, Val, L (ID); else {updata (L, mid, val, L (ID); updata (Mid + 1, R, Val, R (ID);} push_up (ID);} ll query (int l, int R, int Hehe, ll & Minn, ll & Maxx, int ID) {If (L = lson (ID) & rson (ID) = r) {Maxx = max (ID ); Minn = min (ID); If (hehe = 0) return ans0 (ID); else return ans1 (ID);} push_down (ID ); int mid = (lson (ID) + rson (ID)> 1; if (mid <L) return query (L, R, hehe, Minn, Maxx, R (ID); else if (r <= mid) return query (L, R, hehe, Minn, Maxx, L (ID); else {ll maxl = 0, minl = 0, maxr = 0, minr = 0, ans; ans = max (query (L, mid, hehe, minl, maxl, L (ID )), query (Mid + 1, R, hehe, minr, maxr, R (ID); Maxx = max (maxl, maxr); Minn = min (minl, minr); If (hehe = 0) {return max (ANS, maxr-minl);} else {return max (ANS, maxl-minr) ;}}} int N, que; ll Qu (int l, int R) {int F1 = P [L], f2 = P [R]; ll ans = 0, maxl =-INF, minl = inf, maxr =-INF, minr = inf, Tmax = 0, tmin = 0; while (F1! = F2) {If (DEP [F1]> Dep [F2]) {ans = max (ANS, query (W [F1], W [L], 1, tmin, tmax, 1); ans = max (ANS, Tmax-minl); maxl = max (maxl, Tmax); minl = min (minl, tmin ); // printf ("1 (% d, % d) = (% i64d, % i64d) ans: % i64d \ n", l, F1, tmin, Tmax, ANS ); L = Fa [F1]; F1 = P [l];} else {ans = max (ANS, query (W [F2], W [R], 0, tmin, tmax, 1); ans = max (ANS, maxr-tmin); maxr = max (maxr, Tmax); minr = min (minr, tmin ); // printf ("2 (% d, % D) = (% i64d, % i64d) ans: % i64d \ n ", R, F2, tmin, Tmax, ANS); r = Fa [F2]; f2 = P [R] ;}}if (DEP [l] <Dep [R]) {ans = max (ANS, query (W [L], W [R], 0, tmin, Tmax, 1); // printf ("3 (% d, % d) = (% i64d, % i64d) ans: % i64d \ n ", l, R, tmin, Tmax, ANS);} else {ans = max (ANS, query (W [R], W [L], 1, tmin, Tmax, 1); // printf ("4 (% d, % d) = (% i64d, % i64d) ans: % i64d \ n", R, L, tmin, tmax, ANS) ;}ans = max (ANS, Tmax-minl); ans = max (NS, maxr-tmin); ans = max (ANS, maxr-minl); Return ans;} void up (int l, int R, ll Val) {int F1 = P [L], f2 = P [R]; while (F1! = F2) {If (DEP [F1] <Dep [F2]) Swap (F1, F2), swap (L, R); updata (W [F1], W [L], Val, 1); L = Fa [F1]; F1 = P [l];} If (DEP [l]> Dep [R]) swap (L, R); updata (W [L], W [R], Val, 1);} void input () {Init (); rd (N ); for (INT I = 1; I <= N; I ++) RD (A [I]); For (INT I = 1; I <n; I ++) {int U, V; rd (U); rd (V); add (u, v); add (v, u) ;}siz [0] = tree_id = 0; DFS (1, 1, 1); have_p (1, 1); Build (1, n, 1);} int main () {int T, CAS = 1; rd (t); While (t --) {input (); rd (que); int U, V, Val; while (que --) {RD (U ); rd (V); rd (VAL); pt64 (Qu (u, v); up (u, v, (LL) Val) ;}} return 0 ;} /* 9983 2 1 5 4 6 7 21 2 2 32 43 55 65 84 7992 6 23 4 56 1 25 7 38 1 42 3 14 4 16 3 54 7 2 7 6 35 8 3123 4 2 1 5 9 6 10 8 7 11 121 31 23 42 52 65 77 97 106 88 1112 881 12 04 5 010 12 07 12 311 6 24 12 1004 12 07 12 3842 68 35 1 70 25 79 591 22 32 43 55 65 84 7303 1 22 2 42 4 47 58 5 72 5 76 5 78 8 74 3 61 4 44 7 84 3 62 1 68 6 54 5 76 6 44 6 23 5 47 3 13 1 71 1 76 3 23 7 76 2 56 8 53 5 17 1 45 1 83 4 23 7 3ans: 948821414107 */
HDU 5052 yaoge's maximum profit bare tree link generation 2014 ACM/ICPC Asia Regional Shanghai Online