Pagini recente » Cod sursa (job #1621448) | Cod sursa (job #2037303) | Cod sursa (job #2234293) | Cod sursa (job #2624466)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n, l, nr, 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;
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);
}
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);
}
if(instr == 3)
fout << v[heap[1]] << "\n";
}
return 0;
}