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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#pragma GCC optimize("O3")
#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__) << "] "

using ll = long long;
using ld = long double;

constexpr int nax = 100 * 1000 + 105;
constexpr ll minfty = numeric_limits<ll>::min() + 10;
constexpr int pier = 800;

struct SingleResult {
  int w;
  ll value;

  SingleResult(const pair<ll, int>& p) : SingleResult(p.second, -p.first) {}
  SingleResult() : SingleResult(nax, minfty) {}
  SingleResult(int w, ll value) : w(w), value(value) {}

  pair<ll, int> AsPair() const {
    return {-value, w};
  }

  void MoveBy(ll dist) {
    if (value != minfty) {
      value -= dist;
    }
  }

  bool operator<(const SingleResult& sr) const {
    if (value == sr.value) return w > sr.w;
    return value < sr.value;
  }
};

debug& operator<<(debug& deb, const SingleResult& sr) {
  return deb << "SR{w = " << sr.w << ", v = " << sr.value << "}";
}

struct Result {
  SingleResult a, b;

  /*
  void Add(int w, ll value) {
    SingleResult sr(w, value);
    if (res[0] < sr) {
      if (res[0].w == sr.w) {
        // Drop res[0].
      } else {
        res[1] = res[0];
      }
      res[0] = sr;
    } else if (res[1] < sr) {
      if (res[0].w != sr.w) {
        res[1] = sr;
      }
    }
  }
  */

  void Add(const SingleResult& sr) {
    //Add(sr.w, sr.value);
    if (a < sr) {
      if (a.w == sr.w) {
        // Drop a.
      } else {
        b = a;
      }
      a = sr;
    } else if (b < sr) {
      if (a.w != sr.w) {
        b = sr;
      }
    }
  }

  void Add(const Result& r) {
    /*
    Add(r.res[0]);
    Add(r.res[1]);
    */
    Add(r.a);
    Add(r.b);
  }

  Result MovedBy(ll dist) {
    Result r(*this);
    r.a.MoveBy(dist);
    r.b.MoveBy(dist);
    return r;
  }

  int GetNext(int me) {
    if (a.w == me) {
      assert(b.w != me);
      return b.w;
    }
    return a.w;
    /*
    if (res[0].w == me) {
      assert(res[1].w != me);
      return res[1].w;
    }
    return res[0].w;
    */
  }
};

debug& operator<<(debug& deb, const Result& r) {
  return deb << "R{" << r.a << ", " << r.b << "}";
}

struct Graf {
  int* mapping_back;
  Result waga[nax];
  vector<pair<int, ll>> graf[nax];
  Result results[nax];

  void AddEdge(int a, int b, ll c) {
    auto it = lower_bound(graf[a].begin(), graf[a].end(), make_pair(b, numeric_limits<ll>::min()));
    if (it != graf[a].end() and it->first == b) {
      it->second = c;
    } else {
      graf[a].insert(it, make_pair(b, c));
    }
  }

  void Clear(int n, ll* wagi, int* mapping_b) {
    mapping_back = mapping_b;
    for (int i = 1; i <= n; i++) {
      graf[i].clear();
      waga[i] = Result();
      waga[i].Add(SingleResult{mapping_back[i], wagi[i]});
    }
  }

  inline void ComputeResult(int w, int o) {
    results[w] = waga[w];
    for (const auto& g : graf[w]) {
      if (g.first == o) continue;
      ComputeResult(g.first, w);
      results[w].Add(results[g.first].MovedBy(g.second));
    }
  }

  inline void ComputeDownResults(int w, int o) {
    for (const auto& g : graf[w]) {
      if (g.first == o) continue;
      results[g.first].Add(results[w].MovedBy(g.second));
      ComputeDownResults(g.first, w);
    }
  }

  void ComputeBestResults(int w) {
    ComputeResult(w, -1);
    ComputeDownResults(w, -1);
  }
};

struct Query {
  int a, b;
  ll x;
  int next;
};

debug& operator<<(debug& deb, const Query& q) {
  if (q.b == -1) {
    return deb << "Q{waga[" << q.a << "] = " << q.x << "}";
  } else {
    return deb << "Q{edge[" << q.a << ", " << q.b << "] = " << q.x << "}";
  }
}

int n;
ll waga[nax];
vector<pair<int, ll>> graf[nax];

