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
476
477
478
#include <iostream>
#include <limits>
#include <utility>
#include <vector>
using namespace std;

namespace {

constexpr auto inf = numeric_limits<long long>::max();

struct Best {
  long long value;
  int pos;

  Best(): value{-inf}, pos{-1}
  {
  }

  Best(long long v, int p): value{v}, pos{p}
  {
  }

  bool update(Best const& b)
  {
    if (!better(b, *this)) return false;
    value = b.value;
    pos = b.pos;
    return true;
  }

  friend bool better(Best const& a, Best const& b)
  {
    if (a.value < b.value) return false;
    if (a.value == b.value && a.pos >= b.pos) return false;
    return true;
  }

  friend bool operator==(Best const& a, Best const& b)
  {
    if (a.value != b.value) return false;
    if (a.pos != b.pos) return false;
    return true;
  }

  friend bool operator!=(Best const& a, Best const& b)
  {
    return !(a == b);
  }

  Best& operator+=(long long v)
  {
    if (value != -inf) value += v;
    return *this;
  }

  friend Best operator+(Best b, long long v)
  {
    return b += v;
  }

  Best& operator-=(long long v)
  {
    if (value != -inf) value -= v;
    return *this;
  }

  friend Best operator-(Best b, long long v)
  {
    return b -= v;
  }
};

struct Best2 {
  Best first;
  Best second;

  Best2(): first{}, second{}
  {
  }

  Best2(Best const& b): first{b}, second{}
  {
  }

  Best2(Best const& a, Best const& b): first{a}, second{b}
  {
    if (better(second, first)) swap(second, first);
  }

  void update(Best const& b)
  {
    if (b == first || b == second) return;

    if (better(b, first)) {
      second = first;
      first = b;
    } else if (better(b, second)) {
      second = b;
    }
  }

  void update(Best2 const& b)
  {
    update(b.first);
    update(b.second);
  }

  Best except(int p)
  {
    if (first.pos != p) return first;
    return second;
  }

  Best2& operator+=(long long v)
  {
    first += v;
    second += v;
    return *this;
  }

  friend Best2 operator+(Best2 b, long long v)
  {
    return b += v;
  }

  Best2& operator-=(long long v)
  {
    first -= v;
    second -= v;
    return *this;
  }

  friend Best2 operator-(Best2 b, long long v)
  {
    return b -= v;
  }
};

class RangeBest2 {
  int n;
  vector<Best2> data;
  vector<long long> delta;

  static int round_up(int n)
  {
    n += 5; // zapas
    int p = 1;
    while (p < n) p *= 2;
    return p;
  }

  void update_one(int v)
  {
    data[v] = data[2 * v];
    data[v].update(data[2 * v + 1]);
    data[v] += delta[v];
  }

  void update_up(int v)
  {
    while (v > 1) {
      v /= 2;
      update_one(v);
    }
  }

  void add_node(int v, long long d)
  {
    if (v < n) delta[v] += d;
    data[v] += d;
  }

public:
  RangeBest2(): n{0}, data{}, delta{}
  {
  }

  explicit RangeBest2(int n_): n{round_up(n_)}, data(2 * n), delta(n)
  {
  }

  void set(int i, Best2 x)
  {
    int v = n + i;
    while (v > 1) {
      v /= 2;
      x -= delta[v];
    }
    data[n + i] = x;
    update_up(n + i);
  }

  void add(int i, long long d)
  {
    data[n + i] += d;
    update_up(n + i);
  }

  void add(int i, int j, long long d)
  {
    if (i >= j) return;

    data[n + i] += d;

    int u = n + i;
    int v = n + j;

    while (u / 2 != v / 2) {
      if (u % 2 == 0) add_node(u + 1, d);
      if (v % 2 == 1) add_node(v - 1, d);
      u /= 2;
      v /= 2;
      update_one(u);
      update_one(v);
    }
    update_up(u);
  }

  Best2 operator[](int i) const
  {
    Best2 res = data[n + i];
    int v = n + i;
    while (v > 1) {
      v /= 2;
      res += delta[v];
    }
    return res;
  }

  Best2 find_best(int i, int j) const
  {
    if (i >= j) return {};

    Best2 res, res2;
    res.update(data[n + i]);

    int u = n + i;
    int v = n + j;

    while (u / 2 != v / 2) {
      if (u % 2 == 0) res.update(data[u + 1]);
      if (v % 2 == 1) res2.update(data[v - 1]);
      u /= 2;
      v /= 2;
      res += delta[u];
      res2 += delta[v];
    }

    res.update(res2);
    while (u > 1) {
      u /= 2;
      res += delta[u];
    }

    return res;
  }
};

struct Path {
  int n;
  int bottom;
  int top;
  RangeBest2 range;

  explicit Path(int v): n{1}, bottom{v}, top{v}, range{}
  {
  }
};

struct Node {
  long long profit;
  long long cost;
  int parent;
  vector<int> children;
  int start_time;
  int end_time;
  int size;
  int path;
  int path_pos;
  bool heavy;
  int heavy_child;
};

class Solver {
  int n;
  vector<Node> nodes;
  RangeBest2 profits_by_time;

  vector<Path> paths;

  int current = 0;

