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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

/**
 * Intervals are left_bound-closed and right_bound-open: [L, R)
 */
template<typename Config>
class bit {
  template<typename TConfig = Config>
  struct range_updates_config {
    template<class U> static char (&test(typename U::TRangeUpdate const*))[1];
    template<class U> static char (&test(...))[2];

    template<class U = TConfig> constexpr static typename U::TRangeUpdate _neutral(typename U::TRangeUpdate const*) {
      return U::neutral_range_update();
    }
    template<class U = TConfig> constexpr static void* _neutral(...) { return 0; }

    static const bool enabled = (sizeof(test<TConfig>(0)) == 1);
    typedef decltype(_neutral<TConfig>(0)) Type;
    constexpr static Type neutral() {
      return _neutral<TConfig>(0);
    }
  };

  typedef typename Config::TData TData;
  typedef typename range_updates_config<Config>::Type TRangeUpdate;
  typedef const function<bool(const TData&)>& Predicate;

  int size;
  /** Interval represented by the node */
  vector<pair<int, int>> bounds;
  vector<TData> data;
  vector<TRangeUpdate> range_updates;

  bool __intersects(int L, int R, int idx) {
    return bounds[idx].first < R && L < bounds[idx].second;
  }

  bool __covers(int L, int R, int idx) {
    return L <= bounds[idx].first && bounds[idx].second <= R;
  }

  void __update_range_single(int idx, const TRangeUpdate& op) {
    Config::apply(op, data[idx], bounds[idx].first, bounds[idx].second);
    Config::compose_range_updates(op, range_updates[idx]);
  }

  template<class T = Config, typename enable_if<!range_updates_config<T>::enabled, int>::type = 0>
  void __push_range_update(int idx) {}

  template<class T = Config, typename enable_if<range_updates_config<T>::enabled, int>::type = 0>
  void __push_range_update(int idx) {
    if (idx < size) {
      __update_range_single(2*idx, range_updates[idx]);
      __update_range_single(2*idx + 1, range_updates[idx]);
      range_updates[idx] = Config::neutral_range_update();
    }
  }

  void __update_range(int L, int R, const TRangeUpdate& op, int idx) {
    if (!__intersects(L, R, idx)) {
      return;
    }
    if (__covers(L, R, idx)) {
      __update_range_single(idx, op);
      return;
    }

    __push_range_update(idx);
    __update_range(L, R, op, 2*idx);
    __update_range(L, R, op, 2*idx+1);

    data[idx] = Config::merge(data[2*idx], data[2*idx+1]);
  }

  TData __query_range(int L, int R, int idx) {
    if (!__intersects(L, R, idx)) {
      return Config::neutral();
    }
    if (__covers(L, R, idx)) {
      return data[idx];
    }

    __push_range_update(idx);
    return Config::merge(
      __query_range(L, R, 2*idx),
      __query_range(L, R, 2*idx+1)
    );
  }

  /**
  * If last=1, the last matching is returned. If last=0, the first one.
  */
  int __find(int L, int R, Predicate fn, int idx, int last) {
    if (!__intersects(L, R, idx) || !fn(data[idx])) {
      return -1;
    }

    if (idx >= size) {
      return idx - size;
    }

    __push_range_update(idx);
    int preferred = __find(L, R, fn, 2*idx+last, last);
    if (preferred != -1) {
      return preferred;
    }
    return __find(L, R, fn, 2*idx+(last^1), last);
  }

public:
  bit(int _size = 0) {
    size = 1;
    while (size < _size) {
      size <<= 1;
    }
    data = vector<TData>(2*size, Config::neutral());
    range_updates = vector<TRangeUpdate>(2*size, range_updates_config<Config>::neutral());
    bounds = vector<pair<int, int>>(2*size);
    for (int i = 0; i < size; ++i) {
      bounds[i+size] = {i, i+1};
    }
    for (int i = size - 1; i >= 0; --i) {
      bounds[i] = {bounds[2*i].first, bounds[2*i+1].second};
    }
  }

