Pagini recente » Cod sursa (job #2090665) | Cod sursa (job #2475295) | Cod sursa (job #1902931) | Cod sursa (job #2202762) | Cod sursa (job #2049188)
#include <cstdio>
#include <algorithm>
#define MAXN 200001
using namespace std;
int heap[MAXN],v[MAXN],poz[MAXN];
int l,n,k;
inline void up(int p)
{
while(p/2 && v[heap[p/2]]>v[heap[p]])
{
swap(poz[heap[p/2]],poz[heap[p]]);
swap(heap[p/2],heap[p]);
p/=2;
}
}
inline void down(int p)
{
int y;
bool ok=true;
while(ok)
{
ok=false;
if(2*p<=l)
{
ok=true;y=2*p;
if(2*p+1<=l && v[heap[2*p]]>v[heap[2*p+1]])
y++;
}
if(ok && v[heap[y]]<v[heap[p]])
{
swap(poz[heap[p]],poz[heap[y]]);
swap(heap[p],heap[y]);
p=y;
}
}
}
inline void add(int x)
{
v[k]=x;
heap[++l]=k;poz[k]=l;
up(l);
}
inline void pop(int p)
{
poz[heap[l]]=p;
heap[p]=heap[l];
l--;
if(v[heap[p]]<v[heap[p/2]])
up(p);
else
down(p);
}
int main()
{
FILE *fin,*fout;
fin=fopen("heapuri.in","r");
fout=fopen("heapuri.out","w");
int cod,x;
fscanf(fin,"%d",&n);
for(int i=1;i<=n;i++)
{
fscanf(fin,"%d",&cod);
if(cod==3)
fprintf(fout,"%d\n",v[heap[1]]);
else
{
fscanf(fin,"%d",&x);
if(cod==1)
k++,add(x);
else
pop(poz[x]);
}
}
fclose(fin);
fclose(fout);
return 0;
}