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
#include "bits/stdc++.h"

//#define cerr if(0) cerr
using namespace std;
using Int = long long;

const Int kInf = 2e18;

using pIi = pair<Int, int>;


struct Vertex {
    int i;
    Int cost;
    int t0;
    int t1;
};

template<class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
    return out << p.first << ":" << p.second;
}

bool CmpPIi(const pIi& p1, const pIi& p2) {
    return make_pair(p1.first, -p1.second) < make_pair(p2.first, -p2.second);
}

struct Node {
    Node(vector<Vertex>::iterator it0, vector<Vertex>::iterator it1, int i0_) :
            i0(i0_),
            i1(i0 + (int)(it1 - it0)),
            acc(0ll),
            left((i1 - i0 == 1) ? nullptr :
                    new Node(it0, it0 + (i1 - i0) / 2, i0)),
            right((i1 - i0 == 1) ? nullptr :
                    new Node(it0 + (i1 - i0) / 2, it1, i0_ + (i1 - i0) / 2)),
            max_val((i1 - i0 == 1) ? make_pair(it0->cost, it0->i) :
                    max(left->max_val, right->max_val, CmpPIi)) {}

    void Add(int q0, int q1, Int val) {
        PushAcc();
        if (q0 <= i0 && i1 <= q1) {acc += val; PushAcc(); return;}
        if (q1 <= i0 || i1 <= q0) {return;}
        max_val = make_pair(-kInf, -1);
        for (auto child : {left, right}) {
            if (child) { 
                child->Add(q0, q1, val);
                max_val = max(max_val, child->max_val, CmpPIi);
            }
        }
    }

    pair<Int,int> Max(int q0, int q1) {
        PushAcc();
        if (q0 <= i0 && i1 <= q1) {return max_val;}
        if (q1 <= i0 || i1 <= q0) {return make_pair(-kInf, -1);}
        pair<Int, int> mx(-kInf, -1);
        if (left) { mx = max(mx, left->Max(q0, q1), CmpPIi); }
        if (right) { mx = max(mx, right->Max(q0, q1), CmpPIi); }
        return mx;
    }

    void PushAcc() {
        max_val.first += acc;
        if (left) { left->acc += acc;}
        if (right) { right->acc += acc;}
        acc = 0ll;
    }

    const int i0;
    const int i1;
    Int acc;
    Node* const left;
    Node* const right;
    pIi max_val;
};

ostream& operator<<(ostream& out, const Node& node) {
    if (node.i1-node.i0 == 1) {
        out << node.max_val;
    } else {
        out << '[' << *node.left << "," << *node.right << ']';
    }
    return out;

}

using Graph = vector<vector<pair<int, Int>>>;

int GraphSize(const Graph& g, vector<int>& depths, int a, int p) {
    int size = 1;
    for (auto& bi : g[a]) {
        if (bi.first != p && depths[bi.first] == -1) {
            size += GraphSize(g, depths, bi.first, a);
        }
    }
    return size;
}

pair<bool, int> CentroidOrSize(const Graph& g, vector<int>& depths, int a, int p,
        int n) {
    vector<int> sizes;
    for (auto& bi : g[a]) {
        if (bi.first != p && depths[bi.first] == -1) {
            auto cs = CentroidOrSize(g, depths, bi.first, a, n);
            if (cs.first == 0) {
                return cs;
            }
            sizes.push_back(cs.second);
        }
    }
    int size_sum = accumulate(sizes.begin(), sizes.end(), 0);
    if (size_sum >= (n-1) / 2 && (sizes.size() == 0 ||
            *max_element(sizes.begin(), sizes.end()) <= n / 2)) {
        return make_pair(0, a);
    }

    return make_pair(1, 1 + size_sum);
}


void CentroidDecomposition(const Graph& g, vector<int>& depths,
        int a, int l) {
    int n = GraphSize(g, depths, a, -1);
    auto cs = CentroidOrSize(g, depths, a, -1, n);
    assert(cs.first == 0);
    depths[cs.second] = l;
    for (auto& bi : g[cs.second]) {
        if (depths[bi.first] == -1) {
            CentroidDecomposition(g, depths, bi.first, l + 1);
        }
    }

}

void Dfs(const Graph& g, const vector<int>& depths, const vector<Int>& zs,
    int a, int p, int l, Int c, vector<Vertex>& v) {
    if (depths[a] < l) return;
    int t0 = (int) v.size();
    v.emplace_back();
    v[t0].i = a;
    v[t0].cost = zs[a] - c;
    v[t0].t0 = t0;
    for (auto bi : g[a]) {
        if (bi.first != p) {
            Dfs(g, depths, zs, bi.first, a, l, c + bi.second, v);
            assert(depths[bi.first] != -1);
        }
    }
    v[t0].t1 = (int) v.size();
}

pair<int, int> Normalize(int a, int b) {
    if (b < a) swap(a, b);
    return make_pair(a, b);
}

int main() {
    ios_base::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<Int> zs(n);
    map<pair<int, int>, Int> abcs;
    for (int i = 0; i < n; ++i) {
        cin >> zs[i];
    }
    Graph g(n);
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        Int c;
        cin >> a >> b >> c;
        --a;
        --b;
        abcs[Normalize(a, b)] = c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    vector<int> depths(n, -1);
    vector<map<Node*, pair<int, int>>> trees(n);
    CentroidDecomposition(g, depths, 0, 0);

    for (int i = 0; i < n; ++i) {
        assert(depths[i] != -1);
        vector<Vertex> v;
        Dfs(g, depths, zs, i, -1, depths[i], 0ll, v);
        auto node = new Node(v.begin(), v.end(), 0);
        for (auto& w : v) {
            trees[w.i][node] = make_pair(w.t0, w.t1);
        }
    }

    int cur = 0;

    for (int i = 0; i < q; ++i) {
        int type;
        cin >> type;
        if (type == 1) {
            int v;
            Int d;
            cin >> v >> d;
            --v;
            for (auto& kv : trees[v]) {
                Node* node = kv.first;
                int q0, q1;
                tie(q0, q1) = kv.second;
                node->Add(q0, q0+1, d-zs[v]);
            }
            zs[v] = d;
        } else {
            int a, b;
            Int d;
            cin >> a >> b >> d;
            --a;
            --b;
            for (auto &kv : trees[a]) {
                Node* node = kv.first;
                int q0a, q1a;
                tie(q0a, q1a) = kv.second;
                if (trees[b].count(node)) {
                    int q0b, q1b;
                    tie(q0b, q1b) = trees[b][node];
                    int q0 = max(q0a, q0b);
                    int q1 = min(q1a, q1b);
                    node->Add(q0, q1, abcs[Normalize(a, b)] - d);
                }
            }
            abcs[Normalize(a, b)] = d;
        }
        pIi opt = make_pair(-kInf, -1);
        for (auto& kv : trees[cur]) {
            int q0, q1;
            Node* node = kv.first;
            tie(q0, q1) = kv.second;
            auto p0 = max(node->Max(node->i0, q0), node->Max(q0+1, node->i1), CmpPIi);
            int h = trees[cur][node].first;
            auto p1 = node->Max(h, h+1);
            p0.first += p1.first - zs[cur];
            opt = max(opt, p0, CmpPIi);
        }
        cur = opt.second;
        cout << opt.second + 1 << ' ';
    }
    //cout << '\n';
}