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 n nodes, rooted at 1.
Luo Tianyi will tell you that the parent of the i-th node is ai (1≤ai<i for 2≤i≤n), and she will ask you to perform q queries of 2 types:
- She'll give you three integers l, r and x (2≤l≤r≤n, 1≤x≤105). You need to replace ai with max(ai−x,1) for all i with l≤i≤r.
- She'll give you two integers u, v (1≤u,v≤n). You need to find the LCA of nodes u and v (their lowest common ancestor).
岳正灵送给洛天依一棵有 n 个节点的树,根节点为 1。
洛天依会告诉你第 i 个节点的父节点是 ai(其中对 2≤i≤n,满足 1≤ai<i),并要求你执行 q 次查询,查询分为两类:
- 她会给出三个整数 l、r 和 x(满足 2≤l≤r≤n,1≤x≤105)。你需要对所有满足 l≤i≤r 的 i,将 ai 替换为 max(ai−x,1)。
- 她会给出两个整数 u、v(满足 1≤u,v≤n)。你需要求出节点 u 和 v 的 LCA(即它们的最近公共祖先)。
输入格式
The first line contains two integers n and q (2≤n,q≤105) — the number of nodes and the number of queries, respectively.
The second line contains n−1 integers a2,a3,…,an (1≤ai<i), where ai is the parent of the node i.
Next q lines contain queries. For each query, the first integer of each line is t (t=1 or 2) — the type of the query.
If t=1, this represents the query of the first type. Then, three integers will follow: l, r, x (2≤l≤r≤n, 1≤x≤105), meaning that you have to replace ai with max(ai−x,1) for all i with l≤i≤r.
If t=2, this represents the query of the second type. Then, two integers will follow: u and v (1≤u,v≤n), and you have to find the LCA of u and v.
It's guaranteed that there is at least one query of the second type.
第一行包含两个整数 n 和 q(2≤n,q≤105),分别表示节点数量和查询数量。
第二行包含 n−1 个整数 a2,a3,…,an(1≤ai<i),其中 ai 表示节点 i 的父节点。
接下来 q 行为查询。每行查询以一个整数 t(t=1 或 2)开头,表示查询类型。
若 t=1,表示第一类查询。随后将给出三个整数:l、r、x(2≤l≤r≤n,1≤x≤105),表示对所有满足 l≤i≤r 的 i,将 ai 替换为 max(ai−x,1)。
若 t=2,表示第二类查询。随后将给出两个整数:u 和 v(1≤u,v≤n),要求计算 u 与 v 的最近公共祖先(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测评打分。不知道怎么写?