Pagini recente » Cod sursa (job #2659411) | Cod sursa (job #2130105) | Utilizatori inregistrati la Grigore Moisil 2010 - clasele 11-12 | Istoria paginii runda/jc2015-runda2 | Cod sursa (job #3278699)
package org.example;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(Files.newInputStream(Path.of("mergeheap.in")));
PrintWriter pw = new PrintWriter("mergeheap.out");
pw.write("tralala\n");
try {
List<PriorityQueue<Integer>> pq = new ArrayList<>();
int N = scan.nextInt();
for (int i = 0; i <= N; i++) {
pq.add(new PriorityQueue<>(Collections.reverseOrder()));
}
int Q = scan.nextInt();
for (int i = 0; i < Q; ++i) {
int t = scan.nextInt();
if (t == 1) {
int m = scan.nextInt();
int x = scan.nextInt();
pq.get(m).add(x);
} else if (t == 2) {
int m = scan.nextInt();
pw.write(pq.get(m).poll() + "\n");
} else if (t == 3) {
int a = scan.nextInt();
int b = scan.nextInt();
if (pq.get(a).size() < pq.get(b).size()) {
Collections.swap(pq, a, b);
}
while (!pq.get(b).isEmpty()) {
pq.get(a).add(pq.get(b).poll());
pq.get(b).poll();
}
}
}
} finally {
pw.close();
scan.close();
}
}
}