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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

typedef pair<ll, int> restype; // (-zysk, id)

const int N = 100100;
const ll infll = 1000100100200100100LL;
const restype ini = mp(infll, -1);

/// --------------------------- graf -----------------------------------
int n;
/// krawedzie
vi g[N];
/// wagi krawedzi
vector<ll> vcst[N];
/// wielkosc skladowej, ojciec
int siz[N], par[N];
/// koszt krawedzi do ojca
ll cst[N];
/// zyski w wierzcholkach
ll z[N];
/// najlepszy wybor nie z hld (-zysk, id)
restype best_nhld[N], cur_nhld[N];
set<restype> set_nhld[N];

/// -------------------------- drzewa ----------------------------------

restype operator+ (const restype& le, const ll &ri) {
	return restype(le.fi-ri, le.se);
}
restype operator+ (const ll& le, const restype &ri) {
	return restype(ri.fi-le, ri.se);
}

struct Tree {
	int M;
	vi ids; /// 0 - najwyzszy w drzewie
	vector<ll> Tu, Td; /// zysk na calym przedziale idac w gore / dol (czyli lewo / prawo)
	vector<restype> Tbu, Tbd; /// najlepszy zysk w gore / dol startujacy na koncu przedzialu
	void build(vi v) {
		ids = v;
		M=1;
		while (M < SZ(v)) M *= 2;
		while (SZ(ids)<M) ids.pb(n);
		/*printf("robie drzewo M=%d\nids = ", M);
		FOR(i,SZ(ids)) printf("%d ", ids[i]);
		printf("\n");*/
		Tu.resize(2*M);
		Td.resize(2*M);
		Tbu.resize(2*M);
		Tbd.resize(2*M);
		FOR(i,M) Tbd[i+M] = Tbu[i+M] = ini;
		FOR(i,SZ(v)) {
			Tbu[i+M] = Tbd[i+M] = min(best_nhld[ids[i]]+(-z[ids[i]]), mp(0LL,ids[i]));
			//Tbu[i+M] = Tbd[i+M] = min(best_nhld[ids[i]]+z[ids[i]], mp(-z[ids[i]], ids[i]));
			//printf("Tbu[%d] = %lld (%d)\n", i, -Tbu[i+M].fi, Tbu[i+M].se);
		}
		for (int i = M-1; i >= 1; i--) {
			int m = 2*i+1;
			while (m<M) m*=2;
			m -= M;
			//printf("M=%d, i=%d, m=%d\n", M, i, m);
			Td[i] = Td[2*i] - z[ids[m-1]] - cst[ids[m]] + z[ids[m]] + Td[2*i+1];
			Tu[i] = Tu[2*i] - z[ids[m]] - cst[ids[m]] + z[ids[m-1]] + Tu[2*i+1];
			Tbu[i] = min(Tbu[2*i] + (Tu[i] - Tu[2*i]), Tbu[2*i+1]);
			Tbd[i] = min(Tbd[2*i], (Td[i] - Td[2*i+1]) + Tbd[2*i+1]);
			//printf("Tbu[%d] = %lld (%d)\n", i, -Tbu[i].fi, Tbu[i].se);
		}
	}
	/// najlepszy (-zysk, id) w dol i w gore (razem)
	restype get_best(int pos, bool can_stay=false) {
		/*printf("get best %d\n", pos);
		FORI(i,M-1) printf("%lld ", Tu[i]);
		printf("\n");
		FORI(i,M-1) printf("%lld ", Td[i]);
		printf("\n");
		FORI(i,2*M-1) printf("(%lld %d) ", -Tbu[i].fi, Tbu[i].se);
		printf("\n");
		FORI(i,2*M-1) printf("(%lld %d) ", -Tbd[i].fi, Tbd[i].se);
		printf("\n");*/
		pos += M;
		restype best = can_stay ? Tbu[pos] : ini;
		ll le = 0, ri = 0;
		while (pos > 1) {
			//printf("pos=%d, best= %lld %d; ri=%lld, le=%lld\n", pos, -best.fi, best.se, ri, le);
			if (pos & 1) {
				best = min(best, Tbu[pos-1] + le + (Tu[pos/2] - Tu[pos] - Tu[pos-1]));
				le += Tu[pos/2] - Tu[pos];
			} else {
				best = min(best, Tbd[pos+1] + ri + (Td[pos/2] - Td[pos] - Td[pos+1]));
				ri += Td[pos/2] - Td[pos];
			}
			pos /= 2;
		}
		//printf("najlepszy to %lld (%d)\n", -best.fi, best.se);
		return best;
	}
	/// zysk z dojscia do konca do gory
	ll gain_up(int pos) {
		//printf("od %d do korzenia zyskam ", pos);
		pos += M;
		ll res = 0;
		while (pos > 1) {
			if (pos & 1) res += Tu[pos/2]-Tu[pos];
			pos /= 2;
		}
		//printf("%lld\n", res);
		return res;
	}
	/// zmiana z[id[pos]] lub cst[id[pos]] (fst=true) lub best_nhld[id[pos]] (fst=false);
	void update(int pos, bool fst) {
		//printf("update %d (id=%d)\n", pos, ids[pos]);
		//Tbu[pos+M] = Tbd[pos+M] = min(best_nhld[ids[pos]]+z[ids[pos]], mp(-z[ids[pos]], ids[pos]));
		Tbu[pos+M] = Tbd[pos+M] = min(best_nhld[ids[pos]]+(-z[ids[pos]]), mp(0LL, ids[pos]));
		int le = pos, ri = pos+1, len = 1, mi;
		pos += M;
		//printf("z = ");
		//FOR(i,n) printf("%lld ", z[i]);
		//printf("\n");
		while (pos > 1) {
			if (pos & 1) le -= len;
			else ri += len;
			len *= 2;
			mi = (le + ri) / 2;
			int R = pos | 1, L = R - 1, U = pos / 2;
			//printf("pos = %d; L=%d, R=%d, U=%d; le=%d, ri=%d, mi=%d\n", pos, L, R, U, le, ri, mi);
			Td[U] = Td[L] - z[ids[mi-1]] - cst[ids[mi]] + z[ids[mi]] + Td[R];
			Tu[U] = Tu[L] - z[ids[mi]] - cst[ids[mi]] + z[ids[mi-1]] + Tu[R];
			Tbu[U] = min(Tbu[L] + (Tu[U] - Tu[L]), Tbu[R]);
			Tbd[U] = min(Tbd[L], (Td[U] - Td[R]) + Tbd[R]);
			pos /= 2;
		}
	}
};

