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
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

typedef long long ll;
const ll INF = 1e18L + 1e15L;

struct Node {
	pair<ll, int> small;
	ll spusc;
	void merge(const Node & a, const Node & b) {
		assert(spusc == 0);
		small = min(a.small, b.small);
	}
};

//~ #warning small nax i pot

const int pot = 128 * 1024;
struct T {
	Node tr[2*pot];
	void spusc(int i) {
		assert(i < pot);
		for(int j : {2 * i, 2 * i + 1}) {
			tr[j].small.first += tr[i].spusc;
			tr[j].spusc += tr[i].spusc;
		}
		tr[i].spusc = 0;
	}
	void merge(int i) {
		assert(i < pot);
		tr[i].merge(tr[2*i], tr[2*i+1]);
	}
	pair<ll, int> rec(int id, const int & q_low, const int & q_high, int low, int high, ll diff) {
		if(q_high < low || high < q_low) return make_pair(INF, -1);;
		if(q_low <= low && high <= q_high) {
			tr[id].small.first += diff;
			tr[id].spusc += diff;
			return tr[id].small;
		}
		spusc(id);
		int last_left = (low + high) / 2;
		pair<ll, int> r = rec(2 * id, q_low, q_high, low, last_left, diff);
		r = min(r, rec(2 * id + 1, q_low, q_high, last_left + 1, high, diff));
		merge(id);
		return r;
	}
	void add(int low, int high, ll diff) {
		rec(1, low, high, 0, pot - 1, diff);
	}
	pair<ll, int> getMin(int low, int high) {
		return rec(1, low, high, 0, pot - 1, 0);
	}
	ll get(int i) {
		return getMin(i, i).first;
	}
	void init() {
		for(int i = pot - 1; i >= 1; --i)
			tr[i].merge(tr[2*i], tr[2*i+1]);
	}
} tr[20];

const int nax = 1e5 + 5;
ll own[nax];
vector<pair<int, ll>> w[nax];
bool forbidden[nax];
int subtree[nax];
//~ vector<vector<int>> 

// find subtree sizes
void dfs_one(int a, int par) {
	subtree[a] = 1;
	for(const pair<int, ll> & p : w[a]) {
		int b = p.first;
		if(forbidden[b] || b == par) continue;
		dfs_one(b, a);
		subtree[a] += subtree[b];
	}
}

int TAJM[20];
pair<int,int> preorder[20][nax];
int reprezentant[20][nax], szef[20][nax];
void dfs_two(int a, int group, int root, int par, ll so_far, int level) {
	tr[level].tr[pot+TAJM[level]].small = make_pair(so_far - own[a], a);
	reprezentant[level][a] = group;
	szef[level][a] = root;
	preorder[level][a].first = TAJM[level]++;
	for(const pair<int, ll> & p : w[a]) {
		int b = p.first;
		if(forbidden[b] || b == par) continue;
		dfs_two(b, group == -1 ? b : group, root, a, so_far + p.second, level);
	}
	preorder[level][a].second = TAJM[level] - 1;
}

void buduj(int a, int level) {
	// a jest centroidem
	debug() << "buduj" imie(a) imie(level);
	dfs_two(a, -1, a, -1, 0LL, level);
}

void centroiduj(int a, int level) {
	assert(level < 20);
	dfs_one(a, -1);
	const int nnn = subtree[a];
	while(true) {
		bool anything = false;
		for(const pair<int,ll> & p : w[a]) {
			int b = p.first;
			if(forbidden[b]) continue;
			if(subtree[b] < subtree[a] && 2 * subtree[b] >= nnn) {
				a = b;
				anything = true;
				break;
			}
		}
		if(!anything) break;
	}
	
	buduj(a, level);
	// 'a' jest centroidem
	assert(!forbidden[a]);
	forbidden[a] = true;
	for(const pair<int,ll> & p : w[a]) {
		int b = p.first;
		if(!forbidden[b])
			centroiduj(b, level + 1);
	}
}

