CF1491H.Yuezheng Ling and Dynamic Tree

NOI/NOI+/CTSC

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Yuezheng Ling gives Luo Tianyi a tree which has nn nodes, rooted at 11.

Luo Tianyi will tell you that the parent of the ii-th node is aia_i (1≤ai<i1 \leq a_i \lt i for 2≤i≤n2 \le i \le n), and she will ask you to perform qq queries of 22 types:

  1. She'll give you three integers ll, rr and xx (2≤l≤r≤n2 \le l \le r \le n, 1≤x≤1051 \le x \le 10^5). You need to replace aia_i with max⁡(ai−x,1)\max(a_i-x,1) for all ii with l≤i≤rl \leq i \leq r.
  2. She'll give you two integers uu, vv (1≤u,v≤n1 \le u, v \le n). You need to find the LCA of nodes uu and vv (their lowest common ancestor).

岳正灵送给洛天依一棵有 nn 个节点的树,根节点为 11。

洛天依会告诉你第 ii 个节点的父节点是 aia_i(其中对 2≤i≤n2 \le i \le n,满足 1≤ai<i1 \leq a_i < i),并要求你执行 qq 次查询,查询分为两类:

  1. 她会给出三个整数 ll、rr 和 xx(满足 2≤l≤r≤n2 \le l \le r \le n,1≤x≤1051 \le x \le 10^5)。你需要对所有满足 l≤i≤rl \leq i \leq r 的 ii,将 aia_i 替换为 max⁡(ai−x, 1)\max(a_i - x,\, 1)。
  2. 她会给出两个整数 uu、vv(满足 1≤u,v≤n1 \le u, v \le n)。你需要求出节点 uu 和 vv 的 LCA(即它们的最近公共祖先)。

输入格式

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

The second line contains n−1n-1 integers a2,a3,…,ana_2, a_3,\dots, a_n (1≤ai<i1 \le a_i \lt i), where aia_i is the parent of the node ii.

Next qq lines contain queries. For each query, the first integer of each line is tt (t=1t = 1 or 22) — the type of the query.

If t=1t = 1, this represents the query of the first type. Then, three integers will follow: ll, rr, xx (2≤l≤r≤n2 \le l \le r \le n, 1≤x≤1051 \le x \le 10^5), meaning that you have to replace aia_i with max⁡(ai−x,1)\max(a_i-x,1) for all ii with l≤i≤rl \leq i \leq r.

If t=2t = 2, this represents the query of the second type. Then, two integers will follow: uu and vv (1≤u,v≤n1 \le u, v \le n), and you have to find the LCA of uu and vv.

It's guaranteed that there is at least one query of the second type.

第一行包含两个整数 nn 和 qq(2≤n,q≤1052\leq n,q \leq 10^5),分别表示节点数量和查询数量。

第二行包含 n−1n-1 个整数 a2,a3,…,ana_2, a_3,\dots, a_n(1≤ai<i1 \le a_i \lt i),其中 aia_i 表示节点 ii 的父节点。

接下来 qq 行为查询。每行查询以一个整数 tt(t=1t = 1 或 22)开头,表示查询类型。

若 t=1t = 1,表示第一类查询。随后将给出三个整数:ll、rr、xx(2≤l≤r≤n2 \le l \le r \le n,1≤x≤1051 \le x \le 10^5),表示对所有满足 l≤i≤rl \leq i \leq r 的 ii,将 aia_i 替换为 max⁡(ai−x,1)\max(a_i-x,1)。

若 t=2t = 2,表示第二类查询。随后将给出两个整数:uu 和 vv(1≤u,v≤n1 \le u, v \le n),要求计算 uu 与 vv 的最近公共祖先(LCA)。

保证至少存在一个第二类查询。

输出格式

For each query of the second type output answer on a new line.

对于每个第二类查询,在新行中输出答案。

输入输出样例

  • 输入#1

    6 4
    1 2 3 3 4
    2 3 4
    1 2 3 1
    2 5 6
    2 2 3

    输出#1

    3
    3
    1

说明/提示

The tree in example is shown below.

After the query of the first type, the tree changes and is looking as shown below.

示例中的树如下所示。

执行第一类查询后,树发生变化,结果如下图所示。

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

首页