#include <bits/stdc++.h> using namespace std; const int N = 1e5; const int LOGN = 18; int n, q; long long zysk[N + 7]; long long inf = 1.1e18; vector <pair <int, long long>> G0[N + 7]; int par0[N + 7][LOGN]; long long len[N + 7]; int pre0[N + 7]; int pos0[N + 7]; int prem0[N + 7]; int posg = 0; int preg = 0; void dfs0(int v, int p) { par0[v][0] = p; prem0[v] = pre0[v] = ++preg; for(auto u : G0[v]) { if(u.first != p) { len[u.first] = u.second; dfs0(u.first, v); prem0[v] = max(prem0[v], prem0[u.first]); } } pos0[v] = ++posg; } const int M0 = (1 << LOGN); long long D0[2 * M0]; void insert0(int a, int b, long long w) { a += M0; b += M0; D0[a] += w; if(a != b) D0[b] += w; for(; a / 2 != b / 2; a /= 2, b /= 2) { if(a % 2 == 0) D0[a + 1] += w; if(b % 2 == 1) D0[b - 1] += w; } } long long query0(int x) { x += M0; long long res = 0; for(; x > 0; x /= 2) res += D0[x]; return res; } bool czyojciec(int u, int v) { return pre0[u] <= pre0[v] && pos0[u] >= pos0[v]; } int lca(int a, int b) { if(czyojciec(a, b)) return a; if(czyojciec(b, a)) return b; for(int j = LOGN - 1; j >= 0; --j) if(!czyojciec(par0[a][j], b)) a = par0[a][j]; return par0[a][0]; } long long odl(int a, int b) { int c = lca(a, b); return query0(pre0[a]) + query0(pre0[b]) - 2 * query0(pre0[c]); } const int M1 = (1 << 21); long long D1[2 * M1]; long long W1[2 * M1]; int idv[2 * M1]; void insert1(int a, int b, long long w, int v = 1, int p = 0, int k = M1 - 1) { if(a <= p && k <= b) { D1[v] += w; return; } D1[2 * v] += D1[v]; D1[2 * v + 1] += D1[v]; D1[v] = 0; int s = (p + k) / 2; if(a <= s) insert1(a, b, w, 2 * v, p, s); if(s + 1 <= b) insert1(a, b, w, 2 * v + 1, s + 1, k); auto pom = max(make_pair(W1[2 * v] + D1[2 * v], idv[2 * v]), make_pair(W1[2 * v + 1] + D1[2 * v + 1], idv[2 * v + 1])); W1[v] = pom.first; idv[v] = pom.second; } void insert1punkt(int x, long long w) { int v = x + M1; D1[v] += w; v /= 2; while(v > 0) { auto pom = max(make_pair(W1[2 * v] + D1[2 * v], idv[2 * v]), make_pair(W1[2 * v + 1] + D1[2 * v + 1], idv[2 * v + 1])); W1[v] = pom.first; idv[v] = pom.second; v /= 2; } } pair <long long, int> query1(int a, int b, int v = 1, int p = 0, int k = M1 - 1) { if(a <= p && k <= b) { return {D1[v] + W1[v], idv[v]}; } D1[2 * v] += D1[v]; D1[2 * v + 1] += D1[v]; D1[v] = 0; int s = (p + k) / 2; pair <long long, int> res = {-inf, 0}; if(a <= s) res = max(res, query1(a, b, 2 * v, p, s)); if(s + 1 <= b) res = max(res, query1(a, b, 2 * v + 1, s + 1, k)); auto pom = max(make_pair(W1[2 * v] + D1[2 * v], idv[2 * v]), make_pair(W1[2 * v + 1] + D1[2 * v + 1], idv[2 * v + 1])); W1[v] = pom.first; idv[v] = pom.second; return res; } struct rozklad { int kor; int pocz; int kon; int par; rozklad(int kor, int pocz, int kon, int par) : kor(kor), pocz(pocz), kon(kon), par(par) {} }; vector <rozklad> tab[N + 7]; int odw[N + 7]; int roz[N + 7]; int pre1[N + 7]; int prem1[N + 7]; void dfs1(int v, int p) { roz[v] = 1; for(auto u : G0[v]) { if(u.first != p && !odw[u.first]) { dfs1(u.first, v); roz[v] += roz[u.first]; } } } void dfs2(int v, int p, long long od, int ov) { prem1[v] = pre1[v] = ++preg; idv[M1 + preg] = -v; W1[M1 + pre1[v]] = zysk[v] - od; for(auto u : G0[v]) { if(u.first != p && !odw[u.first]) { dfs2(u.first, v, od + u.second, ov); prem1[v] = max(prem1[v], prem1[u.first]); } } tab[v].push_back(rozklad(ov, pre1[v], prem1[v], p)); } int findcentr(int v) { int p = 0; int rv = roz[v]; while(1) { int g = 0; for(auto u : G0[v]) { if(u.first != p && !odw[u.first] && 2 * roz[u.first] > rv) { g = u.first; } } if(g == 0) return v; p = v; v = g; } } void CD(int v) { dfs1(v, 0); v = findcentr(v); odw[v] = 1; dfs2(v, 0, 0, v); for(auto u : G0[v]) { if(!odw[u.first]) CD(u.first); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> q; for(int i = 1; i <= n; ++i) cin >> zysk[i]; for(int i = 2; i <= n; ++i) { int a, b; long long l; cin >> a >> b >> l; G0[a].push_back({b, l}); G0[b].push_back({a, l}); } dfs0(1, 1); for(int i = 2; i <= n; ++i) insert0(pre0[i], prem0[i], len[i]); for(int j = 1; j < LOGN; ++j) for(int i = 1; i <= n; ++i) par0[i][j] = par0[par0[i][j - 1]][j - 1]; for(int i = 0; i < 2 * M1; ++i) W1[i] = -inf; preg = posg = 0; CD(1); for(int i = M1 - 1; i > 0; --i) { pair <long long, int> pom = max(make_pair(W1[2 * i], idv[2 * i]), make_pair(W1[2 * i + 1], idv[2 * i + 1])); W1[i] = pom.first; idv[i] = pom.second; } int vp = 1; while(q--) { int ti; cin >> ti; if(ti == 1) { int v; long long nz; cin >> v >> nz; for(auto u : tab[v]) { insert1punkt(u.pocz, nz - zysk[v]); } zysk[v] = nz; } else { int a, b; long long l; cin >> a >> b >> l; if(a != par0[b][0]) swap(a, b); insert0(pre0[b], prem0[b], l - len[b]); for(auto u : tab[b]) { if(a == u.par) insert1(u.pocz, u.kon, -l + len[b]); } for(auto u : tab[a]) { if(b == u.par) insert1(u.pocz, u.kon, -l + len[b]); } len[b] = l; } int g = 0; long long nz = -inf; for(auto u : tab[vp]) { insert1punkt(u.pocz, -inf); int kor = u.kor; auto re = query1(tab[kor].back().pocz, tab[kor].back().kon); long long zz = re.first - odl(vp, kor); if(zz > nz) { nz = zz; g = -re.second; } else if(zz == nz && -re.second < g) { g = -re.second; } insert1punkt(u.pocz, inf); } vp = g; cout << vp << ' '; } cout << endl; 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | #include <bits/stdc++.h> using namespace std; const int N = 1e5; const int LOGN = 18; int n, q; long long zysk[N + 7]; long long inf = 1.1e18; vector <pair <int, long long>> G0[N + 7]; int par0[N + 7][LOGN]; long long len[N + 7]; int pre0[N + 7]; int pos0[N + 7]; int prem0[N + 7]; int posg = 0; int preg = 0; void dfs0(int v, int p) { par0[v][0] = p; prem0[v] = pre0[v] = ++preg; for(auto u : G0[v]) { if(u.first != p) { len[u.first] = u.second; dfs0(u.first, v); prem0[v] = max(prem0[v], prem0[u.first]); } } pos0[v] = ++posg; } const int M0 = (1 << LOGN); long long D0[2 * M0]; void insert0(int a, int b, long long w) { a += M0; b += M0; D0[a] += w; if(a != b) D0[b] += w; for(; a / 2 != b / 2; a /= 2, b /= 2) { if(a % 2 == 0) D0[a + 1] += w; if(b % 2 == 1) D0[b - 1] += w; } } long long query0(int x) { x += M0; long long res = 0; for(; x > 0; x /= 2) res += D0[x]; return res; } bool czyojciec(int u, int v) { return pre0[u] <= pre0[v] && pos0[u] >= pos0[v]; } int lca(int a, int b) { if(czyojciec(a, b)) return a; if(czyojciec(b, a)) return b; for(int j = LOGN - 1; j >= 0; --j) if(!czyojciec(par0[a][j], b)) a = par0[a][j]; return par0[a][0]; } long long odl(int a, int b) { int c = lca(a, b); return query0(pre0[a]) + query0(pre0[b]) - 2 * query0(pre0[c]); } const int M1 = (1 << 21); long long D1[2 * M1]; long long W1[2 * M1]; int idv[2 * M1]; void insert1(int a, int b, long long w, int v = 1, int p = 0, int k = M1 - 1) { if(a <= p && k <= b) { D1[v] += w; return; } D1[2 * v] += D1[v]; D1[2 * v + 1] += D1[v]; D1[v] = 0; int s = (p + k) / 2; if(a <= s) insert1(a, b, w, 2 * v, p, s); if(s + 1 <= b) insert1(a, b, w, 2 * v + 1, s + 1, k); auto pom = max(make_pair(W1[2 * v] + D1[2 * v], idv[2 * v]), make_pair(W1[2 * v + 1] + D1[2 * v + 1], idv[2 * v + 1])); W1[v] = pom.first; idv[v] = pom.second; } void insert1punkt(int x, long long w) { int v = x + M1; D1[v] += w; v /= 2; while(v > 0) { auto pom = max(make_pair(W1[2 * v] + D1[2 * v], idv[2 * v]), make_pair(W1[2 * v + 1] + D1[2 * v + 1], idv[2 * v + 1])); W1[v] = pom.first; idv[v] = pom.second; v /= 2; } } pair <long long, int> query1(int a, int b, int v = 1, int p = 0, int k = M1 - 1) { if(a <= p && k <= b) { return {D1[v] + W1[v], idv[v]}; } D1[2 * v] += D1[v]; D1[2 * v + 1] += D1[v]; D1[v] = 0; int s = (p + k) / 2; pair <long long, int> res = {-inf, 0}; if(a <= s) res = max(res, query1(a, b, 2 * v, p, s)); if(s + 1 <= b) res = max(res, query1(a, b, 2 * v + 1, s + 1, k)); auto pom = max(make_pair(W1[2 * v] + D1[2 * v], idv[2 * v]), make_pair(W1[2 * v + 1] + D1[2 * v + 1], idv[2 * v + 1])); W1[v] = pom.first; idv[v] = pom.second; return res; } struct rozklad { int kor; int pocz; int kon; int par; rozklad(int kor, int pocz, int kon, int par) : kor(kor), pocz(pocz), kon(kon), par(par) {} }; vector <rozklad> tab[N + 7]; int odw[N + 7]; int roz[N + 7]; int pre1[N + 7]; int prem1[N + 7]; void dfs1(int v, int p) { roz[v] = 1; for(auto u : G0[v]) { if(u.first != p && !odw[u.first]) { dfs1(u.first, v); roz[v] += roz[u.first]; } } } void dfs2(int v, int p, long long od, int ov) { prem1[v] = pre1[v] = ++preg; idv[M1 + preg] = -v; W1[M1 + pre1[v]] = zysk[v] - od; for(auto u : G0[v]) { if(u.first != p && !odw[u.first]) { dfs2(u.first, v, od + u.second, ov); prem1[v] = max(prem1[v], prem1[u.first]); } } tab[v].push_back(rozklad(ov, pre1[v], prem1[v], p)); } int findcentr(int v) { int p = 0; int rv = roz[v]; while(1) { int g = 0; for(auto u : G0[v]) { if(u.first != p && !odw[u.first] && 2 * roz[u.first] > rv) { g = u.first; } } if(g == 0) return v; p = v; v = g; } } void CD(int v) { dfs1(v, 0); v = findcentr(v); odw[v] = 1; dfs2(v, 0, 0, v); for(auto u : G0[v]) { if(!odw[u.first]) CD(u.first); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> q; for(int i = 1; i <= n; ++i) cin >> zysk[i]; for(int i = 2; i <= n; ++i) { int a, b; long long l; cin >> a >> b >> l; G0[a].push_back({b, l}); G0[b].push_back({a, l}); } dfs0(1, 1); for(int i = 2; i <= n; ++i) insert0(pre0[i], prem0[i], len[i]); for(int j = 1; j < LOGN; ++j) for(int i = 1; i <= n; ++i) par0[i][j] = par0[par0[i][j - 1]][j - 1]; for(int i = 0; i < 2 * M1; ++i) W1[i] = -inf; preg = posg = 0; CD(1); for(int i = M1 - 1; i > 0; --i) { pair <long long, int> pom = max(make_pair(W1[2 * i], idv[2 * i]), make_pair(W1[2 * i + 1], idv[2 * i + 1])); W1[i] = pom.first; idv[i] = pom.second; } int vp = 1; while(q--) { int ti; cin >> ti; if(ti == 1) { int v; long long nz; cin >> v >> nz; for(auto u : tab[v]) { insert1punkt(u.pocz, nz - zysk[v]); } zysk[v] = nz; } else { int a, b; long long l; cin >> a >> b >> l; if(a != par0[b][0]) swap(a, b); insert0(pre0[b], prem0[b], l - len[b]); for(auto u : tab[b]) { if(a == u.par) insert1(u.pocz, u.kon, -l + len[b]); } for(auto u : tab[a]) { if(b == u.par) insert1(u.pocz, u.kon, -l + len[b]); } len[b] = l; } int g = 0; long long nz = -inf; for(auto u : tab[vp]) { insert1punkt(u.pocz, -inf); int kor = u.kor; auto re = query1(tab[kor].back().pocz, tab[kor].back().kon); long long zz = re.first - odl(vp, kor); if(zz > nz) { nz = zz; g = -re.second; } else if(zz == nz && -re.second < g) { g = -re.second; } insert1punkt(u.pocz, inf); } vp = g; cout << vp << ' '; } cout << endl; return 0; } |