Cod sursa(job #3039575)

Utilizator lolismekAlex Jerpelea lolismek Data 28 martie 2023 18:04:09
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <queue>

#include <iomanip>

using namespace std;

string filename = "mergeheap";

#ifdef LOCAL
    ifstream fin("input.in");
    ofstream fout("output.out");
#else
    ifstream fin(filename + ".in");
    ofstream fout(filename + ".out");
#endif

const int NMAX = 100;

priority_queue <int> pq[NMAX + 1];

signed main(){

    int n, q;
    fin >> n >> q;

    while(q--){
        int t;
        fin >> t;

        if(t == 1){
            int m, x;
            fin >> m >> x;
            pq[m].push(x);
        }else if(t == 2){
            int m;
            fin >> m;
            fout << pq[m].top() << '\n';
            pq[m].pop();
        }else{
            int a, b;
            fin >> a >> b;

            swap(a, b);

            if((int)pq[a].size() > (int)pq[b].size()){
                pq[a].swap(pq[b]);
            }

            while(!pq[a].empty()){
                pq[b].push(pq[a].top());
                pq[a].pop();
            }
        }
    }

    return 0;
}