Pagini recente » Cod sursa (job #2585086) | Cod sursa (job #481283) | Cod sursa (job #68320) | Cod sursa (job #2772349) | Cod sursa (job #1745907)
#include <iostream>
#include <fstream>
#include <algorithm>
#define nMax 2000001
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout("heapuri.out");
int heap[nMax], nr, v[nMax], pozitie[nMax], k;
//v[i] a i-a valoare inseatain operatie de tip 1;
//heap[i] = poz, unde poz este o pozitie in vectorul v
//v[heap[1]], v[heap[2]], ..., v[heap[k]] are propr unui minheap
//pozitie[i] = pozitia in heap a nodului cu valoarea i
// k = numarul de elemente al vectorlui heap
void upHeap (int nod) {
int tata = nod / 2;
while (nod > 1 && v[heap[nod]] < v[heap[tata]]) {
swap (heap[nod], heap[tata]);
swap (pozitie[heap[nod]], pozitie[heap[tata]]);
nod = tata;
tata = nod / 2;
}
}
void insertHeap(int nod) {
heap[++k] = nod;
pozitie[nod] = k;
upHeap(k);
}
void downHeap (int nod) {
int fiu = 2 * nod;
if (fiu + 1 <= k && v[heap[fiu]] > v[heap[fiu + 1]]) {
fiu++;
}
if (fiu <= k && v[heap[fiu]] < v[heap[nod]]) {
swap (heap[fiu], heap[nod]);
swap (pozitie[heap[fiu]], pozitie[heap[nod]]);
downHeap (fiu);
}
}
void remove (int nod) {
swap (heap[nod], heap[k]);
swap (pozitie[heap[nod]], pozitie[heap[k]]);
k--;
downHeap(nod);
}
int main () {
int n, i, val, op, poz, lgV = 0;
fin >> n;
for (i = 1; i <= n; i++) {
fin >> op;
if (op == 1) {
fin >> val;
v[lgV] = val;
insertHeap(lgV);
lgV++;
}
if (op == 2) {
fin >> poz;
poz--;
remove(pozitie[poz]);
pozitie[poz] = -1;
}
if (op == 3) {
fout << v[heap[1]] << "\n";
}
}
return 0;
}