#P4695. [PA 2017] Banany

[PA 2017] Banany

Description

Bajtazar is a merchant. Every day, he leaves the city he is currently in and travels to another city to sell bananas. Each road has a toll fee, and the destination city provides some revenue (note that intermediate cities provide no revenue). Each day, the fee of one road changes, or the revenue of one city changes. Bajtazar wants to know, after he wakes up each day, which city he should choose as the destination to obtain the maximum profit, and on the next day he will depart from that city again.

There are nn cities, connected by n1n - 1 roads, and every city is reachable from every other city. City ii has revenue ziz_i, and each edge has a fee ww.

If he does business from ss to tt, the profit is

ztePath(s,t)w(e)z_t - \sum_{e \in \mathrm{Path}(s, t)} w(e)

There are qq days in total.

Initially, Bajtazar is in city 11. Each day, after the changes take place, he will go to the next city. He will choose the one that yields the maximum profit; if there are multiple, he chooses the one with the smallest index. However, he cannot stay in the same city and must leave.

Input Format

The first line contains two integers nn and qq.

The second line contains nn integers representing the revenues z1,z2,,znz_1, z_2, \dots, z_n.

The next n1n - 1 lines each contain u v wu\ v\ w, describing an edge and its fee.

The next qq lines are of two types.

  • Input 1 i zi1\ i\ z_i means replacing the revenue of city ii with the new value.
  • Input 2 u v w2\ u\ v\ w means replacing the fee of the edge connecting cities (u,v)(u, v) with the new value.

Output Format

For each change, output one line. In total, output qq answers, each indicating Bajtazar's next destination after the change.

4 4
10 20 30 50
1 2 5
2 3 7
2 4 57
1 3 28
1 1 25
2 3 2 1
2 2 4 13
3 1 3 4

Hint

Constraints: n,q105n, q \le 10^5, 1zi10181 \le z_i \le 10^{18}, 1w10121 \le w \le 10^{12}.

Translated by ChatGPT 5