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
#include <cstdio>
#include <vector>
#include <limits>
#include <unordered_map>
#include <cassert>

#include <iostream>

const long long MIN_INCOME = std::numeric_limits<long long>::min() + (1LL<<40);
const int MAX_Q = 100000;

struct Edge {
    int to;
    long long cost = -1;
    int timestamp = -1;

    Edge() = default;
    Edge(int t, long long c) : to(t), cost(c) { }

    long long best_income = MIN_INCOME;
    int best_node = -1;
};

struct Node {
    long long earnings;
    std::vector<Edge> edges;
};

struct Graph  {

    int n;
    std::vector<Node> nodes;

    // node_id, timestamp
    // if e.timestamp < q best_income is overestimated
    std::unordered_map<int, int> invalidated;

    Graph(int n_, int q) : n(n_), nodes(n) { 
        invalidated.reserve(std::min(n, q));
        invalidated.emplace(-1, MAX_Q);
    }

    void update_node(int node_id, long long earnings, int timestamp)
    {
        // std::cerr << "update_node " << node_id << " earnings " << nodes[node_id].earnings << " -> " << earnings << std::endl;
        if (nodes[node_id].earnings == earnings) return;

        bool growth = (nodes[node_id].earnings < earnings);
        nodes[node_id].earnings = earnings;

        if (growth)
            update(node_id, -1, timestamp, earnings, node_id);
        else
            invalidated.emplace(node_id, timestamp);
    }


    void update_edge(int from, int to, long long v, int timestamp)
    {
        // std::cerr << "update_edge " << from << "->" << to << " cost:" << v<< std::endl;

        for (Edge &e : nodes[from].edges)
        {
            if (e.to == to)
            {
                if (e.cost == v) return;

                bool growth = (e.cost > v);
                e.cost = v;

                if (e.best_node == -1)
                    return;

                if (growth) 
                {
                    // update edge
                    search(e.to, from, e, timestamp);

                    // push new best_income up
                    update(from, to, timestamp, e.best_income, e.best_node);
                }
                else
                    invalidated.emplace(e.best_node, timestamp);

                return;
            }
        }
    }

    
    void add_edge(int from, int to, long long cost)
    {
        nodes[from].edges.emplace_back(to, cost);
    }

    // if !is_valid best_income is overestimated
    bool is_valid(Edge & e)
    {
        auto it = invalidated.find(e.best_node);
        if (it == invalidated.end() || it->second < e.timestamp)
            return true;
        return false;
    }

    Edge & rev_edge(int from, int to) {
        for (Edge & e : nodes[to].edges)
            if (e.to == from)
                return e;
    }

    // dfs push new (bigger) best_income up
    void update(int node_id, int from_node_id, int timestamp, long long best_income, int best_node)
    {
        //std::cerr << "update n:" << node_id << " f:" << from_node_id << " i:" << best_income << " n:" << best_node << std::endl;

        for (Edge & re : nodes[node_id].edges)  // backward
        {
            if (re.to == from_node_id) continue;
            Edge & e = rev_edge(node_id, re.to);

            if (e.best_node == -1) // lazy, edge not explored yet 
                continue;

            if ((e.best_income < best_income - e.cost) || 
                (e.best_income == best_income - e.cost && e.best_node > best_node))
            {
                e.best_income = best_income - e.cost;
                e.best_node = best_node;
                e.timestamp = timestamp;

                update(re.to, node_id, timestamp, e.best_income, e.best_node);
            }
        }
    }
  

    // dfs search, update edge best_income
    void search(int node_id, int from_node_id, Edge &e_from, int timestamp)
    {
        //std::cerr << "serach n:" << node_id << " f:" << e_from.to << " i:" << e_from.best_income << " n:" << e_from.best_node << std::endl;

        // stay in the city
        e_from.best_node = node_id;
        e_from.best_income = nodes[node_id].earnings - e_from.cost;

        for (Edge & e : nodes[node_id].edges)  // backward
        {
            if (e.to == from_node_id) continue;

            if (e.best_node == -1) // now explore yet
                search(e.to, node_id, e, timestamp);

            if ((e_from.best_income < e.best_income - e_from.cost) || 
                ((e_from.best_income == e.best_income - e_from.cost) && e_from.best_node > e.best_node))
            {
                if (!is_valid(e)) // lazy update only if promising (!is_valid means overestimated)
                    search(e.to, node_id, e, timestamp);

                if ((e_from.best_income < e.best_income - e_from.cost) || 
                    ((e_from.best_income == e.best_income - e_from.cost) && e_from.best_node > e.best_node))
                {
                    e_from.best_income = e.best_income - e_from.cost;
                    e_from.best_node = e.best_node;
                }
            }
        }

        e_from.timestamp = timestamp;
    }


    Edge best_edge(int current_node_id, int timestamp)
    {
        //std::cerr << "next( " << current_node_id << " )" << std::endl;
        Edge best_edge; 

        for (Edge & e : nodes[current_node_id].edges)  // backward
        {
            if (!is_valid(e))
                search(e.to, current_node_id, e, timestamp);

            if ((best_edge.best_income < e.best_income) ||
               (best_edge.best_income == e.best_income && best_edge.best_node > e.best_node))
            {
                best_edge = e;
            }
        }

        return best_edge;
    }


    void debug(int current_node_id) {
        std::cerr << "graph: current_node_id: " << current_node_id << std::endl;

        for (int i = 0; i < n; ++i)
        {
            std::cerr << "\t" << i << " e:" << nodes[i].earnings;
            for (Edge & e : nodes[i].edges)  // backward
            {
                std::cerr << " (" << e.to << " c:" << e.cost <<  " n:" << e.best_node << " i:" << e.best_income << ")"; 
            }
            std::cerr << std::endl;
        }
        std::cerr << std::endl;
    }

};

int main() {

    int n, q;
    scanf("%d %d", &n, &q);

    Graph graph(n, q);

    for (auto && n : graph.nodes)
        scanf("%lld", &n.earnings);

    int a, b, c;
    long long v;
    for (int i = 0; i < n-1; ++i)
    {
        scanf("%d %d %lld", &a, &b, &v);
        --a; --b;
        graph.add_edge(a, b, v);
        graph.add_edge(b, a, v);
    }

    int current_node_id = 0;

    for (int i = 0; i < q; ++i) 
    {
        scanf("%d", &c);
        if (c == 1) 
        {
            scanf("%d %lld", &a, &v);
            --a;
            graph.update_node(a, v, q);
        } else 
        {
            scanf("%d %d %lld", &a, &b, &v);
            --a; --b;
            graph.update_edge(a, b, v, q);
            graph.update_edge(b, a, v, q);
        }

        //graph.debug(current_node_id);

        int new_node_id = graph.best_edge(current_node_id, q).best_node;
        assert(new_node_id >= 0);
        assert(new_node_id != current_node_id);
        current_node_id = new_node_id;

        //graph.debug(current_node_id);

        if (i > 0) printf(" ");
        printf("%d", current_node_id + 1);
        //printf("\n");
    }
    printf("\n");

    return 0;
}