void GlobalAddEdge(int a, int b, ll c) {
  auto it = lower_bound(graf[a].begin(), graf[a].end(), make_pair(b, numeric_limits<ll>::min()));
  if (it != graf[a].end() and it->first == b) {
    it->second = c;
  } else {
    graf[a].insert(it, make_pair(b, c));
  }
}

int big_to_id[nax];
vector<pair<pair<int, int>, ll>> add_to_big_graf;
bool is_big[nax];
vector<int> big_nodes;
int first_big_up[nax];
ll first_big_up_dist[nax];
int first_big_down[nax];
ll first_big_down_dist[nax];
Result best_to_small[nax];
Graf big_graf, small_graf;

Result FindResultFromBig(int w, int o) {
  big_graf.ComputeResult(big_to_id[w], (o == -1) ? -1 : big_to_id[o]);
  Result result = big_graf.results[big_to_id[w]];
  debug() << "FindResultFromBig(" imie(w) imie(o) ") = " << result;
  return result;
}

Result FindResult(int w) {
  debug() << "FindResult(" imie(w) ")";
  if (is_big[w]) {
    return FindResultFromBig(w, -1);
  }
  Result small_w = small_graf.results[w];
  debug() << imie(small_w);
  debug() << imie(first_big_up[w]) imie(first_big_down[w]);
  debug() << imie(first_big_up_dist[w]) imie(first_big_down_dist[w]);
  assert(first_big_up[w] != -1);
  if (first_big_down[w] == -1) {
    small_w.Add(
        FindResultFromBig(first_big_up[w], -1).MovedBy(first_big_up_dist[w]));
  } else {
    small_w.Add(
        FindResultFromBig(first_big_up[w], first_big_down[w])
            .MovedBy(first_big_up_dist[w]));
    small_w.Add(
        FindResultFromBig(first_big_down[w], first_big_up[w])
            .MovedBy(first_big_down_dist[w]));
  }
  return small_w;
}

void DoQuery(int next, Query& q) {
  debug() << "DoQuery(" imie(next) imie(q) ")";
  if (q.b == -1) {
    waga[q.a] = q.x;
    big_graf.waga[big_to_id[q.a]] = best_to_small[q.a];
    big_graf.waga[big_to_id[q.a]].Add(SingleResult{q.a, q.x});
  } else {
    GlobalAddEdge(q.a, q.b, q.x);
    GlobalAddEdge(q.b, q.a, q.x);
    //graf[q.a][q.b] = q.x;
    //graf[q.b][q.a] = q.x;
    big_graf.AddEdge(big_to_id[q.a], big_to_id[q.b], q.x);
    big_graf.AddEdge(big_to_id[q.b], big_to_id[q.a], q.x);
    //big_graf.graf[q.a][q.b] = q.x;
    //big_graf.graf[q.b][q.a] = q.x;
  }
  Result result = FindResult(next);
  debug() << imie(result);
  q.next = result.GetNext(next);
}

pair<int, ll> DfsBig(int w, int o) {
  vector<pair<int, ll>> vs;
  for (const auto& g : graf[w]) {
    if (g.first == o) continue;
    auto v = DfsBig(g.first, w);
    if (v.first != -1) {
      v.second += g.second;
      vs.push_back(v);
    }
  }
  if ((int) vs.size() >= 2) {
    is_big[w] = true;
  }
  if (is_big[w]) {
    for (const auto& v : vs) {
      add_to_big_graf.emplace_back(make_pair(v.first, w), v.second);
      /*
      big_graf.AddEdge(v.first, w, v.second);
      big_graf.AddEdge(w, v.first, v.second);
      */
      //big_graf.graf[v.first][w] = v.second;
      //big_graf.graf[w][v.first] = v.second;
    }
    return make_pair(w, 0ll);
  }
  if (vs.empty()) {
    first_big_down[w] = -1;
    return make_pair(-1, 0ll);
  }
  assert((int) vs.size() == 1);
  first_big_down[w] = vs[0].first;
  first_big_down_dist[w] = vs[0].second;
  return vs[0];
}