void solve(int a, int level, pair<ll, int> & answer) {
	debug() << imie(a) imie(level) imie(answer);
	const int root = szef[level][a];
	if(root == a) {
		int low = preorder[level][a].first;
		int high = preorder[level][a].second;
		debug() << imie(root) imie(low) imie(high) imie(tr[level].get(1));
		if(low < high) {
			auto tmp = tr[level].getMin( low + 1, high );
			debug() << imie(tmp);
			answer = min(answer, tmp);
		}
		return;
	}
		
	const int child = reprezentant[level][a];
	assert(child != -1);
	
	const ll extra = tr[level].get(preorder[level][a].first) + own[a];
	
	int low = preorder[level][child].first, high = preorder[level][child].second;
	
	int ALFA = preorder[level][root].first, OMEGA = preorder[level][root].second;
	
	debug() << imie(ALFA) imie(low) imie(high) imie(OMEGA) imie(extra);
	
	auto ff = [&] (int from, int to) {
		if(from <= to) {
			pair<ll,int> maybe = tr[level].getMin(from, to);
			debug() << "old" imie(answer) imie(extra) imie(maybe);
			maybe.first += extra;
			answer = min(answer, maybe);
			debug() << "new" imie(answer);
		}
	};
	ff(ALFA, low - 1);
	ff(high + 1, OMEGA);
	
	solve(a, level + 1, answer);
	
}

map<pair<int,int>, ll> mapka;

int main() {
	int n, q;
	scanf("%d%d", &n, &q);
	for(int j = 0; j < 20; ++j)
		for(int i = 0; i < n; ++i)
			preorder[j][i] = make_pair(-1, -1);
		
	for(int i = 0; i < 20; ++i)
		for(int j = pot; j < 2 * pot; ++j)
			tr[i].tr[j].small = make_pair(INF, -1);
	for(int i = 0; i < n; ++i)
		scanf("%lld", &own[i]);
	for(int i = 0; i < n - 1; ++i) {
		int a, b;
		ll cost;
		scanf("%d%d%lld", &a, &b, &cost);
		--a; --b;
		mapka[make_pair(min(a, b), max(a, b))] = cost;
		w[a].push_back({b, cost});
		w[b].push_back({a, cost});
	}
	
	centroiduj(0, 0);
	
	for(int i = 0; i < 20; ++i) tr[i].init();
	
	int root = 0;
	
	while(q--) {
		int type;
		scanf("%d", &type);
		if(type == 1) {
			int a;
			ll value;
			scanf("%d%lld", &a, &value);
			--a;
			ll diff = value - own[a];
			debug() << imie(diff);
			own[a] = value;
			for(int level = 0; level < 20; ++level) {
				const pair<int,int> & p = preorder[level][a];
				if(p.first != -1)
					tr[level].add(p.first, p.first, -diff);
			}
		}
		else {
			int a, b;
			ll value;
			scanf("%d%d%lld", &a, &b, &value);
			--a; --b;
			ll previously = mapka[make_pair(min(a, b), max(a, b))];
			ll diff = value - previously;
			mapka[make_pair(min(a, b), max(a, b))] = value;
			for(int level = 0; level < 20; ++level) {
				pair<int,int> one = preorder[level][a];
				pair<int,int> two = preorder[level][b];
				if(one.first == -1 || two.first == -1) break;
				if(one.first > two.first) {
					swap(one, two);
					swap(a, b);
				}
				assert(one.first < two.first && two.first <= one.second);
				tr[level].add(two.first, two.second, diff); // koszt z plusem
			}
		}
		
		// pytaj i wypisz, zmien roota
		pair<ll,int> answer = make_pair(INF, -1);
		solve(root, 0, answer);
		assert(answer.second != -1);
		printf("%d ", answer.second + 1);
		root = answer.second;
	}
	puts("");
}