d

· · 个人记录

To solve this problem, we need to efficiently handle three types of operations on pigeons and nests while ensuring optimal performance even for large input sizes. The operations include moving a pigeon, swapping the contents of two nests, and querying the current nest of a pigeon.

Approach Data Structures:

pos Array: Tracks the current real nest of each pigeon.

rev Array: Maps each nest label to its corresponding real nest.

label Array: Maps each real nest to its current label.

Initialization:

Initially, each pigeon is in the nest corresponding to its label. Thus, pos[i], rev[i], and label[i] are all initialized to i.

Operations:

Type 1 (Move): Update the pigeon's real nest using the rev array to find the current real nest of the target label.

Type 2 (Swap): Swap the real nests corresponding to the given labels and adjust their mappings in the rev and label arrays.

Type 3 (Query): Use the pos array to find the real nest of the pigeon and then map it to the current label using the label array. Explanation Type 1 (Move): When moving a pigeon a to nest b, the target nest b is mapped to its current real nest using rev[b], and the pigeon's position is updated to this real nest.

Type 2 (Swap): Swapping nests involves exchanging their real nest mappings in rev and updating their labels in label to reflect the new mappings.

Type 3 (Query): The current nest of a pigeon is determined by first finding its real nest from pos and then converting this real nest to its current label using label.

This approach ensures that all operations are handled in constant time, making the solution efficient even for the upper limits of the input constraints.


#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;

    // Initialize pos: pos[a] is the real nest of pigeon a
    vector<int> pos(N + 1);
    for (int i = 1; i <= N; ++i) {
        pos[i] = i;
    }

    // rev[x] is the real nest corresponding to label x
    vector<int> rev(N + 1);
    for (int i = 1; i <= N; ++i) {
        rev[i] = i;
    }

    // label[r] is the current label of real nest r
    vector<int> label(N + 1);
    for (int i = 1; i <= N; ++i) {
        label[i] = i;
    }

    vector<int> output;
    for (int i = 0; i < Q; ++i) {
        int op;
        cin >> op;
        if (op == 1) {
            int a, b;
            cin >> a >> b;
            pos[a] = rev[b];
        } else if (op == 2) {
            int a, b;
            cin >> a >> b;
            // Swap rev[a] and rev[b]
            int old_rev_a = rev[a];
            int old_rev_b = rev[b];
            rev[a] = old_rev_b;
            rev[b] = old_rev_a;
            // Swap labels for the old real nests
            swap(label[old_rev_a], label[old_rev_b]);
        } else if (op == 3) {
            int a;
            cin >> a;
            output.push_back(label[pos[a]]);
        }
    }

    for (int res : output) {
        cout << res << '\n';
    }

    return 0;
}
```cpp