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 nn rooms numbered from 11 to nn and n−1n - 1 tunnels connecting them such that it is possible to travel between each pair of rooms through several tunnels.

The ii-th room (1≤i≤n1 \leq i \leq n) has aia_i illusion rate. To go from the xx-th room to the yy-th room, there must exist a tunnel between xx and yy, and it takes max⁡(∣ax+ay∣,∣ax−ay∣)\max(|a_x + a_y|, |a_x - a_y|) energy. ∣z∣|z| denotes the absolute value of zz.

To prevent grave robbers, the maze can change the illusion rate of any room in it. Chanek and Indiana would ask qq queries.

There are two types of queries to be done:

  • 1 u c1\ u\ c — The illusion rate of the xx-th room is changed to cc (1≤u≤n1 \leq u \leq n, 0≤∣c∣≤1090 \leq |c| \leq 10^9).
  • 2 u v2\ u\ v — Chanek and Indiana ask you the minimum sum of energy needed to take the secret treasure at room vv if they are initially at room uu (1≤u,v≤n1 \leq u, v \leq n).

Help them, so you can get a portion of the treasure!

查内克·琼斯再度归来,协助他失散多年的亲戚印第安纳·琼斯,在一片充满幻象的沙漠之下寻找一座迷宫中的秘密宝藏。

该迷宫的地图构成一棵树,包含 nn 个房间,编号为 11 至 nn,以及 n−1n - 1 条连接这些房间的隧道,使得任意两个房间之间均可通过若干条隧道相互到达。

第 ii 个房间(1≤i≤n1 \leq i \leq n)具有幻象率 aia_i。要从第 xx 个房间前往第 yy 个房间,xx 与 yy 之间必须存在一条隧道,且所需能量为 max⁡(∣ax+ay∣,∣ax−ay∣)\max(|a_x + a_y|, |a_x - a_y|)。其中 ∣z∣|z| 表示 zz 的绝对值。

为防止盗墓者闯入,迷宫可随时更改任意房间的幻象率。查内克与印第安纳将提出 qq 个查询。

查询共分两类:

  • 1 u c1\ u\ c —— 将第 uu 个房间的幻象率修改为 cc(1≤u≤n1 \leq u \leq n,0≤∣c∣≤1090 \leq |c| \leq 10^9)。
  • 2 u v2\ u\ v —— 查内克与印第安纳询问:若他们初始位于第 uu 个房间,要抵达存放秘密宝藏的第 vv 个房间,所需的最小总能量是多少?(1≤u,v≤n1 \leq u, v \leq n)

请帮助他们,这样你也能分得一份宝藏!

输入格式

The first line contains two integers nn and qq (2≤n≤1052 \leq n \leq 10^5, 1≤q≤1051 \leq q \leq 10^5) — the number of rooms in the maze and the number of queries.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤∣ai∣≤1090 \leq |a_i| \leq 10^9) — inital illusion rate of each room.

The ii-th of the next n−1n-1 lines contains two integers sis_i and tit_i (1≤si,ti≤n1 \leq s_i, t_i \leq n), meaning there is a tunnel connecting sis_i-th room and tit_i-th room. The given edges form a tree.

The next qq lines contain the query as described. The given queries are valid.

第一行包含两个整数 nn 和 qq(2≤n≤1052 \leq n \leq 10^5,1≤q≤1051 \leq q \leq 10^5)—— 分别表示迷宫中的房间数量和查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤∣ai∣≤1090 \leq |a_i| \leq 10^9)—— 表示每个房间的初始幻象率。

接下来的 n−1n-1 行中,第 ii 行包含两个整数 sis_i 和 tit_i(1≤si,ti≤n1 \leq s_i, t_i \leq n),表示存在一条连接第 sis_i 个房间与第 tit_i 个房间的隧道。所给的边构成一棵树。

接下来的 qq 行按描述给出查询。所有给定的查询均有效。

输出格式

For each type 22 query, output a line containing an integer — the minimum sum of energy needed for Chanek and Indiana to take the secret treasure.

对于每个类型 22 的查询,输出一行,包含一个整数——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 11-st to the 22-nd room is as follows.

  • 1→51 \rightarrow 5 — takes max⁡(∣10+4∣,∣10−4∣)=14\max(|10 + 4|, |10 - 4|) = 14 energy.
  • 5→65 \rightarrow 6 — takes max⁡(∣4+(−6)∣,∣4−(−6)∣)=10\max(|4 + (-6)|, |4 - (-6)|) = 10 energy.
  • 6→26 \rightarrow 2 — takes max⁡(∣−6+(−9)∣,∣−6−(−9)∣)=15\max(|-6 + (-9)|, |-6 - (-9)|) = 15 energy.

In total, it takes 3939 energy.

In the second query, the illusion rate of the 11-st room changes from 1010 to −3-3.

In the third query, their movement from the 11-st to the 22-nd room is as follows.

  • 1→51 \rightarrow 5 — takes max⁡(∣−3+4∣,∣−3−4∣)=7\max(|-3 + 4|, |-3 - 4|) = 7 energy.
  • 5→65 \rightarrow 6 — takes max⁡(∣4+(−6)∣,∣4−(−6)∣)=10\max(|4 + (-6)|, |4 - (-6)|) = 10 energy.
  • 6→26 \rightarrow 2 — takes max⁡(∣−6+(−9)∣,∣−6−(−9)∣)=15\max(|-6 + (-9)|, |-6 - (-9)|) = 15 energy.

Now, it takes 3232 energy.

在第一次查询中,他们从第 11 个房间移动到第 22 个房间的过程如下:

  • 1→51 \rightarrow 5 — 消耗 max⁡(∣10+4∣,∣10−4∣)=14\max(|10 + 4|, |10 - 4|) = 14 单位能量;
  • 5→65 \rightarrow 6 — 消耗 max⁡(∣4+(−6)∣,∣4−(−6)∣)=10\max(|4 + (-6)|, |4 - (-6)|) = 10 单位能量;
  • 6→26 \rightarrow 2 — 消耗 max⁡(∣−6+(−9)∣,∣−6−(−9)∣)=15\max(|-6 + (-9)|, |-6 - (-9)|) = 15 单位能量。

总计消耗 3939 单位能量。

在第二次查询中,第 11 个房间的幻象系数由 1010 变为 −3-3。

在第三次查询中,他们从第 11 个房间移动到第 22 个房间的过程如下:

  • 1→51 \rightarrow 5 — 消耗 max⁡(∣−3+4∣,∣−3−4∣)=7\max(|-3 + 4|, |-3 - 4|) = 7 单位能量;
  • 5→65 \rightarrow 6 — 消耗 max⁡(∣4+(−6)∣,∣4−(−6)∣)=10\max(|4 + (-6)|, |4 - (-6)|) = 10 单位能量;
  • 6→26 \rightarrow 2 — 消耗 max⁡(∣−6+(−9)∣,∣−6−(−9)∣)=15\max(|-6 + (-9)|, |-6 - (-9)|) = 15 单位能量。

此时,总计消耗 3232 单位能量。

输入解题思路,AI测评打分。不知道怎么写?

首页