Pagini recente » Cod sursa (job #2682655) | Cod sursa (job #1032048) | Cod sursa (job #2953391) | Cod sursa (job #1081318) | Cod sursa (job #1113071)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N = 200001;
int v[N], poz[N], h[N], nh;
void schimba(int p1, int p2){
int aux;
aux = h[p1];
h[p1] = h[p2];
h[p2] = aux;
poz[h[p2]] = p2;
poz[h[p1]] = p1;
}
void urca(int p){
while(p != 1 && v[h[p]] < v[h[p/2]]){
schimba(p, p/2);
p /= 2;
}
}
void coboara(int p){
int fs = 2*p, fd = 2*p+1, bun = p;
if(fs <= nh && v[h[fs]] < v[h[bun]])
bun = fs;
if(fd <= nh && v[h[fd]] < v[h[bun]])
bun = fd;
if(bun != p){
schimba(p, bun);
coboara(bun);
}
}
void sterge(int p){
schimba(p,nh--);
urca(p);
coboara(p);
}
void adauga(int pozitie){
h[++nh] = pozitie;
poz[pozitie] = nh;
urca(nh);
}
int main()
{
int n, i, tip, val, citit = 0;
in >> n;
for(i = 1; i <= n; i++){
in >> tip;
if(tip == 1){
in >> val;
v[++citit] = val;
adauga(citit);
} else {
if(tip == 2){
in >> val;
sterge(poz[val]);
} else {
out << v[h[1]] << "\n";
}
}
}
return 0;
}