Tree tr[N];
/// zawartosc drzew, od gory
vi vtr[N];
/// liczba drzew
int tn;
/// numer drzewa dla wierzcholkow, pozycja w drzewie
int ntr[N], ptr[N];

/// --------------------------- graf -----------------------------------

void dfs(int u, int pa) {
	par[u] = pa;
	siz[u] = 1;
	FOR(i,SZ(g[u])) {
		int v = g[u][i];
		if (v != pa) {
			cst[v] = vcst[u][i];
			dfs(v,u);
			siz[u] += siz[v];
		}
	}
}

void dfs_hld(int u, int hn) {
	ptr[u] = SZ(vtr[hn]);
	ntr[u] = hn;
	vtr[hn].pb(u);
	int who = -1;
	FOR(i,SZ(g[u])) {
		int v = g[u][i];
		if (v==par[u]) continue;
		if (who == -1 || siz[who] < siz[v]) who = v;
	}
	FOR(i,SZ(g[u])) {
		int v = g[u][i];
		if (v == par[u]) continue;
		if (who == v) dfs_hld(v, hn);
		else dfs_hld(v, tn++);
	}
}

/// ------------------------ zapytania ---------------------------------

int find_best(int u) {
	/// (-zysk, id)
	restype best = best_nhld[u] + (-z[u]);
	//printf("start best  = %lld (%d)\n", -best.fi, best.se);
	ll path = 0;
	while (u != -1) {
		restype cand = tr[ntr[u]].get_best(ptr[u]);
		cand.fi -= path;
		if (cand < best) best = cand;
		
		path += tr[ntr[u]].gain_up(ptr[u]);
		u = vtr[ntr[u]][0];
		if (par[u] != -1) {
			path += z[par[u]] - cst[u] - z[u];
			//printf("path up is %lld\n", path);
			best = min(best, mp(-path, par[u]));
			restype cand2 = best_nhld[par[u]];
			if (cand2.se != -1 && cand2 == cur_nhld[u]) {
				set<restype>::iterator it=set_nhld[par[u]].begin();
				it++;
				if (it != set_nhld[par[u]].end()) cand2 = *it;
				else cand2=ini;
			}
			cand2.fi -= path;
			cand2 = cand2 + (-z[par[u]]);
			//printf("candidate 2: %lld (%d)\n", -cand2.fi, cand2.se);
			best = min(best, cand2);
		}
		u = par[u];
	}
	return best.se;
}

