Cod sursa(job #2918016)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 9 august 2022 12:01:37
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>

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

const int NMAX = 100;

int n, q;
__gnu_pbds :: priority_queue<int, less<int>, __gnu_pbds :: pairing_heap_tag> pq[NMAX + 1];

int main() {
	ios_base :: sync_with_stdio(0); fin.tie(0); fout.tie(0);
	fin >> n >> q;

	for(int i = 1; i <= q; i++) {
		int task;
		fin >> task;

		if(task == 1) {
			int m, x;
			fin >> m >> x;

			pq[m].push(x);
		} else if(task == 2) {
			int m;
			fin >> m;

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

			// if(pq[a].size() < pq[b].size()) {
			// 	pq[a].swap(pq[b]);
			// }

			pq[a].join(pq[b]);
			pq[b].clear();
		}
	}
	return 0;
}