#include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PLI pair <LL, int> #define PIL pair <int, LL> #define PII pair <int, int> const int N = 1e5 + 7; const LL inf = LL(1e18) + 7LL; int n, q; LL val[N]; int pod[N]; bool block[N]; vector <PIL> G[N]; int K = 0; int cnt = 0; int czas = 0; map <PII, LL> M; vector <pair <int, PII> > C[N]; vector <pair <PLI, LL> > tree[20 * N]; int dfs(int u, int p){ pod[u] = 1; for(auto v: G[u]) if(!block[v.st] && v.st != p) pod[u] += dfs(v.st, u); return pod[u]; } int Find(int u){ dfs(u, u); K = pod[u]; int parent = u; int half = (pod[u] + 1) / 2; bool change = true; while(change){ change = false; for(auto v: G[u]) if(!block[v.st] && v.st != parent && pod[v.st] >= half){ parent = u; u = v.st; change = true; break; } } return u; } void add(int u, int p, LL curD){ C[u].push_back({cnt, {++czas, 0}}); tree[cnt][czas + K] = {{val[u] - curD, -u}, 0}; for(auto v: G[u]) if(!block[v.st] && v.st != p) add(v.st, u, curD + v.nd); C[u].back().nd.nd = czas; } inline int sz(int a){ int ret = 1; while(ret < a) ret <<= 1; return ret - 1; } void Rozbicie(int u){ u = Find(u); block[u] = true; ++cnt; czas = 0; K = sz(K); tree[cnt].resize(2 * K + 2); add(u, u, 0); for(int i = K + czas + 1; i < (int)tree[cnt].size(); ++i) tree[cnt][i] = {{-inf, 0}, 0}; for(int i = K; i >= 1; --i) tree[cnt][i] = max(tree[cnt][i + i], tree[cnt][i + i + 1]); for(auto v: G[u]) if(!block[v.st]) Rozbicie(v.st); } void update(int t, int v){ tree[t][v].st = max(tree[t][v + v].st, tree[t][v + v + 1].st); tree[t][v].st.st += tree[t][v].nd; } void change(int cur, int t, int p, int k, LL value, int from, int to){ if(p <= from && to <= k){ tree[t][cur].nd += value; tree[t][cur].st.st += value; return; } int m = (from + to) / 2; if(p <= m) change(cur + cur, t, p, k, value, from, m); if(m < k) change(cur + cur + 1, t, p, k, value, m + 1, to); update(t, cur); } PLI ask(int cur, int t, int p, int k, LL up, int from, int to){ if(p > k) return {-inf, 0}; if(p <= from && to <= k) return {tree[t][cur].st.st + up, tree[t][cur].st.nd}; PLI ret = {-inf, 0}; int m = (from + to) / 2; if(p <= m) ret = max(ret, ask(cur + cur, t, p, k, up + tree[t][cur].nd, from, m)); if(m < k) ret = max(ret, ask(cur + cur + 1, t, p, k, up + tree[t][cur].nd, m + 1, to)); return ret; } int main(){ scanf("%d %d", &n, &q); for(int i = 1; i <= n; ++i) scanf("%lld", &val[i]); for(int i = 1; i < n; ++i){ int u, v; LL c; scanf("%d %d %lld", &u, &v, &c); G[u].push_back({v, c}); G[v].push_back({u, c}); if(u > v) swap(u, v); M[{u, v}] = c; } Rozbicie(1); /* for(int i = 1; i <= cnt; ++i){ for(int j = 1; j < (int)tree[i].size(); ++j) printf(" :%lld %d: ", tree[i][j].st.st, tree[i][j].st.nd); puts(""); } */ int cur = 1; while(q--){ int t; scanf("%d", &t); if(t == 1){ int u; LL cost; scanf("%d %lld", &u, &cost); for(auto v: C[u]) change(1, v.st, v.nd.st, v.nd.st, cost - val[u], 1, tree[v.st].size() / 2); val[u] = cost; } else{ int u, v; LL cost; scanf("%d %d %lld", &u, &v, &cost); if(u > v) swap(u, v); LL curD = M[{u, v}]; for(int i = 0; i < (int)C[u].size() && i < (int)C[v].size(); ++i){ if(C[u][i].st != C[v][i].st) break; change(1, C[u][i].st, max(C[u][i].nd.st, C[v][i].nd.st), min(C[u][i].nd.nd, C[v][i].nd.nd), curD - cost, 1, tree[C[u][i].st].size() / 2); } M[{u, v}] = cost; } PLI maxi = {-inf, -cur}; for(auto v: C[cur]){ int curS = tree[v.st].size() / 2; PLI maxi2 = max(ask(1, v.st, 1, v.nd.st - 1, 0, 1, curS), ask(1, v.st, v.nd.st + 1, curS, 0, 1, curS)); maxi2.st += ask(1, v.st, v.nd.st, v.nd.st, 0, 1, curS).st - val[cur]; maxi = max(maxi, maxi2); } cur = -maxi.nd; printf("%d ", cur); /* for(int i = 1; i <= cnt; ++i){ for(int j = 1; j < (int)tree[i].size(); ++j) printf(" :%lld %d: ", tree[i][j].st.st, tree[i][j].st.nd); puts(""); } */ } 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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PLI pair <LL, int> #define PIL pair <int, LL> #define PII pair <int, int> const int N = 1e5 + 7; const LL inf = LL(1e18) + 7LL; int n, q; LL val[N]; int pod[N]; bool block[N]; vector <PIL> G[N]; int K = 0; int cnt = 0; int czas = 0; map <PII, LL> M; vector <pair <int, PII> > C[N]; vector <pair <PLI, LL> > tree[20 * N]; int dfs(int u, int p){ pod[u] = 1; for(auto v: G[u]) if(!block[v.st] && v.st != p) pod[u] += dfs(v.st, u); return pod[u]; } int Find(int u){ dfs(u, u); K = pod[u]; int parent = u; int half = (pod[u] + 1) / 2; bool change = true; while(change){ change = false; for(auto v: G[u]) if(!block[v.st] && v.st != parent && pod[v.st] >= half){ parent = u; u = v.st; change = true; break; } } return u; } void add(int u, int p, LL curD){ C[u].push_back({cnt, {++czas, 0}}); tree[cnt][czas + K] = {{val[u] - curD, -u}, 0}; for(auto v: G[u]) if(!block[v.st] && v.st != p) add(v.st, u, curD + v.nd); C[u].back().nd.nd = czas; } inline int sz(int a){ int ret = 1; while(ret < a) ret <<= 1; return ret - 1; } void Rozbicie(int u){ u = Find(u); block[u] = true; ++cnt; czas = 0; K = sz(K); tree[cnt].resize(2 * K + 2); add(u, u, 0); for(int i = K + czas + 1; i < (int)tree[cnt].size(); ++i) tree[cnt][i] = {{-inf, 0}, 0}; for(int i = K; i >= 1; --i) tree[cnt][i] = max(tree[cnt][i + i], tree[cnt][i + i + 1]); for(auto v: G[u]) if(!block[v.st]) Rozbicie(v.st); } void update(int t, int v){ tree[t][v].st = max(tree[t][v + v].st, tree[t][v + v + 1].st); tree[t][v].st.st += tree[t][v].nd; } void change(int cur, int t, int p, int k, LL value, int from, int to){ if(p <= from && to <= k){ tree[t][cur].nd += value; tree[t][cur].st.st += value; return; } int m = (from + to) / 2; if(p <= m) change(cur + cur, t, p, k, value, from, m); if(m < k) change(cur + cur + 1, t, p, k, value, m + 1, to); update(t, cur); } PLI ask(int cur, int t, int p, int k, LL up, int from, int to){ if(p > k) return {-inf, 0}; if(p <= from && to <= k) return {tree[t][cur].st.st + up, tree[t][cur].st.nd}; PLI ret = {-inf, 0}; int m = (from + to) / 2; if(p <= m) ret = max(ret, ask(cur + cur, t, p, k, up + tree[t][cur].nd, from, m)); if(m < k) ret = max(ret, ask(cur + cur + 1, t, p, k, up + tree[t][cur].nd, m + 1, to)); return ret; } int main(){ scanf("%d %d", &n, &q); for(int i = 1; i <= n; ++i) scanf("%lld", &val[i]); for(int i = 1; i < n; ++i){ int u, v; LL c; scanf("%d %d %lld", &u, &v, &c); G[u].push_back({v, c}); G[v].push_back({u, c}); if(u > v) swap(u, v); M[{u, v}] = c; } Rozbicie(1); /* for(int i = 1; i <= cnt; ++i){ for(int j = 1; j < (int)tree[i].size(); ++j) printf(" :%lld %d: ", tree[i][j].st.st, tree[i][j].st.nd); puts(""); } */ int cur = 1; while(q--){ int t; scanf("%d", &t); if(t == 1){ int u; LL cost; scanf("%d %lld", &u, &cost); for(auto v: C[u]) change(1, v.st, v.nd.st, v.nd.st, cost - val[u], 1, tree[v.st].size() / 2); val[u] = cost; } else{ int u, v; LL cost; scanf("%d %d %lld", &u, &v, &cost); if(u > v) swap(u, v); LL curD = M[{u, v}]; for(int i = 0; i < (int)C[u].size() && i < (int)C[v].size(); ++i){ if(C[u][i].st != C[v][i].st) break; change(1, C[u][i].st, max(C[u][i].nd.st, C[v][i].nd.st), min(C[u][i].nd.nd, C[v][i].nd.nd), curD - cost, 1, tree[C[u][i].st].size() / 2); } M[{u, v}] = cost; } PLI maxi = {-inf, -cur}; for(auto v: C[cur]){ int curS = tree[v.st].size() / 2; PLI maxi2 = max(ask(1, v.st, 1, v.nd.st - 1, 0, 1, curS), ask(1, v.st, v.nd.st + 1, curS, 0, 1, curS)); maxi2.st += ask(1, v.st, v.nd.st, v.nd.st, 0, 1, curS).st - val[cur]; maxi = max(maxi, maxi2); } cur = -maxi.nd; printf("%d ", cur); /* for(int i = 1; i <= cnt; ++i){ for(int j = 1; j < (int)tree[i].size(); ++j) printf(" :%lld %d: ", tree[i][j].st.st, tree[i][j].st.nd); puts(""); } */ } return 0; } |