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

Description

A kingdom has $n$ cities, labelled $1$ to $n$, connected by $n-1$ bidirectional roads in such a way that every city can be reached from every other city. City $1$ is the **capital**. We say that city $y$ is **subordinate** to city $x$ if every path from the capital to city $y$ passes through city $x$. In particular, every city is subordinate to itself. **One knight** lives in each city, and the knight from city $i$ has **reputation** $v_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 $3$. ![](https://cdn.luogu.com.cn/upload/image_hosting/45q2x4f7.webp) A sequence of $q$ 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 $x$ becomes $y$, - `2 x` – a feast is held in city $x$, to which **some knights from the cities subordinate to city** $x$ 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 $0$. Feasts do not change the reputations of the knights.

Input Format

The first line contains positive integers $n$ and $q$ ($1 \le n \le 200\,000$, $1 \le q \le 1\,000\,000$), the number of cities and the number of events. The second line contains $n$ integers $v_1, v_2, \ldots, v_n$ ($-10^9 \le v_i \le 10^9$), the initial reputations of the knights. Each of the following $n-1$ lines contains two positive integers $a$ and $b$ ($1 \le a,b \le n$, $a \ne b$), denoting that cities $a$ and $b$ are connected by a road. Each of the following $q$ lines contains the description of one event in one of the forms given above. It holds that $1 \le x \le n$ and $-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.

Explanation/Hint

### Explanation of example 1 The cities are connected in the order $1 - 2 - 3 - 4$. The distance between cities $1$ and $4$ is $3$, so the knights from these cities are not enemies and both can be invited to the feast in city $1$. Cities $2$, $3$ and $4$ are subordinate to city $2$, and every two knights from these cities are enemies, so only the knight from city $4$ is invited to the feast in city $2$. ### Explanation of example 2 All the feasts are held in the capital. The distance between cities $2$ and $3$ is $2$, so the two of them cannot both be invited to the first feast, and it is best to invite only the knight from city $3$. After his reputation becomes $-5$, it is best to invite only the knight from city $2$. At the third feast, all knights have negative reputation, so nobody is invited. Only the knight from city $5$ is invited to the fourth feast. ### Explanation of example 3 The knights from cities $4$ and $7$ are invited to the first feast, and the knight from city $4$ to the second one, for which only the knights from cities $2$, $4$ and $5$ can be considered. After his reputation becomes $-1$, the knight from city $2$ is invited to the feast in city $2$, and the knights from cities $1$ and $7$, whose distance is $3$, to the feast in city $1$. After the reputation of the knight from city $6$ becomes $10$, only he is invited to the feast in city $3$, and the knights from cities $2$ and $6$ to the feast in city $1$. ### Scoring | Subtask | Points | Constraints | | --- | --- | --- | | 1 | 18 | $n,q \le 2\,000$. | | 2 | 36 | Cities $i$ and $i+1$ are connected by a road for each $i=1,2,\ldots,n-1$. | | 3 | 37 | $n,q \le 100\,000$. | | 4 | 19 | No additional constraints. |