#P3976. [TJOI2015] 旅游

    ID: 2908 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2015线段树各省省选树链剖分,树剖Link-Cut Tree,LCT天津

[TJOI2015] 旅游

Description

To improve her IQ, ZJY plans to travel to a new world. The cities in this world form a tree, and there is exactly one path between any two cities.

Each city has a type of gem with a certain price. To maximize profit, ZJY will choose to buy in city A and then resell in city B (only one purchase is allowed).

Because ZJY often acts cute when buying gems, the gem price in every city she passes through will increase. Let us calculate the maximum profit ZJY can earn after the trip. (For example, if the gem price in city A is vv, then ZJY’s selling price is also vv.)

Description

Input Format

  • The first line contains a positive integer nn, the number of cities.
  • The next line contains nn positive integers, the initial prices pp of the gems in each city. Each initial price does not exceed 100100.
  • Starting from the third line, there are n1n-1 lines. Each line contains two integers xx and yy, indicating there is an edge between city xx and city yy. City indices start from 11.
  • The next line contains a positive integer qq, the number of queries.
  • Each of the next qq lines contains three positive integers aa, bb, vv, indicating that ZJY travels from aa to bb, and the gem price in every city along the route (including both endpoints) increases by vv.

Output Format

For each query, output the maximum possible profit ZJY can obtain. If the best result is a loss, output 00.

3
1 2 3
1 2
2 3
2
1 2 100
1 3 100
1
1
5
1 2 3 4 5
1 2
1 3
2 4
4 5
6
1 5 50
2 4 500
3 4 5000
3 5 50000
1 3 500000
2 3 5000000

4
2
551
551
0
499499

Hint

Constraints

  • For 30%30\% of the testdata, n100n \le 100, q104q \le 10^4.
  • For 100%100\% of the testdata, 1n,q5×1041 \le n, q \le 5 \times 10^4, and at any time the gem price in any city does not exceed 10910^9.

Translated by ChatGPT 5