Pagini recente » Cod sursa (job #26695) | Cod sursa (job #2679490) | Cod sursa (job #2745713) | Cod sursa (job #650558) | Cod sursa (job #3165083)
#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;
}