CF1575I.Illusions of the Desert
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Chanek Jones is back, helping his long-lost relative Indiana Jones, to find a secret treasure in a maze buried below a desert full of illusions.
The map of the labyrinth forms a tree with n rooms numbered from 1 to n and n−1 tunnels connecting them such that it is possible to travel between each pair of rooms through several tunnels.
The i-th room (1≤i≤n) has ai illusion rate. To go from the x-th room to the y-th room, there must exist a tunnel between x and y, and it takes max(∣ax+ay∣,∣ax−ay∣) energy. ∣z∣ denotes the absolute value of z.
To prevent grave robbers, the maze can change the illusion rate of any room in it. Chanek and Indiana would ask q queries.
There are two types of queries to be done:
- 1 u c — The illusion rate of the x-th room is changed to c (1≤u≤n, 0≤∣c∣≤109).
- 2 u v — Chanek and Indiana ask you the minimum sum of energy needed to take the secret treasure at room v if they are initially at room u (1≤u,v≤n).
Help them, so you can get a portion of the treasure!
查内克·琼斯再度归来,协助他失散多年的亲戚印第安纳·琼斯,在一片充满幻象的沙漠之下寻找一座迷宫中的秘密宝藏。
该迷宫的地图构成一棵树,包含 n 个房间,编号为 1 至 n,以及 n−1 条连接这些房间的隧道,使得任意两个房间之间均可通过若干条隧道相互到达。
第 i 个房间(1≤i≤n)具有幻象率 ai。要从第 x 个房间前往第 y 个房间,x 与 y 之间必须存在一条隧道,且所需能量为 max(∣ax+ay∣,∣ax−ay∣)。其中 ∣z∣ 表示 z 的绝对值。
为防止盗墓者闯入,迷宫可随时更改任意房间的幻象率。查内克与印第安纳将提出 q 个查询。
查询共分两类:
- 1 u c —— 将第 u 个房间的幻象率修改为 c(1≤u≤n,0≤∣c∣≤109)。
- 2 u v —— 查内克与印第安纳询问:若他们初始位于第 u 个房间,要抵达存放秘密宝藏的第 v 个房间,所需的最小总能量是多少?(1≤u,v≤n)
请帮助他们,这样你也能分得一份宝藏!
输入格式
The first line contains two integers n and q (2≤n≤105, 1≤q≤105) — the number of rooms in the maze and the number of queries.
The second line contains n integers a1,a2,…,an (0≤∣ai∣≤109) — inital illusion rate of each room.
The i-th of the next n−1 lines contains two integers si and ti (1≤si,ti≤n), meaning there is a tunnel connecting si-th room and ti-th room. The given edges form a tree.
The next q lines contain the query as described. The given queries are valid.
第一行包含两个整数 n 和 q(2≤n≤105,1≤q≤105)—— 分别表示迷宫中的房间数量和查询次数。
第二行包含 n 个整数 a1,a2,…,an(0≤∣ai∣≤109)—— 表示每个房间的初始幻象率。
接下来的 n−1 行中,第 i 行包含两个整数 si 和 ti(1≤si,ti≤n),表示存在一条连接第 si 个房间与第 ti 个房间的隧道。所给的边构成一棵树。
接下来的 q 行按描述给出查询。所有给定的查询均有效。
输出格式
For each type 2 query, output a line containing an integer — the minimum sum of energy needed for Chanek and Indiana to take the secret treasure.
对于每个类型 2 的查询,输出一行,包含一个整数——Chanek 和 Indiana 取得秘密宝藏所需的最小能量总和。
输入输出样例
输入#1
6 4 10 -9 2 -1 4 -6 1 5 5 4 5 6 6 2 6 3 2 1 2 1 1 -3 2 1 2 2 3 3
输出#1
39 32 0
说明/提示

In the first query, their movement from the 1-st to the 2-nd room is as follows.
- 1→5 — takes max(∣10+4∣,∣10−4∣)=14 energy.
- 5→6 — takes max(∣4+(−6)∣,∣4−(−6)∣)=10 energy.
- 6→2 — takes max(∣−6+(−9)∣,∣−6−(−9)∣)=15 energy.
In total, it takes 39 energy.
In the second query, the illusion rate of the 1-st room changes from 10 to −3.
In the third query, their movement from the 1-st to the 2-nd room is as follows.
- 1→5 — takes max(∣−3+4∣,∣−3−4∣)=7 energy.
- 5→6 — takes max(∣4+(−6)∣,∣4−(−6)∣)=10 energy.
- 6→2 — takes max(∣−6+(−9)∣,∣−6−(−9)∣)=15 energy.
Now, it takes 32 energy.

在第一次查询中,他们从第 1 个房间移动到第 2 个房间的过程如下:
- 1→5 — 消耗 max(∣10+4∣,∣10−4∣)=14 单位能量;
- 5→6 — 消耗 max(∣4+(−6)∣,∣4−(−6)∣)=10 单位能量;
- 6→2 — 消耗 max(∣−6+(−9)∣,∣−6−(−9)∣)=15 单位能量。
总计消耗 39 单位能量。
在第二次查询中,第 1 个房间的幻象系数由 10 变为 −3。
在第三次查询中,他们从第 1 个房间移动到第 2 个房间的过程如下:
- 1→5 — 消耗 max(∣−3+4∣,∣−3−4∣)=7 单位能量;
- 5→6 — 消耗 max(∣4+(−6)∣,∣4−(−6)∣)=10 单位能量;
- 6→2 — 消耗 max(∣−6+(−9)∣,∣−6−(−9)∣)=15 单位能量。
此时,总计消耗 32 单位能量。
输入解题思路,AI测评打分。不知道怎么写?