CF1083C.Max Mex

普及/提高-

通过率:0%

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 n1n - 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 .

输入格式

The first line contains a single integer nn ( 2n21052 \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 ( 0pi<n0\leq p_i < n ) — the permutation pp , it's guaranteed that all numbers are different .

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

The fourth line contains a single integer qq ( 1q21051 \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 ( 1i,jn1 \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 .

输出格式

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

输入输出样例

  • 输入#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 .

首页