Pagini recente » Cod sursa (job #1664238) | Cod sursa (job #383509) | Cod sursa (job #397841) | Cod sursa (job #564488) | Cod sursa (job #2722259)
/**
*/
#include <fstream>
#include <algorithm>
#include <cstdio>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int nmax=2e5 +5;
int n,k,t,v[nmax],poz,h[nmax],p[nmax],m;
void swap_2(int b,int c)
{
swap(p[h[b]],p[h[c]]);
swap(h[b],h[c]);
}
void percolate(int x)
{
int f=x>>1;
if(f>0 && v[h[x]]<v[h[f]])
{
swap_2(x,f);
percolate(f);
}
}
void insert_in(int a)
{
h[++m]=a;
p[a]=m;
percolate(m);
}
void sift(int x)
{
int s=x<<1;
if(s+1<=m && h[s+1]<h[s])
s++;
if(s<=m && h[s]<h[x])
{
swap_2(x,s);
sift(s);
}
}
void delete_elem(int x)
{
swap_2(x,m);
p[h[m]]=0;
h[m--]=0;
sift(x);
}
void read()
{
fin>>n;
while(n)
{
fin>>t;
if(t==1)
{
fin>>v[++k];
insert_in(k);
}
if(t==2)
{
fin>>poz;
delete_elem(p[poz]);
}
if(t==3)
{
fout<<v[h[1]];
}
n--;
}
}
int main()
{
read();
return 0;
}