void DfsSmall(int w, int o, int fbu, ll fbud) {
  if (o != -1 and is_big[o]) {
    small_graf.ComputeBestResults(w);
  }
  if (is_big[w]) {
    fbu = w;
    fbud = 0;
  } else {
    first_big_up[w] = fbu;
    first_big_up_dist[w] = fbud;
  }
  for (const auto& g : graf[w]) {
    if (g.first == o) continue;
    if (!is_big[w] and first_big_down[w] != -1 and !is_big[g.first]) {
      if (first_big_down[g.first] != -1) {
        assert(first_big_down[g.first] == first_big_down[w]);
      } else {
        first_big_down[g.first] = first_big_down[w];
        first_big_down_dist[g.first] = first_big_down_dist[w] + g.second;
      }
    }
    DfsSmall(g.first, w, fbu, fbud + g.second);
  }
}

int identity[nax];
int big_mapping_back[nax];

void BuildGraphs(const Query* queries, int len) {
  add_to_big_graf.clear();
  big_nodes.clear();
  small_graf.Clear(n, waga, identity);
  for (int i = 1; i <= n; i++) {
    is_big[i] = false;
  }
  for (int i = 0; i < len; i++) {
    const Query& q = queries[i];
    is_big[q.a] = true;
    if (q.b != -1) {
      is_big[q.b] = true;
    }
  }
  is_big[1] = true;
  DfsBig(1, -1);
  vector<ll> big_wagi;
  big_wagi.push_back(-1);
  for (int i = 1; i <= n; i++) {
    if (!is_big[i]) {
      for (const auto& g : graf[i]) {
        if (!is_big[g.first]) {
          small_graf.AddEdge(i, g.first, g.second);
          //small_graf.graf[i][g.first] = g.second;
        }
      }
    } else {
      big_nodes.push_back(i);
      big_wagi.push_back(waga[i]);
      big_to_id[i] = (int) big_nodes.size();
      big_mapping_back[big_to_id[i]] = i;
    }
  }
  big_graf.Clear(big_nodes.size(), big_wagi.data(), big_mapping_back);
  for (const auto& p : add_to_big_graf) {
    const int a = p.first.first;
    const int b = p.first.second;
    const ll c = p.second;
    big_graf.AddEdge(big_to_id[a], big_to_id[b], c);
    big_graf.AddEdge(big_to_id[b], big_to_id[a], c);
  }
  DfsSmall(1, -1, -1, 0);
  for (int i = 1; i <= n; i++) {
    if (is_big[i]) {
      best_to_small[i] = Result();
      for (const auto& g : graf[i]) {
        if (!is_big[g.first]) {
          best_to_small[i].Add(small_graf.results[g.first].MovedBy(g.second));
        }
      }
      big_graf.waga[big_to_id[i]].Add(best_to_small[i]);
    }
  }
  #ifdef LOCAL
  /* Big */ {
    debug deb;
    deb << "Big:";
    for (int i = 1; i <= n; i++) {
      if (is_big[i]) {
        deb << " " << i;
      }
    }
  }
  /* Small */ {
    debug deb;
    deb << "Small:";
    for (int i = 1; i <= n; i++) {
      if (!is_big[i]) {
        deb << " " << i;
      }
    }
  }
  #endif
}

int Subtask(int next, Query* queries, int len) {
  assert(1 <= len and len <= pier);
  BuildGraphs(queries, len);
  for (int i = 0; i < len; i++) {
    DoQuery(next, queries[i]);
    next = queries[i].next;
  }
  return next;
}

int main() {
  int q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &waga[i]);
  }
  for (int i = 1; i <= n; i++) {
    identity[i] = i;
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    ll c;
    scanf("%d%d%lld", &a, &b, &c);
    GlobalAddEdge(a, b, c);
    GlobalAddEdge(b, a, c);
    //graf[a][b] = c;
    //graf[b][a] = c;
  }
  vector<Query> queries;
  for (int i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    Query q;
    if (t == 1) {
      q.b = -1;
      scanf("%d%lld", &q.a, &q.x);
    } else if (t == 2) {
      scanf("%d%d%lld", &q.a, &q.b, &q.x);
    } else {
      assert(false);
    }
    queries.push_back(q);
  }
  int next = 1;
  for (int i = 0; i < q; i++) {
    if (i % pier == 0) {
      next = Subtask(next, queries.data() + i, min(pier, q - i));
    }
  }
  for (int i = 0; i < q; i++) {
    if (i) {
      printf(" ");
    }
    printf("%d", queries[i].next);
  }
  printf("\n");
  return 0;
}