Pagini recente » Cod sursa (job #271383) | Cod sursa (job #2095250) | Cod sursa (job #2921768) | Cod sursa (job #826051) | Cod sursa (job #1325366)
#include <fstream>
#define DIM 20000
using namespace std;
int n,c,x,v[2][DIM],H[DIM],hp,cr;
inline int ls(int k) {
return k*2;
}
inline int rs(int k) {
return k*2+1;
}
inline int father(int k) {
return k/2;
}
int poz(int k)
{
for(int i=1;i<=cr;++i)
if(v[0][i]==k) return i;
}
void percolate(int k)
{
while(H[father(k)] > H[k] && k>1)
{
swap(H[father(k)], H[k]);
int a=poz(H[father(k)]);
int b=poz(H[k]);
swap(v[1][a],v[1][b]);
k=father(k);
}
}
void sift(int k)
{
int son;
do {
son=0;
if(ls(k) <=hp)
{
son=ls(k);
if(rs(k) <=hp && H[rs(k)] < H[ls(k)])
son=rs(k);
if(H[son] >H[k])
son=0;
}
if(son)
{
swap(H[son],H[k]);
int a=poz(H[son]);
int b=poz(H[k]);
swap(v[1][a],v[1][b]);
k=son;
}
}while(son);
}
void addHeap(int k)
{
H[++hp]=k;
percolate(hp);
}
void delHeap(int k)
{
H[k]=H[hp--];
if(k>1 && H[k]<H[father(k)])
percolate(k);
else
sift(k);
}
int main()
{
FILE *f=fopen("heapuri.in","r");
FILE *g=fopen("heapuri.out","w");
fscanf(f,"%d",&n);
for(int i=1;i<=n;++i)
{
fscanf(f,"%d",&c);
if(c==1)
{
fscanf(f,"%d",&x);
v[0][++cr]=x;
v[1][cr]=hp+1;
addHeap(x);
}
else if(c==2)
{
fscanf(f,"%d",&x);
delHeap(v[1][x]);
}
else
fprintf(g,"%d\n", H[1]);
}
fclose(f);
fclose(g);
return 0;
}