#P6498. Swappable Sets and Auspicious Clouds

    ID: 19712 Type: Default 1000ms 256MiB

Swappable Sets and Auspicious Clouds

Swappable Sets and Auspicious Clouds

Dominik has constructed an array \(p_1,p_2,\dots,p_n\) and its sorted version \(q_1,q_2,\dots,q_n\) (sorted in non‐decreasing order). He also defines a swappable set of unordered index pairs. If an unordered pair \((a,b)\) belongs to the swappable set, then he is allowed to swap the elements \(p_a\) and \(p_b\) (and by performing several such swaps, he may rearrange some elements of \(p\)).

The following four types of operations are supported:

  1. Operation 1:
    Format: 1 a b
    Unconditionally swap \(p_a\) and \(p_b\). (This swap does not require \((a,b)\) to be in the swappable set.)
  2. Operation 2:
    Format: 2 a b
    Add the unordered pair \((a,b)\) to the swappable set.
  3. Operation 3:
    Format: 3
    Determine whether it is possible, by using only swaps corresponding to pairs in the swappable set (possibly repeatedly), to rearrange the array \(p\) so that \(p_i=q_i\) for all \(i\). In other words, if we view every allowed swap as an edge connecting indices, then for every connected component, the multiset of \(p\)-values in that component must equal the multiset of target values \(q_i\) for indices in that component.
  4. Operation 4:
    Format: 4
    We say that indices \(x\) and \(y\) are connected if, using the swappable set, the element at position \(x\) in \(p\) can be moved to position \(y\) (note that \(x\) may equal \(y\)). The set of indices connected with \(x\) is called the cloud of \(x\). A cloud is called an auspicious cloud if it is possible (via swaps in the swappable set) to rearrange \(p\) so that every index \(i\) in the cloud satisfies \(p_i=q_i\).</p>

    For Operation 4, you are to count the number of unordered pairs \((a,b)\) satisfying all of the following conditions:

    • \(1 \le a, b \le n\) with \(a \neq b\).
    • Indices \(a\) and \(b\) are not connected (i.e. they belong to different clouds).
    • Both the cloud of \(a\) and the cloud of \(b\) are not auspicious.
    • If the unordered pair \((a,b)\) is added to the swappable set (thus merging the two clouds), then the merged cloud becomes auspicious.

    Your task is to process a sequence of operations on \(p\) and the swappable set.

    inputFormat

    The first line contains two integers \(n\) and \(m\) \((1 \le n, m \le 10^5)\): the number of elements in the array and the number of operations.

    The second line contains \(n\) integers \(p_1,p_2,\dots,p_n\). Let \(q\) be the array obtained by sorting \(p\) in non-decreasing order.

    The next \(m\) lines each represent an operation in one of the following formats:

    • 1 a b — Operation 1.
    • 2 a b — Operation 2.
    • 3 — Operation 3. For this operation, print a line containing either YES or NO.
    • 4 — Operation 4. For this operation, print a line containing the number of unordered pairs \((a,b)\) that satisfy the conditions.

    Indices are 1-indexed. It is guaranteed that the operations are given in a valid format.

    outputFormat

    For each Operation 3, output a line with either YES or NO depending on whether it is possible to rearrange \(p\) (using only swaps from the swappable set) so that \(p_i=q_i\) for all \(i\).

    For each Operation 4, output a line with a single integer — the number of unordered pairs \((a,b)\) (with \(a \neq b\)) satisfying the specified conditions.

    sample

    3 6
    3 1 2
    3
    1 1 3
    3
    2 1 2
    3
    4
    
    NO
    

    NO YES 0

    </p>