Pagini recente » Cod sursa (job #1139115) | Cod sursa (job #2416024) | Cod sursa (job #362408) | Cod sursa (job #609019) | Cod sursa (job #948603)
Cod sursa(job #948603)
#include<fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int MAXN = 200010;
int n, x, y, nh, h[MAXN], v[MAXN], poz[MAXN], l;
void sterge();
void coboara();
void schimba(int p, int q)
{
int aux;
aux = h[p];
h[p] = h[q];
h[q] = aux;
poz[h[p]] = p;
poz[h[q]] = q;
}
void urca(int);
void coboara(int);
void adauga(int x)
{
h[++nh] = x;
poz[x] = nh;
urca(nh);
}
void sterge(int x)
{
schimba(x, nh--);
urca(x);
coboara(x);
}
void urca(int p)
{
if(p==1 || v[h[p/2]]<=v[h[p]])
return;
schimba(p, p/2);
urca(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(bun, p);
coboara(bun);
}
}
int main()
{
int i;
fin >> n;
for(i=0; i<n; ++i){
fin >> x;
if(x == 1){
fin >> y;
v[++l] = y;
adauga(l);
}
if(x == 2){
fin >> y;
sterge(poz[y]);
}
if(x == 3){
fout << v[h[1]] << "\n";
}
}
fin.close();
fout.close();
return 0;
}