  int init_node(int v, vector<vector<pair<int, long long>>> const& neigh, int p = -1, long long c = 0, long long tc = 0, int t = 0)
  {
    nodes[v].parent = p;
    nodes[v].cost = c;
    nodes[v].start_time = t;
    nodes[v].size = 1;
    nodes[v].path = -1;
    nodes[v].heavy = false;
    nodes[v].heavy_child = -1;
    profits_by_time.set(t, Best{nodes[v].profit - tc, v});
    ++t;
    for (auto const& wd: neigh[v]) {
      auto w = wd.first;
      auto d = wd.second;
      if (w == p) continue;
      nodes[v].children.emplace_back(w);
      t = init_node(w, neigh, v, d, tc + d, t);
      nodes[v].size += nodes[w].size;
    }
    nodes[v].end_time = t;

    int half = nodes[v].size / 2;
    for (int w: nodes[v].children) {
      if (nodes[w].size > half) {
        nodes[v].path = nodes[w].path;
        nodes[w].heavy = true;
        nodes[v].heavy_child = w;
      }
    }
    if (nodes[v].path < 0) {
      nodes[v].path = paths.size();
      paths.emplace_back(v);
    }
    auto& path = paths[nodes[v].path];
    nodes[v].path_pos = path.n;
    ++path.n;
    path.top = v;
    return t;
  }

public:
  Solver(vector<long long> const& profits, vector<vector<pair<int, long long>>> const& neigh): n(profits.size()), nodes(n), profits_by_time(n)
  {
    paths.reserve(n);
    for (int i = 0; i < n; ++i) {
      nodes[i].profit = profits[i];
    }
    init_node(0, neigh);

    for (auto& p: paths) {
      p.range = RangeBest2(p.n);
      int v = p.bottom;
      long long c = 0;
      while (true) {
        p.range.set(nodes[v].path_pos, find_best2(v, nodes[v].heavy_child) - c);
        if (v == p.top) break;
        c += nodes[v].cost;
        v = nodes[v].parent;
      }
    }
  }

  void change_profit(int v, long long d)
  {
    profits_by_time.add(nodes[v].start_time, d - nodes[v].profit);
    nodes[v].profit = d;

    while (v >= 0) {
      auto& path = paths[nodes[v].path];
      auto c = distance(path.bottom) - distance(v);
      path.range.set(nodes[v].path_pos, find_best2(v, nodes[v].heavy_child) - c);
      if (nodes[v].heavy) {
        v = path.top;
      } else {
        v = nodes[v].parent;
      }
    }
  }

  void change_cost(int a, int b, long long d)
  {
    if (nodes[a].parent != b) swap(a, b);
    profits_by_time.add(nodes[a].start_time, nodes[a].end_time, nodes[a].cost - d);
    if (nodes[a].heavy) {
      auto& path = paths[nodes[a].path];
      path.range.add(nodes[a].path_pos + 1, path.n, nodes[a].cost - d);
    }
    nodes[a].cost = d;

    int v = a;
    while (v >= 0) {
      auto& path = paths[nodes[v].path];
      auto c = distance(path.bottom) - distance(v);
      path.range.set(nodes[v].path_pos, find_best2(v, nodes[v].heavy_child) - c);
      if (nodes[v].heavy) {
        v = path.top;
      } else {
        v = nodes[v].parent;
      }
    }
  }

  long long distance(int v)
  {
    return nodes[v].profit - profits_by_time[nodes[v].start_time].first.value;
  }

  Best2 find_best2(int v)
  {
    return profits_by_time.find_best(nodes[v].start_time, nodes[v].end_time) + distance(v);
  }

  Best2 find_best2(int v, int except)
  {
    if (except < 0) return find_best2(v);
    Best2 res = profits_by_time.find_best(nodes[v].start_time, nodes[except].start_time);
    res.update(profits_by_time.find_best(nodes[except].end_time, nodes[v].end_time));
    return res + distance(v);
  }

  long long find_next()
  {
    Best best;
    int v = current;
    auto d = distance(current);
    while (v >= 0) {
      auto c = d - distance(v);
      best.update(find_best2(v).except(current) - c);
      if (nodes[v].heavy) {
        auto const& path = paths[nodes[v].path];
        int i = nodes[v].path_pos;
        auto delta = distance(path.bottom) - distance(v);
        best.update(path.range.find_best(i, path.n).except(current) + delta - c);
        v = path.top;
      } else {
        v = nodes[v].parent;
      }
    }
    current = best.pos;
    return current;
  }
};

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  cin >> n >> q;

  vector<long long> profits(n);
  for (auto& p: profits) cin >> p;
  vector<vector<pair<int, long long>>> neigh(n);
  for (int i = 1; i < n; ++i) {
    int a, b;
    long long d;
    cin >> a >> b >> d;
    neigh[a - 1].emplace_back(b - 1, d);
    neigh[b - 1].emplace_back(a - 1, d);
  }

  Solver solver(profits, neigh);
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    if (t == 1) {
      int v;
      long long d;
      cin >> v >> d;
      solver.change_profit(v - 1, d);
    } else if (t == 2) {
      int a, b;
      long long d;
      cin >> a >> b >> d;
      solver.change_cost(a - 1, b - 1, d);
    }
    if (i > 0) cout << ' ';
    cout << solver.find_next() + 1;
  }
  cout << std::endl;

  return 0;
}