Pagini recente » Cod sursa (job #2072039) | Cod sursa (job #1600079) | Cod sursa (job #2064738) | Cod sursa (job #628529) | Cod sursa (job #2742826)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream o("heapuri.out");
int k, nod, heap[200001], arb[200001], ind[200001];
void urca(int copil) {
int tata = copil / 2;
if (arb[heap[tata]] > arb[heap[copil]]) {
swap(heap[tata], heap[copil]);
ind[heap[tata]] = tata;
ind[heap[copil]] = copil;
urca(tata);
}
}
void coboara(int tata) {
int fiu_stang = 2 * tata, fiu_drept = 2 * tata + 1;
int copil;
if (tata >= fiu_stang) {
copil = fiu_stang;
if (tata >= fiu_drept && arb[heap[fiu_stang]] > arb[heap[fiu_drept]])
copil = fiu_drept;
if (arb[heap[tata]] > arb[heap[copil]]) {
swap(heap[tata], heap[copil]);
ind[heap[tata]] = tata;
ind[heap[copil]] = copil;
coboara(copil);
}
}
}
void inserare(int x) {
nod++;
k++;
heap[nod] = k;
arb[heap[nod]] = x;
ind[heap[nod]] = nod;
urca(nod);
}
void stergere(int x) {
int copil = ind[x];
swap(heap[copil], heap[nod]);
ind[heap[copil]] = copil;
ind[heap[nod]] = nod;
nod--;
int tata = copil / 2;
if (arb[heap[tata]] < arb[heap[copil]])
coboara(copil);
else
urca(copil);
}
int main() {
int n, i, op, x;
f >> n;
for (i = 0; i < n; i++) {
f >> op;
if (op == 1) {
f >> x;
inserare(x);
} else if (op == 2) {
f >> x;
stergere(x);
} else {
cout << arb[heap[1]] << '\n';
}
}
f.close();
o.close();
return 0;
}