#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // typedef pair < LL, int > PLI; const LL INF = 2e18 + 7; struct SegmentTree { const static int MAX = 1 << 17; vector < PLI > tree; vector < LL > lazy; SegmentTree(vector < PLI > &vec) { tree.resize(2 * MAX, {INF, 0}), lazy.resize(2 * MAX, 0); for(int i = 0; i < sz(vec); i++) tree[i + MAX] = vec[i]; for(int i = MAX - 1; i; i--) tree[i] = min(tree[2 * i], tree[2 * i + 1]); } inline void propagate(int node, int l, int r) { if(!lazy[node]) return; tree[node].first += lazy[node]; if(l != r) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } void update(int a, int b, LL val, int node = 1, int l = 0, int r = MAX - 1) { propagate(node, l, r); if(b < l || r < a) return; if(a <= l && r <= b) lazy[node] += val, propagate(node, l, r); else { update(a, b, val, 2 * node, l, (l + r) / 2); update(a, b, val, 2 * node + 1, (l + r) / 2 + 1, r); tree[node] = min(tree[2 * node], tree[2 * node + 1]); } } PLI query(int a, int b, int node = 1, int l = 0, int r = MAX - 1) { propagate(node, l, r); if(b < l || r < a) return {INF, 0}; if(a <= l && r <= b) return tree[node]; return min(query(a, b, 2 * node, l, (l + r) / 2), query(a, b, 2 * node + 1, (l + r) / 2 + 1, r)); } }; const int N = 1e5 + 7; const int LG = 20; int n, m, l; LL T[N], C[N]; map < PII, int > mp; bool vis[N], bl[N]; int sz[LG][N], pre[LG][N], F[N], c, lv[N], sz2[N]; vector < PLI > vec[LG]; SegmentTree *tree[LG]; vector < PII > G[N]; int Size(int v, int f = 0) { sz2[v] = 1; for(auto u: G[v]) if(u.first != f && !bl[u.first]) sz2[v] += Size(u.first, v); return sz2[v]; } inline void Find(int v, int r, int f = 0) { bool can = 1; for(auto u: G[v]) { if(!bl[u.first] && u.first != f && 2 * sz2[u.first] > r) { can = 0; Find(u.first, r, v); break; } } if(can) c = v; } int cnt[LG]; int dfs(int v, int lvl, LL s = 0, int f = 0) { sz[lvl][v] = 1; pre[lvl][v] = cnt[lvl]++; vec[lvl].push_back({s - T[v], v}); for(auto u: G[v]) if(u.first != f && !bl[u.first]) sz[lvl][v] += dfs(u.first, lvl, s + C[u.second], v); return sz[lvl][v]; } void centroid(int v = 1, int lvl = 0, int f = 0) { Find(v, Size(v)); int cen = c; dfs(cen, lvl); F[cen] = f; bl[cen] = 1; lv[cen] = lvl; for(auto u: G[cen]) if(!bl[u.first]) centroid(u.first, lvl + 1, cen); l = max(l, lvl); } int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%lld", &T[i]); for(int i = 1; i < n; i++) { int a, b; LL cost; scanf("%d%d%lld", &a, &b, &cost); C[i] = cost; mp[{a, b}] = mp[{b, a}] = i; G[a].push_back({b, i}); G[b].push_back({a, i}); } centroid(); for(int i = 0; i <= l; i++) tree[i] = new SegmentTree(vec[i]); int last = 1; while(m--) { int t; scanf("%d", &t); if(t == 1) { int v; LL d; scanf("%d%lld", &v, &d); int cen = v; while(cen > 0) { tree[lv[cen]]->update(pre[lv[cen]][v], pre[lv[cen]][v], T[v] - d); cen = F[cen]; } T[v] = d; } else { int a, b; LL cost; scanf("%d%d%lld", &a, &b, &cost); int i = mp[{a, b}], cen = a; if(lv[a] > lv[b]) cen = b; while(cen > 0) { if(pre[lv[cen]][b] < pre[lv[cen]][a] && pre[lv[cen]][a] < pre[lv[cen]][b] + sz[lv[cen]][b]) swap(a, b); tree[lv[cen]]->update(pre[lv[cen]][b], pre[lv[cen]][b] + sz[lv[cen]][b] - 1, cost - C[i]); cen = F[cen]; } C[i] = cost; } PLI ans = {INF, 0}; int cen = last; while(cen > 0) { LL val1 = tree[lv[cen]]->query(pre[lv[cen]][last], pre[lv[cen]][last]).first; tree[lv[cen]]->update(pre[lv[cen]][last], pre[lv[cen]][last], INF - val1); PLI val = tree[lv[cen]]->query(pre[lv[cen]][cen], pre[lv[cen]][cen] + sz[lv[cen]][cen] - 1); val.first += val1 + T[last]; ans = min(ans, val); tree[lv[cen]]->update(pre[lv[cen]][last], pre[lv[cen]][last], val1 - INF); cen = F[cen]; } last = ans.second; printf("%d ", last); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // typedef pair < LL, int > PLI; const LL INF = 2e18 + 7; struct SegmentTree { const static int MAX = 1 << 17; vector < PLI > tree; vector < LL > lazy; SegmentTree(vector < PLI > &vec) { tree.resize(2 * MAX, {INF, 0}), lazy.resize(2 * MAX, 0); for(int i = 0; i < sz(vec); i++) tree[i + MAX] = vec[i]; for(int i = MAX - 1; i; i--) tree[i] = min(tree[2 * i], tree[2 * i + 1]); } inline void propagate(int node, int l, int r) { if(!lazy[node]) return; tree[node].first += lazy[node]; if(l != r) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } void update(int a, int b, LL val, int node = 1, int l = 0, int r = MAX - 1) { propagate(node, l, r); if(b < l || r < a) return; if(a <= l && r <= b) lazy[node] += val, propagate(node, l, r); else { update(a, b, val, 2 * node, l, (l + r) / 2); update(a, b, val, 2 * node + 1, (l + r) / 2 + 1, r); tree[node] = min(tree[2 * node], tree[2 * node + 1]); } } PLI query(int a, int b, int node = 1, int l = 0, int r = MAX - 1) { propagate(node, l, r); if(b < l || r < a) return {INF, 0}; if(a <= l && r <= b) return tree[node]; return min(query(a, b, 2 * node, l, (l + r) / 2), query(a, b, 2 * node + 1, (l + r) / 2 + 1, r)); } }; const int N = 1e5 + 7; const int LG = 20; int n, m, l; LL T[N], C[N]; map < PII, int > mp; bool vis[N], bl[N]; int sz[LG][N], pre[LG][N], F[N], c, lv[N], sz2[N]; vector < PLI > vec[LG]; SegmentTree *tree[LG]; vector < PII > G[N]; int Size(int v, int f = 0) { sz2[v] = 1; for(auto u: G[v]) if(u.first != f && !bl[u.first]) sz2[v] += Size(u.first, v); return sz2[v]; } inline void Find(int v, int r, int f = 0) { bool can = 1; for(auto u: G[v]) { if(!bl[u.first] && u.first != f && 2 * sz2[u.first] > r) { can = 0; Find(u.first, r, v); break; } } if(can) c = v; } int cnt[LG]; int dfs(int v, int lvl, LL s = 0, int f = 0) { sz[lvl][v] = 1; pre[lvl][v] = cnt[lvl]++; vec[lvl].push_back({s - T[v], v}); for(auto u: G[v]) if(u.first != f && !bl[u.first]) sz[lvl][v] += dfs(u.first, lvl, s + C[u.second], v); return sz[lvl][v]; } void centroid(int v = 1, int lvl = 0, int f = 0) { Find(v, Size(v)); int cen = c; dfs(cen, lvl); F[cen] = f; bl[cen] = 1; lv[cen] = lvl; for(auto u: G[cen]) if(!bl[u.first]) centroid(u.first, lvl + 1, cen); l = max(l, lvl); } int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%lld", &T[i]); for(int i = 1; i < n; i++) { int a, b; LL cost; scanf("%d%d%lld", &a, &b, &cost); C[i] = cost; mp[{a, b}] = mp[{b, a}] = i; G[a].push_back({b, i}); G[b].push_back({a, i}); } centroid(); for(int i = 0; i <= l; i++) tree[i] = new SegmentTree(vec[i]); int last = 1; while(m--) { int t; scanf("%d", &t); if(t == 1) { int v; LL d; scanf("%d%lld", &v, &d); int cen = v; while(cen > 0) { tree[lv[cen]]->update(pre[lv[cen]][v], pre[lv[cen]][v], T[v] - d); cen = F[cen]; } T[v] = d; } else { int a, b; LL cost; scanf("%d%d%lld", &a, &b, &cost); int i = mp[{a, b}], cen = a; if(lv[a] > lv[b]) cen = b; while(cen > 0) { if(pre[lv[cen]][b] < pre[lv[cen]][a] && pre[lv[cen]][a] < pre[lv[cen]][b] + sz[lv[cen]][b]) swap(a, b); tree[lv[cen]]->update(pre[lv[cen]][b], pre[lv[cen]][b] + sz[lv[cen]][b] - 1, cost - C[i]); cen = F[cen]; } C[i] = cost; } PLI ans = {INF, 0}; int cen = last; while(cen > 0) { LL val1 = tree[lv[cen]]->query(pre[lv[cen]][last], pre[lv[cen]][last]).first; tree[lv[cen]]->update(pre[lv[cen]][last], pre[lv[cen]][last], INF - val1); PLI val = tree[lv[cen]]->query(pre[lv[cen]][cen], pre[lv[cen]][cen] + sz[lv[cen]][cen] - 1); val.first += val1 + T[last]; ans = min(ans, val); tree[lv[cen]]->update(pre[lv[cen]][last], pre[lv[cen]][last], val1 - INF); cen = F[cen]; } last = ans.second; printf("%d ", last); } return 0; } |