CF1083C.Max Mex

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Once Grisha found a tree (connected graph without cycles) with a root in node 11.

But this tree was not just a tree. A permutation pp of integers from 00 to n−1n - 1 is written in nodes, a number pip_i is written in node ii.

As Grisha likes to invent some strange and interesting problems for himself, but not always can solve them, you need to help him deal with two types of queries on this tree.

Let's define a function MEX(S)MEX(S), where SS is a set of non-negative integers, as a smallest non-negative integer that is not included in this set.

Let ll be a simple path in this tree. So let's define indices of nodes which lie on ll as u1u_1, u2u_2, …\ldots, uku_k.

Define V(l)V(l) as a set {pu1p_{u_1}, pu2p_{u_2}, …\ldots , pukp_{u_k}}.

Then queries are:

  1. For two nodes ii and jj, swap pip_i and pjp_j.
  2. Find the maximum value of MEX(V(l))MEX(V(l)) in all possible ll.

格里沙曾发现一棵以节点 11 为根的树(即无环连通图)。

但这棵树并不仅仅是一棵普通的树:每个节点上写有一个 00 到 n−1n-1 的排列 pp,其中节点 ii 上写的数为 pip_i。

由于格里沙喜欢为自己构思一些奇怪而有趣的问题,却并不总能自行解决,因此你需要帮助他处理该树上的两类查询。

我们定义函数 MEX(S)MEX(S),其中 SS 是一个非负整数集合,其值为不属于该集合的最小非负整数。

设 ll 是该树中的一条简单路径。记路径 ll 上的节点下标依次为 u1,u2,…,uku_1, u_2, \ldots, u_k。

定义 V(l)V(l) 为集合 {pu1,pu2,…,puk}\{p_{u_1}, p_{u_2}, \ldots, p_{u_k}\}。

则查询类型如下:

  1. 给定两个节点 ii 和 jj,交换 pip_i 与 pjp_j 的值;
  2. 在所有可能的路径 ll 中,求 MEX(V(l))MEX(V(l)) 的最大值。

输入格式

The first line contains a single integer nn (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5) — the number of nodes of a tree.

The second line contains nn integers — p1p_1, p2p_2, …\ldots, pnp_n (0≤pi<n0\leq p_i \lt n) — the permutation pp, it's guaranteed that all numbers are different .

The third line contains n−1n - 1 integers — d2d_2, d3d_3, …\ldots, dnd_n (1≤di<i1 \leq d_i \lt i), where did_i is a direct ancestor of node ii in a tree.

The fourth line contains a single integer qq (1≤q≤2⋅1051 \leq q \leq 2 \cdot 10^5) — the number of queries.

The following qq lines contain the description of queries:

At the beginning of each of next qq lines, there is a single integer tt (11 or 22) — the type of a query:

  1. If t=1t = 1, the line also contains two integers ii and jj (1≤i,j≤n1 \leq i, j \leq n) — the indices of nodes, where values of the permutation should be swapped.
  2. If t=2t = 2, you need to find the maximum value of MEX(V(l))MEX(V(l)) in all possible ll.

第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)——树的节点数量。

第二行包含 nn 个整数:p1p_1, p2p_2, …\ldots, pnp_n(0≤pi<n0\leq p_i \lt n)——排列 pp,保证所有数值互不相同。

第三行包含 n−1n - 1 个整数:d2d_2, d3d_3, …\ldots, dnd_n(1≤di<i1 \leq d_i \lt i),其中 did_i 表示节点 ii 在树中的直接祖先。

第四行包含一个整数 qq(1≤q≤2⋅1051 \leq q \leq 2 \cdot 10^5)——查询的数量。

接下来的 qq 行描述了各次查询:

在接下来的每行开头,有一个整数 tt(11 或 22)——查询类型:

  1. 若 t=1t = 1,该行还包含两个整数 ii 和 jj(1≤i,j≤n1 \leq i, j \leq n)——需交换排列中对应节点 ii 和 jj 处的值。
  2. 若 t=2t = 2,你需要求出所有可能的 ll 中 MEX(V(l))MEX(V(l)) 的最大值。

输出格式

For each type 2 query print a single integer — the answer for this query.

对于每个类型 2 的查询,输出一个整数——该查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    3
    2
  • 输入#2

    6
    5 2 1 4 3 0
    1 1 1 3 3
    9
    2
    1 5 3
    2
    1 6 1
    2
    1 4 2
    2
    1 1 6
    2

    输出#2

    3
    2
    4
    4
    2

说明/提示

Number written in brackets is a permutation value of a node.

In the first example, for the first query, optimal path is a path from node 11 to node 55. For it, set of values is 0,1,2{0, 1, 2} and MEXMEX is 33.

For the third query, optimal path is a path from node 55 to node 66. For it, set of values is 0,1,4{0, 1, 4} and MEXMEX is 22.

In the second example, for the first query, optimal path is a path from node 22 to node 66. For it, set of values is 0,1,2,5{0, 1, 2, 5} and MEXMEX is 33.

For the third query, optimal path is a path from node 55 to node 66. For it, set of values is 0,1,3{0, 1, 3} and MEXMEX is 22.

For the fifth query, optimal path is a path from node 55 to node 22. For it, set of values is 0,1,2,3{0, 1, 2, 3} and MEXMEX is 44.

For the seventh query, optimal path is a path from node 55 to node 44. For it, set of values is 0,1,2,3{0, 1, 2, 3} and MEXMEX is 44.

For the ninth query, optimal path is a path from node 66 to node 55. For it, set of values is 0,1,3{0, 1, 3} and MEXMEX is 22.

括号中写的数字是节点的排列值。

在第一个样例中,对于第一次查询,最优路径是从节点 11 到节点 55 的路径。该路径上节点值的集合为 {0,1,2}\{0, 1, 2\},其 MEXMEX 值为 33。

对于第三次查询,最优路径是从节点 55 到节点 66 的路径。该路径上节点值的集合为 {0,1,4}\{0, 1, 4\},其 MEXMEX 值为 22。

在第二个样例中,对于第一次查询,最优路径是从节点 22 到节点 66 的路径。该路径上节点值的集合为 {0,1,2,5}\{0, 1, 2, 5\},其 MEXMEX 值为 33。

对于第三次查询,最优路径是从节点 55 到节点 66 的路径。该路径上节点值的集合为 {0,1,3}\{0, 1, 3\},其 MEXMEX 值为 22。

对于第五次查询,最优路径是从节点 55 到节点 22 的路径。该路径上节点值的集合为 {0,1,2,3}\{0, 1, 2, 3\},其 MEXMEX 值为 44。

对于第七次查询,最优路径是从节点 55 到节点 44 的路径。该路径上节点值的集合为 {0,1,2,3}\{0, 1, 2, 3\},其 MEXMEX 值为 44。

对于第九次查询,最优路径是从节点 66 到节点 55 的路径。该路径上节点值的集合为 {0,1,3}\{0, 1, 3\},其 MEXMEX 值为 22。

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

首页