Pagini recente » Cod sursa (job #1950810) | Cod sursa (job #691557) | Cod sursa (job #786184) | Cod sursa (job #2125073) | Cod sursa (job #2624471)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n, l = 0, nr = 0, v[1000000], heap[1000000], poz[1000000];
void adauga(int x) {
int aux;
while (x / 2 && v[heap[x]] < v[heap[x / 2]]) {
aux = heap[x];
heap[x] = heap[x / 2];
heap[x / 2] = aux;
poz[heap[x]] = x;
poz[heap[x / 2]] = x / 2;
x /= 2;
}
}
void sterge(int x) {
int aux, y = 0;
while (x != y) {
y = x;
if (y * 2 <= l && v[heap[x]] > v[heap[y * 2]])
x = y * 2;
if (y * 2 + 1 <= l && v[heap[x]] > v[heap[y * 2 + 1]])
x = y * 2 + 1;
aux = heap[x];
heap[x] = heap[y];
heap[y] = aux;
poz[heap[x]] = x;
poz[heap[y]] = y;
}
}
int main() {
int x, i, instr;
fin >> n;
for (i = 1; i <= n; ++i) {
fin >> instr;
if (instr < 3)
fin >> x;
if (instr == 1) {
l++;
nr++;
v[nr] = x;
heap[l] = nr;
poz[nr] = l;
adauga(l);
} else if (instr == 2) {
v[x] = -1;
adauga(poz[x]);
poz[heap[1]] = 0;
heap[1] = heap[l--];
poz[heap[1]] = 1;
if (l > 1)
sterge(1);
} else if (instr == 3)
fout << v[heap[1]] << "\n";
}
return 0;
}