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
#include <cstdio>
#include <climits>
#include <vector>
#include <map>
#include <queue>
#include <utility>

class Road {
	long long int tax;

	int best;
	long long int profit;

public:
	long long int getProfit() { return profit; }
	long long int getProfitNoTax() { return profit + tax; }
	long long int getTax() { return tax; }
	int getBest() { return best; }
	void setBest(int id, long long int profit) { 
		best = id; 
		this->profit = profit - tax; 
	}
	void updateTax(long long int tax) { 
		if (best != -1) {
			profit += this->tax - tax;
		}
		this->tax = tax;
	}

	Road(long long t) : tax(t), best(-1), profit(LLONG_MIN) {}
	Road() : tax(-1), best(-1), profit(LLONG_MIN) {}
};

std::vector<long long int> cityProfits;
std::vector<std::map<int, Road> > roads;

struct State {
	int previous;
	int current;
	int iter;
	int best;
	long long int profit;

	State(int p, int c, int i, int b, long long pr) : previous(p), current(c), iter(i), best(b), profit(pr) {}
};

bool isBetter(int id1, int id2, long long int profit1, long long int profit2) {
	if (profit2 == profit1) {
		return id2 < id1;
	}
	return profit2 > profit1;
}

int moveToNext(int current)
{
	State cs(-1, current, roads[current].begin()->first, -1, LLONG_MIN);
	std::vector<State> states(1, cs);

	while (!states.empty()) {
		cs = states.back();
		states.pop_back();

		auto iter = roads[cs.current].find(cs.iter);
		while (iter != roads[cs.current].end()) {
			if (iter->first != cs.previous) {
				if (iter->second.getBest() != -1) {
					if (isBetter(cs.best, iter->second.getBest(), cs.profit, iter->second.getProfit())) {
						cs.profit = iter->second.getProfit();
						cs.best = iter->second.getBest();
					}
				}
				else {
					cs.iter = iter->first;
					states.push_back(cs);
					states.push_back(State(cs.current, iter->first, roads[iter->first].begin()->first, -1, LLONG_MIN));
					break;
				}
			}
			++iter;
		}

		if (iter == roads[cs.current].end()) {
			if (cs.previous != -1) {
				if (isBetter(cs.best, cs.current, cs.profit, cityProfits[cs.current])) {
					cs.best = cs.current;
					cs.profit = cityProfits[cs.current];
				}
				
				roads[cs.previous][cs.current].setBest(cs.best, cs.profit);
			}
		}
	}

	return cs.best;
}

void processRoadUpdated(int a, int b) 
{
	std::queue<std::pair<int, int> > toProcess;
	toProcess.push(std::make_pair(a, b));

	while (!toProcess.empty()) {
		auto road = toProcess.front();
		toProcess.pop();

		for (auto iter = roads[road.first].begin(); iter != roads[road.first].end(); ++iter) {
			if (roads[iter->first][road.first].getBest() != -1 && iter->first != road.second) {
				int best = road.first;
				long long int profit = cityProfits[road.first];
				for (auto it2 = roads[road.first].begin(); it2 != roads[road.first].end(); ++it2) {
					if ((it2->first != iter->first) && isBetter(best, it2->second.getBest(), profit, it2->second.getProfit())) {
						best = it2->second.getBest();
						profit = it2->second.getProfit();
					}
				}

				if (roads[iter->first][road.first].getBest() != best
					|| roads[iter->first][road.first].getProfit() != profit) {
					roads[iter->first][road.first].setBest(best, profit);
					toProcess.push(std::make_pair(iter->first, road.first));
				}
			}
		}
	}
};

void updateCityProfit(int city, long long int newProfit)
{
	long long int diff = newProfit - cityProfits[city];
	if (diff != 0) {
		cityProfits[city] = newProfit;
		for (auto iter = roads[city].begin(); iter != roads[city].end(); ++iter) {
			if (roads[iter->first][city].getBest() == -1) {
				// nothing to do
			}
			else if (roads[iter->first][city].getBest() != city) {
				if (diff > 0 
					&& isBetter(roads[iter->first][city].getBest(), city, roads[iter->first][city].getProfitNoTax(), cityProfits[city])) {
					roads[iter->first][city].setBest(city, cityProfits[city]);
					processRoadUpdated(iter->first, city);
				}
			}
			else /* (roads[iter->first][city].best == city) */ {
				if (diff > 0) {
					roads[iter->first][city].setBest(city, cityProfits[city]);
					processRoadUpdated(iter->first, city);
				}
				else {
					int best = city;
					long long int profit = cityProfits[city];
					for (auto it2 = roads[city].begin(); it2 != roads[city].end(); ++it2) {
						if ((it2->first != iter->first) && isBetter(best, it2->second.getBest(), profit, it2->second.getProfit())) {
							best = it2->second.getBest();
							profit = it2->second.getProfit();
						}
					}
					roads[iter->first][city].setBest(best, profit);
					processRoadUpdated(iter->first, city);
				}
			}
		}
	}
}

void updateRoadTax(int a, int b, long long int tax) {
	if (roads[a][b].getTax() != tax) {
		roads[a][b].updateTax(tax);
		roads[b][a].updateTax(tax);
		if (roads[a][b].getBest() != -1) {
			processRoadUpdated(a, b);
		}
		if (roads[b][a].getBest() != -1) {
			processRoadUpdated(b, a);
		}
	}
}

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

	long long int tmp;
	cityProfits.push_back(0);
	for (int i = 0; i < n; ++i) {
		scanf("%lld", &tmp);
		cityProfits.push_back(tmp);
	}

	roads.assign(n + 1, std::map<int, Road>());
	int a, b;
	for (int i = 1; i < n; ++i) {
		scanf("%d %d %lld", &a, &b, &tmp);
		roads[a][b] = Road(tmp);
		roads[b][a] = Road(tmp);
	}

	int current = 1;
	int variant;
	while (q--) {
		scanf("%d", &variant);
		if (variant == 1) {
			scanf("%d %lld", &a, &tmp);
			updateCityProfit(a, tmp);
		}
		else {
			scanf("%d %d %lld", &a, &b, &tmp);
			updateRoadTax(a, b, tmp);
		}

		current = moveToNext(current);
		printf("%d ", current);
	}
	return 0;
}