Cod sursa(job #3039778)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 28 martie 2023 20:57:41
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
/// Preset de infoarena
#include <fstream>
#include <iostream>
#include <queue>

using namespace std;

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

const int maxN = 105;
int n, q;
priority_queue <int> heap[maxN];

int main()
{
    fin >> n >> q;
    while(q--)
    {
        int op;
        fin >> op;
        if(op == 1)
        {
            int m, x;
            fin >> m >> x;
            heap[m].push(x);
        }
        if(op == 2)
        {
            int m;
            fin >> m;
            fout << heap[m].top() << '\n';
            heap[m].pop();
        }
        if(op == 3)
        {
            int m1, m2;
            fin >> m1 >> m2;
            if(heap[m1].size() < heap[m2].size())
                heap[m1].swap(heap[m2]);
            while(!heap[m2].empty())
            {
                int x = heap[m2].top();
                heap[m2].pop();
                heap[m1].push(x);
            }
        }
    }
    return 0;
}