void update(int u) { /// uwzglednia zmiane z[v] i cst[v]
	bool fst = true;
	while (u != -1) {
		tr[ntr[u]].update(ptr[u], fst);
		fst = false;
		u = vtr[ntr[u]][0];
		if (par[u] != -1) {
			set_nhld[par[u]].erase(cur_nhld[u]);
			cur_nhld[u] = tr[ntr[u]].get_best(0, true) + (z[u] - cst[u]);
			set_nhld[par[u]].insert(cur_nhld[u]);
			best_nhld[par[u]] = *set_nhld[par[u]].begin();
			//printf("%d ma mozliwosc pojscia do %d zyskujac %lld\n", par[u], best_nhld[par[u]].se, -best_nhld[par[u]].fi);
		}
		u = par[u];
	}
}

int main() {
	int q;
	scanf("%d%d", &n, &q);
	FOR(i,n) scanf("%lld", &z[i]);
	FOR(i,n-1) {
		int a,b;
		ll c;
		scanf("%d%d%lld", &a, &b, &c);
		a--; b--;
		g[a].pb(b);
		g[b].pb(a);
		vcst[a].pb(c);
		vcst[b].pb(c);
	}
	/// budowanie hld
	dfs(0, -1);
	dfs_hld(0, tn++);
	FOR(i,n+1) best_nhld[i] = ini;
	for (int i = tn-1; i >= 0; i--) {
		tr[i].build(vtr[i]);
		int u = par[vtr[i][0]];
		if (u == -1) continue;
		cur_nhld[vtr[i][0]] = tr[i].get_best(0, true) + (z[vtr[i][0]] - cst[vtr[i][0]]);
		set_nhld[u].insert(cur_nhld[vtr[i][0]]);
		best_nhld[u] = *set_nhld[u].begin();
		//printf("%d ma mozliwosc pojscia do %d zyskujac %lld\n", u, best_nhld[u].se, -best_nhld[u].fi);
	}
	int cur = 0; /// miasto w ktorym jestem
	FOR(i,q) {
		int qt;
		scanf("%d", &qt);
		if (qt==1) {
			int v;
			ll d;
			scanf("%d%lld", &v, &d);
			v--;
			//printf("zysk %d to teraz %lld\n", v, d);
			z[v] = d;
			update(v);
		} else {
			int u,v;
			ll d;
			scanf("%d%d%lld", &u, &v, &d);
			u--; v--;
			if (u==par[v]) swap(u,v);
			assert(v==par[u]); /// todo wywalic po przetestowaniu
			//printf("koszt %d->%d to teraz %lld\n", u, v, d);
			cst[u] = d;
			update(u);
		}
		cur = find_best(cur);
		printf("%d ", cur+1); /// todo spacja
	}
	printf("\n");
	return 0;
}