Pagini recente » Cod sursa (job #163096) | Cod sursa (job #3513) | Cod sursa (job #1204857) | Cod sursa (job #2280689) | Cod sursa (job #2437948)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[200005], v[200005], v2[200005], poz, n;
void schimb(int nod1, int nod2){
swap(v2[nod1], v2[nod2]);
v[v2[nod1]] = nod1;
v[v2[nod2]] = nod2;
swap(heap[nod1], heap[nod2]);
}
void inserez(int x){
poz++;
n++;
heap[poz] = x;
v[n] = poz;
v2[poz] = n;
int p = poz;
while (heap[p] < heap[p / 2] && p > 0){
schimb(p, p / 2);
p /= 2;
}
}
void sterg(int x){
int nod = v[x];
schimb(nod, poz);
heap[poz] = 0;
poz--;
int p = nod;
while ((heap[p] > heap[2 * p] && heap[2 * p] > 0) || (heap[p] > heap[2 * p + 1] && heap[2 * p + 1] > 0)){
if (heap[2 * p] < heap[2 * p + 1] && heap[2 * p] > 0){
schimb(p, 2 * p);
p = 2 * p;
}
else if (heap[2 * p] >= heap[2 * p + 1] && heap[2 * p + 1] > 0){
schimb(p, 2 * p + 1);
p = 2 * p + 1;
}
}
}
int main()
{
int nr;
fin >> nr;
for (int i = 1; i <= nr; i++){
int cod, x;
fin >> cod;
if (cod == 1){
fin >> x;
inserez(x);
}
else if (cod == 2){
fin >> x;
sterg(x);
}
else
fout << heap[1] << '\n';
}
return 0;
}