#include <bits/stdc++.h> using namespace std; #define MAX_N 100010 #define MAX_Q 100010 int N, Q; struct E { int v; long long c; int s; bool operator< (E const &rhs) const { return s < rhs.s; } }; struct compDist { bool operator() (const pair<int, long long>& lhs, const pair<int, long long>& rhs) const { if (lhs.second == rhs.second) { return lhs.first < rhs.first; } else { return lhs.second > rhs.second; } } }; struct V { int i, m, s; long long z; vector<E> e; priority_queue<E> ts; vector<pair<int, long long>> vws; set<pair<int, long long>, compDist> dists; }; vector<V> vs; void css() { queue<int> qv; for (int i = 0; i < N; ++i) { if (vs[i].m == 1) { qv.push(i); } } while (!qv.empty()) { int vi = qv.front(); qv.pop(); V& v = vs[vi]; if (v.m == 1) { v.m = 0; for (const E &e : v.e) { if (vs[e.v].m != 0) { v.ts.push({e.v, e.c, N - v.s - 1}); vs[e.v].ts.push({vi, e.c, v.s + 1}); vs[e.v].s += v.s + 1; vs[e.v].m--; if (vs[e.v].m == 1) { qv.push(e.v); } break; } } } } } void fss(int vwi, int tsv, int wr, long long tsc) { stack<pair<int, long long>> sv; vs[vwi].m = 1; sv.push(make_pair(tsv, tsc)); while (!sv.empty()) { pair<int, long long> sve = sv.top(); V& v = vs[sve.first]; if (v.m == 0) { for (const E &e : v.e) { if (vs[e.v].m != 1) { sv.push(make_pair(e.v, sve.second + e.c)); } } v.m = 1; } else { sv.pop(); v.vws.push_back(make_pair(vwi, sve.second)); E tt = v.ts.top(); v.ts.pop(); if (tt.s > wr) { tt.s -= wr; v.ts.push(tt); } v.m = 0; } } } void fwc(int vr, int w) { if (vs[vr].ts.empty()) { return; } int vwi = vr; while (vs[vwi].ts.top().s > w) { vwi = vs[vwi].ts.top().v; } V& vw = vs[vwi]; vw.vws.push_back(make_pair(vwi, 0LL)); while (!vw.ts.empty()) { E ts = vw.ts.top(); vw.ts.pop(); fss(vwi, ts.v, vs[ts.v].ts.top().s, ts.c); fwc(ts.v, ts.s / 2); } } void bdai() { for (int vi = 0; vi < N; ++vi) { for (const pair<int, long long> vwe : vs[vi].vws) { vs[vwe.first].dists.insert(make_pair(vi, vs[vi].z - vwe.second)); } } } void fc(int vi, long long z) { for (const pair<int, long long> vwe : vs[vi].vws) { vs[vwe.first].dists.erase(make_pair(vi, vs[vi].z - vwe.second)); vs[vwe.first].dists.insert(make_pair(vi, z - vwe.second)); } vs[vi].z = z; } void fr(int v1, int v2, long long c) { stack<pair<int, long long>> sv; int vw1, vw2, vwr; if (vs[v2].dists.empty() || (!vs[v1].dists.empty() && (vs[v1].vws.size() < vs[v2].vws.size()))) { vw1 = v1; vw2 = v2; vwr = vs[v1].vws.size(); } else { vw1 = v2; vw2 = v1; vwr = vs[v2].vws.size(); } vs[vw1].m = 1; sv.push(make_pair(vw2, c)); while (!sv.empty()) { pair<int, long long> sve = sv.top(); V& v = vs[sve.first]; if (v.m == 0) { for (const E &e : v.e) { if (vs[e.v].m != 1) { sv.push(make_pair(e.v, sve.second + e.c)); } } v.m = 1; } else { sv.pop(); for (int i = 0; i < vwr; ++i) { vs[v.vws[i].first].dists.erase(make_pair(sve.first, v.z - v.vws[i].second)); v.vws[i] = make_pair(v.vws[i].first, sve.second); vs[v.vws[i].first].dists.insert(make_pair(sve.first, v.z - v.vws[i].second)); } v.m = 0; } } } int fmi(int vi) { int nc; long long mi = LLONG_MIN; for (const pair<int, long long> vwe : vs[vi].vws) { auto bc = begin(vs[vwe.first].dists); if (bc->first == vi) { bc = next(bc); } if (bc->second - vwe.second > mi) { nc = bc->first; mi = bc->second - vwe.second; } } return nc; } int main() { int a, b, p; long long c; p = scanf("%d %d", &N, &Q); vs.resize(N); for (int i = 0; i < N; ++i) { vs[i].i = i + 1; p = scanf("%lld", &vs[i].z); } for (int i = 0; i < N - 1; ++i) { p = scanf ("%d %d %lld", &a, &b, &c); --a; --b; vs[a].e.push_back({b, c, 0}); vs[a].m++; vs[b].e.push_back({a, c, 0}); vs[b].m++; } css(); fwc(0, N / 2); bdai(); int nc = 0; for (int i = 0; i < Q; ++i) { p = scanf("%d", &a); if (a == 1) { p = scanf("%d %lld", &a, &c); fc(a - 1, c); } else if (a == 2) { p = scanf("%d %d %lld", &a, &b, &c); fr(a - 1, b - 1, c); } nc = fmi(nc); printf("%d ", vs[nc].i); } printf("\n"); 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 | #include <bits/stdc++.h> using namespace std; #define MAX_N 100010 #define MAX_Q 100010 int N, Q; struct E { int v; long long c; int s; bool operator< (E const &rhs) const { return s < rhs.s; } }; struct compDist { bool operator() (const pair<int, long long>& lhs, const pair<int, long long>& rhs) const { if (lhs.second == rhs.second) { return lhs.first < rhs.first; } else { return lhs.second > rhs.second; } } }; struct V { int i, m, s; long long z; vector<E> e; priority_queue<E> ts; vector<pair<int, long long>> vws; set<pair<int, long long>, compDist> dists; }; vector<V> vs; void css() { queue<int> qv; for (int i = 0; i < N; ++i) { if (vs[i].m == 1) { qv.push(i); } } while (!qv.empty()) { int vi = qv.front(); qv.pop(); V& v = vs[vi]; if (v.m == 1) { v.m = 0; for (const E &e : v.e) { if (vs[e.v].m != 0) { v.ts.push({e.v, e.c, N - v.s - 1}); vs[e.v].ts.push({vi, e.c, v.s + 1}); vs[e.v].s += v.s + 1; vs[e.v].m--; if (vs[e.v].m == 1) { qv.push(e.v); } break; } } } } } void fss(int vwi, int tsv, int wr, long long tsc) { stack<pair<int, long long>> sv; vs[vwi].m = 1; sv.push(make_pair(tsv, tsc)); while (!sv.empty()) { pair<int, long long> sve = sv.top(); V& v = vs[sve.first]; if (v.m == 0) { for (const E &e : v.e) { if (vs[e.v].m != 1) { sv.push(make_pair(e.v, sve.second + e.c)); } } v.m = 1; } else { sv.pop(); v.vws.push_back(make_pair(vwi, sve.second)); E tt = v.ts.top(); v.ts.pop(); if (tt.s > wr) { tt.s -= wr; v.ts.push(tt); } v.m = 0; } } } void fwc(int vr, int w) { if (vs[vr].ts.empty()) { return; } int vwi = vr; while (vs[vwi].ts.top().s > w) { vwi = vs[vwi].ts.top().v; } V& vw = vs[vwi]; vw.vws.push_back(make_pair(vwi, 0LL)); while (!vw.ts.empty()) { E ts = vw.ts.top(); vw.ts.pop(); fss(vwi, ts.v, vs[ts.v].ts.top().s, ts.c); fwc(ts.v, ts.s / 2); } } void bdai() { for (int vi = 0; vi < N; ++vi) { for (const pair<int, long long> vwe : vs[vi].vws) { vs[vwe.first].dists.insert(make_pair(vi, vs[vi].z - vwe.second)); } } } void fc(int vi, long long z) { for (const pair<int, long long> vwe : vs[vi].vws) { vs[vwe.first].dists.erase(make_pair(vi, vs[vi].z - vwe.second)); vs[vwe.first].dists.insert(make_pair(vi, z - vwe.second)); } vs[vi].z = z; } void fr(int v1, int v2, long long c) { stack<pair<int, long long>> sv; int vw1, vw2, vwr; if (vs[v2].dists.empty() || (!vs[v1].dists.empty() && (vs[v1].vws.size() < vs[v2].vws.size()))) { vw1 = v1; vw2 = v2; vwr = vs[v1].vws.size(); } else { vw1 = v2; vw2 = v1; vwr = vs[v2].vws.size(); } vs[vw1].m = 1; sv.push(make_pair(vw2, c)); while (!sv.empty()) { pair<int, long long> sve = sv.top(); V& v = vs[sve.first]; if (v.m == 0) { for (const E &e : v.e) { if (vs[e.v].m != 1) { sv.push(make_pair(e.v, sve.second + e.c)); } } v.m = 1; } else { sv.pop(); for (int i = 0; i < vwr; ++i) { vs[v.vws[i].first].dists.erase(make_pair(sve.first, v.z - v.vws[i].second)); v.vws[i] = make_pair(v.vws[i].first, sve.second); vs[v.vws[i].first].dists.insert(make_pair(sve.first, v.z - v.vws[i].second)); } v.m = 0; } } } int fmi(int vi) { int nc; long long mi = LLONG_MIN; for (const pair<int, long long> vwe : vs[vi].vws) { auto bc = begin(vs[vwe.first].dists); if (bc->first == vi) { bc = next(bc); } if (bc->second - vwe.second > mi) { nc = bc->first; mi = bc->second - vwe.second; } } return nc; } int main() { int a, b, p; long long c; p = scanf("%d %d", &N, &Q); vs.resize(N); for (int i = 0; i < N; ++i) { vs[i].i = i + 1; p = scanf("%lld", &vs[i].z); } for (int i = 0; i < N - 1; ++i) { p = scanf ("%d %d %lld", &a, &b, &c); --a; --b; vs[a].e.push_back({b, c, 0}); vs[a].m++; vs[b].e.push_back({a, c, 0}); vs[b].m++; } css(); fwc(0, N / 2); bdai(); int nc = 0; for (int i = 0; i < Q; ++i) { p = scanf("%d", &a); if (a == 1) { p = scanf("%d %lld", &a, &c); fc(a - 1, c); } else if (a == 2) { p = scanf("%d %d %lld", &a, &b, &c); fr(a - 1, b - 1, c); } nc = fmi(nc); printf("%d ", vs[nc].i); } printf("\n"); return 0; } |