  void update_range(int L, int R, const TRangeUpdate& op) {
    __update_range(L, R, op, 1);
  }

  void update_single(int pos, const TRangeUpdate& op) {
    update_range(pos, pos+1, op);
  }

  TData query_range(int L, int R) {
    return __query_range(L, R, 1);
  }

  TData query_single(int pos) {
    return query_range(pos, pos+1);
  }

  void set(int pos, TData value) {
    int idx = size + pos;
    if (range_updates_config<Config>::enabled) {
      idx = 1;
      while (idx < size) {
        __push_range_update(idx);
        idx = 2*idx + (pos >= bounds[2*idx+1].first);
      }
    }
    // Push pending operations

    data[idx] = value;
    idx >>= 1;
    while (idx > 0) {
      data[idx] = Config::merge(data[2*idx], data[2*idx+1]);
      idx >>= 1;
    }
  }

  /** @returns -1 if no element found */
  int first_which(int L, int R, Predicate contain_check) {
    return __find(L, R, contain_check, 1, 0);
  }

  int first_which(Predicate contain_check) {
    return first_which(0, size, contain_check);
  }

  /** @returns -1 if no element found */
  int last_which(int L, int R, Predicate contain_check) {
    return __find(L, R, contain_check, 1, 1);
  }

  int last_which(Predicate contain_check) {
    return last_which(0, size, contain_check);
  }
};

const int MOD = 1000000007;

const int NOT_FLIPPED = 0;
const int FLIPPED = 1;

typedef pair<LL, int> Result;
const Result NO_RESULT = { -2e18, -1 };

#define dprintf(...) printf(__VA_ARGS__); fflush(stdout);

struct Path {
  struct Data {
    Result best[2];

    LL gain() {
      return (best[0].st + best[1].st) / 2;
    }

    LL path_cost() {
      return (best[1].st - best[0].st) / 2;
    }

    void set_gain(Result g) {
      LL p = path_cost();
      best[NOT_FLIPPED] = { g.st - p, g.nd };
      best[FLIPPED] = { g.st + p, g.nd };
    }
  };

  struct bit_config {
    typedef Data TData;
    static TData neutral() {
      return { {{(LL)-2e18, -1}, {(LL)-2e18, -1} } };
    }
    static TData merge(const TData& left, const TData& right) {
      return Data {
        {
          max(left.best[NOT_FLIPPED], right.best[NOT_FLIPPED]),
          max(left.best[FLIPPED], right.best[FLIPPED]),
        }
      };
    }

    typedef LL TRangeUpdate;
    static void apply(const TRangeUpdate& op, TData& value, int A, int B) {
      value.best[NOT_FLIPPED].st -= op;
      value.best[FLIPPED].st += op;
    }

    static void compose_range_updates(const TRangeUpdate& outer, TRangeUpdate& inner) {
      inner += outer;
    }
    static TRangeUpdate neutral_range_update() {
      return TRangeUpdate();
    }
  };

  int N;
  bit<bit_config>* data;
  vector<LL> edge_costs;

  Path(vector<Result> gains, vector<LL> _edge_costs) {
    N = gains.size();
    edge_costs = _edge_costs;

    data = new bit<bit_config>(N);

    LL path_cost = 0;
    REP(i,N) {
      path_cost += edge_costs[i];
      // dprintf("Path cost: %lld\n", path_cost);
      Data node_data = {
        {
          {gains[i].st - path_cost, gains[i].nd},
          {gains[i].st + path_cost, gains[i].nd}
        }
      };

      Data tmp = node_data;
      // // dprintf("??!!%lld %lld\n", tmp.best[0].st, tmp.best[1].st);
      // // dprintf("!!%lld %lld\n", gains[i], edge_costs[i]);
      data->set(i, node_data);
    }

    // // dprintf("DATA: "); REP(i,N) // // dprintf("(%lld %lld) ", data->query_single(i).best[0].st, data->query_single(i).best[1].st); // // dprintf("\n");
  }

