CF 526F Max Mex(倍增求LCA+线段树路径合并)

Max Mex
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

But this tree was not just a tree. A permutation p of integers from 0 to n1 is written in nodes, a number pi is written in node i.

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), where S is a set of non-negative integers, as a smallest non-negative integer that is not included in this set.

Let l be a simple path in this tree. So let's define indices of nodes which lie on l as u1u2uk.

Define V(l) as a set {pu1pu2 , puk}.

Then queries are:

  1. For two nodes i and j, swap pi and pj.
  2. Find the maximum value of MEX(V(l)) in all possible l.
Input

The first line contains a single integer n (2n2105) — the number of nodes of a tree.

The second line contains n integers — p1p2pn (0pi<n) — the permutation p, it's guaranteed that all numbers are different .

The third line contains n1 integers — d2d3dn (1di<i), where di is a direct ancestor of node i in a tree.

The fourth line contains a single integer q (1q2105) — the number of queries.

The following q lines contain the description of queries:

At the beginning of each of next q lines, there is a single integer t (1 or 2) — the type of a query:

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

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

Examples
input
Copy
6
2 5 0 3 1 4
1 1 3 3 3
3
2
1 6 3
2
output
Copy
3
2
input
Copy
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
output
Copy
3
2
4
4
2

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

CF 526F Max Mex(倍增求LCA+线段树路径合并)
In the first example, for the first query, optimal path is a path from node 1 to node 5. For it, set of values is {0,1,2} and MEX is 3.
CF 526F Max Mex(倍增求LCA+线段树路径合并)
For the third query, optimal path is a path from node 5 to node 6. For it, set of values is {0,1,4} and MEX is 2.
CF 526F Max Mex(倍增求LCA+线段树路径合并)
In the second example, for the first query, optimal path is a path from node 2 to node 6. For it, set of values is {0,1,2,5} and MEX is 3.
CF 526F Max Mex(倍增求LCA+线段树路径合并)
For the third query, optimal path is a path from node 5 to node 6. For it, set of values is {0,1,3} and MEX is 2.
CF 526F Max Mex(倍增求LCA+线段树路径合并)
For the fifth query, optimal path is a path from node 5 to node 2. For it, set of values is {0,1,2,3} and MEX is 4.
CF 526F Max Mex(倍增求LCA+线段树路径合并)

相关文章:

  • 2021-05-20
  • 2021-04-28
  • 2022-12-23
  • 2022-01-11
  • 2021-05-24
  • 2022-01-28
  • 2021-07-26
  • 2021-12-20
猜你喜欢
  • 2021-12-10
  • 2021-05-18
  • 2021-12-10
  • 2021-07-16
  • 2022-01-10
  • 2021-09-02
相关资源
相似解决方案