Cod sursa(job #3295692)

Utilizator Mariusq17Ignat Marius Florentin Mariusq17 Data 7 mai 2025 20:43:50
Problema Heapuri cu reuniune Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.22 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
using namespace std;

struct TreapNode {
    int key, priority;
    TreapNode *left, *right;
};

TreapNode* rightRotate(TreapNode* y) {
    TreapNode *x = y->left;
    TreapNode *T2 = x->right;
    x->right = y;
    y->left = T2;
    return x;
}

TreapNode* leftRotate(TreapNode* x) {
    TreapNode *y = x->right;
    TreapNode *T2 = y->left;
    y->left = x;
    x->right = T2;
    return y;
}

TreapNode* newNode(int key) {
    TreapNode* temp = new TreapNode;
    temp->key = key;
    temp->priority = rand();
    temp->left = temp->right = nullptr;
    return temp;
}

TreapNode* insert(TreapNode* root, int key) {
    if (root == nullptr) return newNode(key);
    if (key <= root->key) {
        root->left = insert(root->left, key);
        if (root->left->priority > root->priority)
            root = rightRotate(root);
    } else {
        root->right = insert(root->right, key);
        if (root->right->priority > root->priority)
            root = leftRotate(root);
    }
    return root;
}

TreapNode* deleteNode(TreapNode* root, int key) {
    if (!root) return root;
    if (key < root->key) root->left = deleteNode(root->left, key);
    else if (key > root->key) root->right = deleteNode(root->right, key);
    else {
        if (!root->left) {
            TreapNode* temp = root->right;
            delete root;
            return temp;
        } else if (!root->right) {
            TreapNode* temp = root->left;
            delete root;
            return temp;
        } else if (root->left->priority < root->right->priority) {
            root = leftRotate(root);
            root->left = deleteNode(root->left, key);
        } else {
            root = rightRotate(root);
            root->right = deleteNode(root->right, key);
        }
    }
    return root;
}

TreapNode* mergeTreaps(TreapNode* left, TreapNode* right) {
    if (!left) return right;
    if (!right) return left;
    if (left->priority > right->priority) {
        left->right = mergeTreaps(left->right, right);
        return left;
    } else {
        right->left = mergeTreaps(left, right->left);
        return right;
    }
}

// Găsește maximul (cel mai din dreapta nod)
TreapNode* findMax(TreapNode* root) {
    while (root && root->right)
        root = root->right;
    return root;
}

int main() {
    ifstream fin("mergeheap.in");
    ofstream fout("mergeheap.out");

    int N, Q;
    fin >> N >> Q;

    vector<TreapNode*> heap(N + 1, nullptr);

    for (int i = 0; i < Q; ++i) {
        int op;
        fin >> op;
        if (op == 1) {
            int m, x;
            fin >> m >> x;
            heap[m] = insert(heap[m], x);
        } else if (op == 2) {
            int m;
            TreapNode* maxNode = findMax(heap[m]);
            fout << maxNode->key << '\n';
            heap[m] = deleteNode(heap[m], maxNode->key);
        } else if (op == 3) {
            int a, b;
            fin >> a >> b;
            if (a == b) continue;
            heap[a] = mergeTreaps(heap[a], heap[b]);
            heap[b] = nullptr;
        }
    }

    fin.close();
    fout.close();
    return 0;
}