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
#include <iostream>
#include <vector>
#include <queue>
#include <map>

using namespace std;

struct Edge {
    Edge(long long c1, long long c2) : c1(c1), c2(c2) {}
    long long c1, c2;
};

ostream &operator<<(ostream &s, const Edge &e) {
    return s << '(' << e.c1 << ", " << e.c2 << ')';
}

bool operator==(const Edge &e1, const Edge &e2) {
    return e1.c1 == e2.c1 and e1.c2 == e2.c2;
}

bool operator<(const Edge &e1, const Edge &e2) {
    if (e1.c1 == e2.c1) {
        return e1.c2 < e2.c2;
    }
    return e1.c2 < e2.c2;
};

struct City {
    long long price;
    vector<int> neighbors;
};

long long MIN = -2000000000000000000;
struct EdgeValue {
    long long cost = 0;
    long long profit = MIN;
    int direction = -1;
};

ostream &operator<<(ostream &s, const EdgeValue &v) {
    return s << '(' << v.cost << ", " << v.profit << ", " << v.direction << ')';
}


int n, q;
int city = 0;
vector<City> cities;
map<Edge, EdgeValue> edges;

Edge getEdge(int c1, int c2) {
    if (c1 < c2) {
        return {c1, c2};
    } else {
        return {c2, c1};
    }
}


void show() {
    cerr << ">>>>>>>>>>>" << endl;
    for (int i = 0; i < n; i++) {
        cerr << i << " " << cities[i].price << " {";
        for (int n : cities[i].neighbors) {
            cerr << n << ",";
        }
        cerr << "}" << endl;
    }
    cerr << "city: " << city << endl;
    for (auto it : edges)
       cerr << " " << it.first << ":" << it.second << endl;

    cerr << "<<<<<<<<<<<<<<<<" << endl;
}


void update(int c, int parent) {
    if (parent >= 0) {
        long long profit = cities[c].price;
        for (int n : cities[c].neighbors) {
            if (n != parent) {
                profit = max(profit, edges[getEdge(c,n)].profit);
            }
        }
        EdgeValue& ev = edges[getEdge(c, parent)];
        ev.profit = profit - ev.cost;
        ev.direction = parent;
    }
}

void dfs(int c, int parent, bool visited[]) {
    visited[c] = true;

    for (int n : cities[c].neighbors) {
        if (!visited[n]) {
            dfs(n, c, visited);
        }
    }
    if (parent >= 0) {
        update(c, parent);
    }
}


void init() {
    bool *visited = new bool[n];
    for (int i = 0; i < n; i++) {
        visited[i] = false;
    }
 
    dfs(city, -1, visited);
}

void update(int c) {
    for (int n : cities[c].neighbors) {
        if (edges[getEdge(c,n)].direction != c) {
            update(c, n);
            update(n);
            return;
        } 
    }  
}

void doChange(int c, long long change) {
    cities[c].price = change;
    update(c);
}

void doChange(const int c1, int c2, long long change) {
    EdgeValue& ev = edges[getEdge(c1,c2)];
    ev.cost = change;
    update(c1 == ev.direction ? c2 : c1);
}

void go(int c, int parent) {
    long long max;
    int direction;
    if (parent < 0) {
        direction = -1;
        max = MIN;
    } else {
        max = cities[c].price;
        direction = c;
    }
    for (int n : cities[c].neighbors) {
        EdgeValue& ev = edges[getEdge(c,n)];
        if (n != parent && (ev.profit > max || (ev.profit == max && n < direction))) {
            direction = n;
            max = ev.profit;
        }
    }
    
    if (direction == c) {
        city = c;
    } else {
        update(c, direction);
        go(direction, c);
    }
}

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

    cin >> n >> q;
    cities.resize(n);
  
    for (int i = 0; i < n; i++) {
        cin >> cities[i].price;
    }
    for (int i = 0; i < n-1; i++) {
        int c1, c2;
        long long cost;
        cin >> c1 >> c2;
        cin >> cost;
        edges[getEdge(c1-1, c2-1)].cost = cost;
        cities[c1-1].neighbors.push_back(c2-1);
        cities[c2-1].neighbors.push_back(c1-1);
    }
    init();
    
    for (int i = 0; i < q; i++) {
        int kind;
        long long change;
        cin >> kind;
        if (kind == 1) {
            int c;
            cin >> c >> change;
            doChange(c-1, change);
        } else {
            int c1, c2;
            cin >> c1 >> c2 >> change;
            doChange(c1-1, c2-1, change);
        }
        go(city, -1);
        cout << city + 1;
        if (i == q-1) {
            cout << endl;
        } else {
            cout << " ";
        }
    }

}