33OJ
登录 注册

#P18107. [COCI 2026/2027 #2] 敌人 / Neprijatelji

    ID: 20868 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度暂无评定 上传者: 标签>COCI(克罗地亚)2026

[COCI 2026/2027 #2] 敌人 / Neprijatelji

Problem Description

A kingdom has nn cities, labelled 11 to nn, connected by n−1n-1 bidirectional roads in such a way that every city can be reached from every other city. City 11 is the capital. We say that city yy is subordinate to city xx if every path from the capital to city yy passes through city xx. In particular, every city is subordinate to itself.

One knight lives in each city, and the knight from city ii has reputation viv_i, which may be negative. The distance between two cities is the number of roads on the path between them. Knights from two different cities are enemies if the distance between their cities is less than 33.

A sequence of qq events will take place in the kingdom, each in one of the following two forms:

  • 1 x y – the reputation of the knight from city xx becomes yy,
  • 2 x – a feast is held in city xx, to which some knights from the cities subordinate to city xx are invited. No two of the invited knights may be enemies. What is the largest possible sum of reputations of the invited knights?

It is allowed to invite no knights to a feast, in which case the sum of reputations is 00. Feasts do not change the reputations of the knights.

Input Format

The first line contains positive integers nn and qq (1≤n≤200 0001 \le n \le 200\,000, 1≤q≤1 000 0001 \le q \le 1\,000\,000), the number of cities and the number of events.

The second line contains nn integers v1,v2,…,vnv_1, v_2, \ldots, v_n (−109≤vi≤109-10^9 \le v_i \le 10^9), the initial reputations of the knights.

Each of the following n−1n-1 lines contains two positive integers aa and bb (1≤a,b≤n1 \le a,b \le n, a≠ba \ne b), denoting that cities aa and bb are connected by a road.

Each of the following qq lines contains the description of one event in one of the forms given above. It holds that 1≤x≤n1 \le x \le n and −109≤y≤109-10^9 \le y \le 10^9.

There is at least one event of the form 2 x.

Output Format

For each event of the form 2 x, in the order in which they are given, output in a separate line the largest possible sum of reputations of the invited knights.

4 2
5 1 1 5
1 2
2 3
3 4
2 1
2 2
10
5
5 7
-3 4 6 -2 -1
1 2
1 3
1 4
4 5
2 1
1 3 -5
2 1
1 2 -1
2 1
1 5 7
2 1
6
4
0
7
8 8
3 2 4 5 1 2 6 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
2 1
2 2
1 4 -1
2 2
2 1
1 6 10
2 3
2 1
11
5
2
9
10
12

Hint

Explanation of example 1

The cities are connected in the order 1−2−3−41 - 2 - 3 - 4. The distance between cities 11 and 44 is 33, so the knights from these cities are not enemies and both can be invited to the feast in city 11. Cities 22, 33 and 44 are subordinate to city 22, and every two knights from these cities are enemies, so only the knight from city 44 is invited to the feast in city 22.

Explanation of example 2

All the feasts are held in the capital. The distance between cities 22 and 33 is 22, so the two of them cannot both be invited to the first feast, and it is best to invite only the knight from city 33. After his reputation becomes −5-5, it is best to invite only the knight from city 22. At the third feast, all knights have negative reputation, so nobody is invited. Only the knight from city 55 is invited to the fourth feast.

Explanation of example 3

The knights from cities 44 and 77 are invited to the first feast, and the knight from city 44 to the second one, for which only the knights from cities 22, 44 and 55 can be considered. After his reputation becomes −1-1, the knight from city 22 is invited to the feast in city 22, and the knights from cities 11 and 77, whose distance is 33, to the feast in city 11. After the reputation of the knight from city 66 becomes 1010, only he is invited to the feast in city 33, and the knights from cities 22 and 66 to the feast in city 11.

Scoring

Subtask Points Constraints
1 18 n,q≤2 000n,q \le 2\,000.
2 36 Cities ii and i+1i+1 are connected by a road for each i=1,2,…,n−1i=1,2,\ldots,n-1.
3 37 n,q≤100 000n,q \le 100\,000.
4 19 No additional constraints.