Pagini recente » Cod sursa (job #1775705) | Cod sursa (job #1114919) | Cod sursa (job #791337) | Cod sursa (job #1128370) | Cod sursa (job #1856253)
#include <bits/stdc++.h>
using namespace std;
ifstream inf("heapuri.in");
ofstream outf("heapuri.out");
const int N=200010;
int h[N], x[N], p[N];
void heap_up(int);
void heap_down(int);
void Swap(int, int);
int n, t, m, i, k, y, pos;
int main()
{
inf>>n;
for(i=1; i<=n; i++)
{
inf>>t;
if(t==3)
outf<<x[h[1]]<<'\n';
else
{
inf>>y;
if(t==1)
{
x[++k]=y;
m++;
h[m]=k;
p[k]=m;
heap_up(m);
}
else
{
pos=p[y];
Swap(m, pos);
m--;
heap_up(pos);
heap_down(pos);
}
}
}
return 0;
}
void heap_up(int fiu)
{
int tata=fiu/2;
if(!tata)
return;
if(x[h[fiu]]<x[h[tata]])
{
Swap(tata, fiu);
heap_up(tata);
}
}
void heap_down(int tata)
{
int fiu=2*tata;
if(fiu>m)
return;
if(fiu<m&&x[h[fiu+1]]<x[h[fiu]])
fiu++;
if(x[h[fiu]]<x[h[tata]])
{
Swap(tata, fiu);
heap_down(fiu);
}
}
void Swap(int u, int v)
{
int aux=h[u];
h[u]=h[v];
h[v]=aux;
p[h[u]]=u;
p[h[v]]=v;
}