Pagini recente » Cod sursa (job #1322712) | Cod sursa (job #1264245) | Cod sursa (job #2196913) | Cod sursa (job #2976969) | Cod sursa (job #1920850)
#include <stdio.h>
using namespace std;
#define N 200001
FILE *fin = fopen("heapuri.in", "r");
FILE *fout = fopen("heapuri.out", "w");
int h[N], poz[N];
int v[N];
int sizeh;
void schimb(int p, int q)
{
int aux = h[p];
h[p] = h[q];
h[q] = aux;
poz[h[p]] = p;
poz[h[q]] = q;
}
void urca(int p)
{
while(p>1 && v[h[p]] < v[h[p/2]])
{
schimb(p,p/2);
p/=2;
}
}
void coboara(int p)
{
int fs = 2*p;
int fd = 2*p+1;
int bun = p;
if(fs<=sizeh && v[h[fs]] < v[h[bun]])
{
bun = fs;
}
if(fd<=sizeh && v[h[fd]] < v[h[bun]])
{
bun = fd;
}
if(bun != p)
{
schimb(p,bun);
coboara(bun);
}
}
void adaug(int pv)
{
h[++sizeh] = pv;
poz[pv] = sizeh;
urca(sizeh);
}
void sterge(int pv)
{
int p = poz[pv];
schimb(p , sizeh--);
urca(p);
coboara(p);
}
int main()
{
int n;
fscanf(fin, "%d", &n);
sizeh=0;
for(int i=1;i<=n;i++)
{
int tip;
fscanf(fin, "%d", &tip);
if(tip == 1)
{
int x;
fscanf(fin ,"%d", &v[i]);
adaug(i);
}
if(tip == 2)
{
int x;
fscanf(fin ,"%d", &x);
sterge(x);
}
if(tip == 3)
{
fprintf(fout, "%d\n", v[h[1]]);
}
}
return 0;
}