#P6498. [COCI 2016/2017 #2] Zamjene

[COCI 2016/2017 #2] Zamjene

Description

Dominik builds an array p1,p2,,pnp_1,p_2,\dots,p_n with nn elements, and the array q1,q2,,qnq_1,q_2,\dots,q_n obtained by sorting it.

He also defines a “swappable set”. If an unordered pair (a,b)(a,b) belongs to the “swappable set”, then he can swap the positions of pap_a and pbp_b. “Using the swappable set” means performing a number of such swaps.

There are four operations:

  • Operation 11:

    Format: 1 a b.

    Swap pap_a and pbp_b (not restricted by the “swappable set”).

  • Operation 22:

    Format: 2 a b.

    Add the unordered pair (a,b)(a,b) to the “swappable set”.

  • Operation 33:

    Format: 3.

    Determine whether the array pp can be sorted using the “swappable set”.

  • Operation 44:

    Format: 4.

    If the xx-th element in array pp can be moved to position yy using the “swappable set”, then xx and yy are said to be connected. Here xx may be equal to yy.

    The set of all yy connected to xx is called the “cloud” of xx. If a “cloud” can use the “swappable set” to make every ii in the “cloud” satisfy pi=qip_i=q_i, then this “cloud” is called an “auspicious cloud”.

    Compute how many unordered pairs (a,b)(a,b) satisfy:

    • 1a,bn1\le a,b\le n and aba\not=b.
    • aa and bb are not connected.
    • Neither the “cloud” of aa nor the “cloud” of bb is an “auspicious cloud”.
    • After adding the unordered pair (a,b)(a,b) to the “swappable set”, the “cloud” of aa becomes an “auspicious cloud”.

Please help Dominik complete these operations.

Input Format

The first line contains two integers n,qn,q, representing the number of elements in array pp and the number of operations.

The second line contains nn integers pip_i.

The next qq lines are given in the following format:

  1. An integer tt, representing the type of the operation.

  2. If tt is 11 or 22, then two distinct integers a,ba,b follow.

Output Format

  • For each operation 33:

    If the array pp can be sorted using the “swappable set”, output one line DA.

    Otherwise, output one line NE.

  • For each operation 44:

    Output one line with an integer, representing the number of unordered pairs (a,b)(a,b) that satisfy the conditions.

3 5
1 3 2
4
3
2 2 3
4
3 
1
NE
0
DA 
5 5
4 2 1 4 4
3
4
1 1 3
3
4 
NE
1
DA
0 
4 10
2 1 4 3
3
4
1 1 2
3
4
2 2 3
2 1 2
4
2 3 4
3 
NE
2
NE
1
3
DA 

Hint

Sample 1 Explanation

  • The first operation: only the unordered pair (2,3)(2,3) satisfies the requirement.
  • The second operation: the array pp cannot be sorted using the “swappable set”.
  • The third operation: add the unordered pair (2,3)(2,3) to the “swappable set”.
  • The fourth operation: there is no unordered pair that satisfies the requirement.
  • The fifth operation: swap p2p_2 and p3p_3, and then the array pp can be sorted using the “swappable set”.

Constraints

For 100%100\% of the data, 1n,q1061\le n,q\le 10^6, 1pi1061\le p_i\le 10^6, 1t41\le t\le 4, 1a,bn1\le a,b\le n and aba\not=b.


Notes

This problem is translated from COCI2016-2017 CONTEST #2 T5 Zamjene.

Translated by ChatGPT 5