  Result get_best(int root, bool include_root = false) {
    LL path_to_root = root == -1 ? 0 : data->query_single(root).path_cost();
    // // dprintf("ASKING FOR ROOT %d WITH PATH_COST %lld (%lld %lld)\n", root, root_data.path_cost(), root_data.best[0].st, root_data.best[1].st);

    Result result = { -2e18, -1};

    Result above_root = data->query_range(0, root + include_root).best[FLIPPED];
    above_root.st -= path_to_root;
    result = max(result, above_root);

    Result below_root = data->query_range(root+1, N).best[NOT_FLIPPED];
    below_root.st += path_to_root;
    result = max(result, below_root);

    return result;
  };

  void set_gain(int v, Result gain) {
    auto dt = data->query_single(v);
    // // dprintf("Before updating %d to have gain %lld: %lld %lld\n", v, gain, dt.best[0].st, dt.best[1].st);
    dt.set_gain(gain);
    // // dprintf("After updating %d to have gain %lld: %lld %lld\n", v, gain, dt.best[0].st, dt.best[1].st);
    data->set(v, dt);
  }

  void set_edge_cost(int v, LL cost) {
    // dprintf("Updating edge cost on position %d of %d\n", v, edge_costs.size());

    LL previous_cost = edge_costs[v];

    data->update_range(v, N, cost - previous_cost);
    edge_costs[v] = cost;
  }

  LL path_length(int v) {
    return data->query_single(v).path_cost();
  }
};

vector<pair<int, LL>> adj[100005];

int paths_num = 0;
vector<int> path_nodes[100005];
PII path_parent[100005];
vector<int> child_paths[100005];

int parent[100005];
LL edge_to_parent[100005];
PII position_in_path[100005];

int dfs(int v, int p = -1) {
  parent[v] = p;

  vector<PII> child_paths_with_priority;
  int subtree_size = 1;
  for (auto uu: adj[v]) if (uu.st != p) {
    int child_size = dfs(uu.st, v);
    subtree_size += child_size;

    edge_to_parent[uu.st] = uu.nd;
    child_paths_with_priority.pb({position_in_path[uu.st].st, child_size});
  }

  int main_path;
  if (child_paths_with_priority.empty()) {
    main_path = paths_num++;
    path_parent[main_path] = { -1, -1 };
  } else {
    PII best_child_path = child_paths_with_priority[0];
    for (auto p: child_paths_with_priority) if (p.nd > best_child_path.nd) {
      best_child_path = p;
    }
    main_path = best_child_path.st;
    for (auto p: child_paths_with_priority) if (p.st != main_path) {
      child_paths[main_path].pb(p.st);
      path_parent[p.st] = { main_path, path_nodes[main_path].size() };
    }
  }
  position_in_path[v] = { main_path, path_nodes[main_path].size() };
  path_nodes[main_path].pb(v);

  return subtree_size;
}

LL node_gain[100005];
Result path_results[100005];
set<Result> subpath_gains[100005];

inline Result best_subpath_gain(int v, int subpath_to_ignore) {
  if (subpath_gains[v].empty()) {
    return NO_RESULT;
  }
  auto it = subpath_gains[v].rbegin();
  if (subpath_to_ignore > -1 && *it == path_results[subpath_to_ignore]) {
    ++it;
  }
  return it == subpath_gains[v].rend() ? NO_RESULT : *it;
}

inline Result full_gain(int v) {
  Result noderes = { node_gain[v], -v };
  return max(noderes, best_subpath_gain(v, -1));
}

int rev[100005];

Path* paths[100005];

void update_superpaths(int p) {
  // dprintf("Updating superpaths of %d...\n", p);

  while (true) {
    Result res = paths[p]->get_best(-1, false);

    Result previous_result = path_results[p];
    if (res == previous_result) break;

    path_results[p] = res;
    // dprintf("Setting the result for path %d to %lld %d\n", p, res.st, res.nd);

    PII par = path_parent[p];
    if (par.st == -1) break;

    subpath_gains[path_nodes[par.st][par.nd]].erase(previous_result);
    subpath_gains[path_nodes[par.st][par.nd]].insert(res);

    paths[par.st]->set_gain(par.nd, full_gain(path_nodes[par.st][par.nd]));
    p = par.st;
  }
}

