Cod sursa(job #3165083)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 5 noiembrie 2023 13:37:35
Problema Heapuri cu reuniune Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <fstream>
#include <queue>

using namespace std;

/// Varianta 2: Small to Large pe heap-uri normale (binare)

const int NMAX = 1e2;
const int QMAX = 1e5;

priority_queue<int> pq[1 + NMAX];
int index[1 + NMAX];

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

    ios_base::sync_with_stdio(false);
    in.tie(nullptr);

    int n, q;
    in >> n >> q;

    for (int i = 1; i <= n; ++i)
    {
        index[i] = i;
    }

    for (int i = 1; i <= q; ++i)
    {
        int tip;
        in >> tip;

        if (tip == 1)
        {
            int m, x;
            in >> m >> x;

            pq[index[m]].emplace(x);
        }
        else if (tip == 2)
        {
            int m;
            in >> m;

            out << pq[index[m]].top() << '\n';
            pq[index[m]].pop();
        }
        else
        {
            int a, b;
            in >> a >> b;

            if (pq[index[a]].size() > pq[index[b]].size())
            {
                while (!pq[index[b]].empty())
                {
                    pq[index[a]].emplace(pq[index[b]].top());
                    pq[index[b]].pop();
                }
            }
            else
            {
                while (!pq[index[a]].empty())
                {
                    pq[index[b]].emplace(pq[index[a]].top());
                    pq[index[a]].pop();
                }
                index[a] = index[b];
            }
        }
    }

    in.close();
    out.close();

    return 0;
}