Pagini recente » Cod sursa (job #163860) | Cod sursa (job #2819618) | Cod sursa (job #2114688) | Cod sursa (job #2901708) | Cod sursa (job #1575456)
#include <iostream>
#include <fstream>
#define maxSize 200002
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int q,n,nr;
int H[maxSize], poz[maxSize], ord[maxSize];
void push(int x)
{
poz[++nr]=++n;
ord[n]=nr;
H[n]=x;
int t=n/2, k=n;
while (k&&H[k]<H[t])
{
swap(H[k],H[t]);
swap(ord[k],ord[t]);
swap(poz[ord[k]],poz[ord[t]]);
k/=2;
t/=2;
}
}
void del(int x)
{
int k=poz[x];
H[k]=H[n];
ord[k]=ord[n];
poz[ord[n]]=k;
--n;
int fiu;
do {
fiu=0;
if (2*k<=n && H[k]>H[2*k]) fiu=2*k;
if(2*k+1<=n && H[2*k]>H[2*k+1]) fiu=2*k+1;
if(H[fiu]>=H[k]) fiu=0;
if (fiu)
{
swap(H[fiu],H[k]);
swap(ord[fiu],ord[k]);
swap(poz[ord[fiu]],poz[ord[k]]);
k=fiu;
}
} while(fiu);
}
int main()
{
fin>>q;
int cod,val;
while (q--)
{
fin>>cod;
switch (cod)
{
case 1 :
fin>>val;
push(val);
break;
case 2 :
fin>>val;
del(val);
break;
case 3 :
fout<<H[1]<<'\n';
}
}
return 0;
}