void initialize_path(int p) {
  vector<int>& path = path_nodes[p];

  for (int v: path_nodes[p]) {
    position_in_path[v].nd = path.size() - 1 - position_in_path[v].nd;
  }
  reverse(path.begin(), path.end());
  for (int cp: child_paths[p]) {
    path_parent[cp].nd = path.size() - 1 - path_parent[cp].nd;
    initialize_path(cp);
    subpath_gains[path[path_parent[cp].nd]].insert(path_results[cp]);
  }

  vector<Result> gains;
  vector<LL> edges;
  for (int v: path) {
    gains.pb(full_gain(v));
    edges.pb(edge_to_parent[v]);
  }

  paths[p] = new Path(gains, edges);
  path_results[p] = paths[p]->get_best(-1, false);

  // dprintf("Initial best for path %d: %lld %d\n", p, path_results[p].st, path_results[p].nd);
}

Result get_best(int v) {
  PII pos = position_in_path[v];
  Result res = { -2e18, -1 };

  int processed_path = -1;
  LL additional_distance = 0;
  while (true) {
    Path* pth = paths[pos.st];
    auto path_best = pth->get_best(pos.nd, false);
    // dprintf("Best from path %d: %lld %d\n", pos.st, path_best.st, path_best.nd);
    path_best.st -= additional_distance;
    res = max(res, path_best);

    int v = path_nodes[pos.st][pos.nd];
    if (processed_path > -1) {
      Result node_res = { node_gain[v], -v };
      node_res.st -= additional_distance;
      res = max(res, node_res);
    }

    Result subpath_res = best_subpath_gain(v, processed_path);
    subpath_res.st -= additional_distance;
    res = max(res, subpath_res);

    additional_distance += pth->path_length(pos.nd);

    if (path_parent[pos.st].st == -1) break;
    processed_path = pos.st;
    pos = path_parent[pos.st];

  }

  return res;
}

int main() {
  // ios_base::sync_with_stdio(0);

  int N, Q;
  scanf("%d%d", &N, &Q);
  FOR(i,1,N+1) scanf("%lld", &node_gain[i]);
  REP(i,N-1) {
    int a, b;
    LL c;
    scanf("%d%d%lld", &a, &b, &c);
    adj[a].pb({b, c});
    adj[b].pb({a, c});
  }

  int root = 1;
  dfs(root);

  initialize_path(position_in_path[root].st);

  // dprintf("PATHS\n");
  REP(p, paths_num) {
    // dprintf("%d: ", p)
    for (int n: path_nodes[p]) {
      // dprintf("%d ", n);
    }
    // dprintf("(parent %d %d)\n", path_parent[p].st, path_parent[p].nd);
  }

  int current = 1;
  REP(q,Q) {
    int type;
    scanf("%d", &type);
    if (type == 1) {
      int v;
      LL d;
      scanf("%d%lld", &v, &d);
      node_gain[v] = d;

      PII pos = position_in_path[v];
      paths[pos.st]->set_gain(pos.nd, full_gain(v));
      update_superpaths(pos.st);
    } else {
      assert(type == 2);

      int a, b;
      LL d;
      scanf("%d%d%lld", &a, &b, &d);
      if (parent[b] == a) {
        swap(a, b);
      }
      PII pos = position_in_path[a];
      // dprintf("Updating edge cost on path %d(%d)\n", pos.st, pos.nd);
      // dprintf("!!%d\n", paths[pos.st]);
      paths[pos.st]->set_edge_cost(pos.nd, d);
      update_superpaths(pos.st);
    }

    Result best = get_best(current);
    current = -best.nd;
    // printf("%d(%lld)\n", current, best.st);
    // fflush(stdout);
    printf("%d ", current);
  }

  printf("\n");
}