Allowed swaps

Maintain an array under swaps and union operations, answering whether it can be sorted and counting pairs of clouds whose merge would fix both.

Hard9Union-findImplementationMathSortingNo attempts yetTime limit6sMemory limit512 MB

Problem

Dominik has in mind an array of positive integers p1,,pNp_1, \dots, p_N. Write q1,,qNq_1, \dots, q_N for the same array sorted in non-decreasing order.

He also has in mind a set of allowed swaps. If the pair (X,Y)(X, Y) is in the set of allowed swaps, Dominik can swap the numbers at positions XX and YY of the array pp. The set of allowed swaps is empty at the start.

Marin gives Dominik QQ queries. Each query is one of the following four kinds.

  1. Swap the numbers at positions AA and BB.

  2. Add the pair (A,B)(A, B) to the set of allowed swaps. Marin can give a pair that is already in the set.

  3. Decide whether the array can be sorted using only the allowed swaps. The swaps can be used in any order, and each swap can be used any number of times.

  4. Two positions (A,B)(A, B) are linked if the number at position AA can be moved to position BB using only allowed swaps. The set of all positions linked to position AA is the cloud of AA. A cloud is good if the allowed swaps can be applied so that pj=qjp_j = q_j holds at the same time for every position jj of that cloud.

    Count the pairs of distinct positions (A,B)(A, B) that satisfy all three conditions below.

    • Positions AA and BB are not linked.
    • The cloud of AA is not good and the cloud of BB is not good.
    • Once the pair (A,B)(A, B) is added to the set of allowed swaps, the cloud of AA formed by joining the cloud of AA and the cloud of BB is good.

The pairs (A,B)(A, B) and (B,A)(B, A) count as the same pair.

Input

The first line contains the integers NN and QQ (1N,Q1061 \le N, Q \le 10^6).

The second line contains NN integers p1,,pNp_1, \dots, p_N (1pi1061 \le p_i \le 10^6).

Each of the next QQ lines contains one query.

  • The first number on the line is the query kind TT, one of 1, 2, 3, 4.
  • If TT is 1 or 2, two different integers AA and BB follow (1A,BN1 \le A, B \le N).
  • If TT is 3 or 4, the line contains nothing else.

Output

For each query of kind 3 or 4, print the answer on its own line.

The answer to a query of kind 3 is DA if the array can be sorted and NE if it cannot. These words are Croatian for yes and no, and they are printed without quotation marks.

The answer to a query of kind 4 is the non-negative integer defined in the statement.

Hint

In the first example p=[1,3,2]p = [1, 3, 2] and q=[1,2,3]q = [1, 2, 3]. No swap is allowed at the start, so every position forms a cloud that holds only itself.

The answer to the first query is 1, because (2,3)(2, 3) is the only pair that satisfies all three conditions.

The answer to the second query is NE, because the set of allowed swaps is empty and the numbers 2 and 3 cannot reach their places.

The third query adds the pair (2,3)(2, 3) to the set of allowed swaps.

The answer to the fourth query is 0, because positions 2 and 3 are already linked.

The answer to the fifth query is DA, because one application of the allowed swap (2,3)(2, 3) sorts the array.