Pagini recente » Cod sursa (job #585028) | Cod sursa (job #384851) | Cod sursa (job #487354) | Cod sursa (job #1151720) | Cod sursa (job #2835367)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int a,b,c,h[400005],n,q,pos,f[400005],where[400005],tp;
//h[i] val in heap de pe poz i
//f[i] timpul valorii de pe pozitia i
//where[i] unde gasim valoarea intrata a i - a
void myswap(int i, int j)
{
where[f[i]]=j;
where[f[j]]=i;
int aux=h[i];
h[i]=h[j];
h[j]=aux;
aux=f[i];
f[i]=f[j];
f[j]=aux;
}
void up(int pos)
{
if(pos<=1)return;
if(h[pos]<h[pos/2])
{
myswap(pos,pos/2);
up(pos/2);
}
}
void down(int pos)
{
if(pos*2>n)return;
if(pos*2+1<=n)
{
if(min(h[pos*2],h[pos*2+1])>=h[pos])return;
int p=pos*2;
if(h[p+1]<h[p])p++;
myswap(pos,p);
down(p);
return;
}
if(h[pos*2]<h[pos])myswap(pos,pos*2);
}
void pr(){fout<<h[1]<<'\n';}
void ins(int x)
{
h[++n]=x;
f[n]=++tp;
where[tp]=n;
up(n);
}
void rmv(int x)
{
int pos=where[x];
myswap(pos,n);
n--;
if(pos>1&&h[pos]<h[pos/2])up(pos);
else down(pos);
}
signed main()
{
fin>>q;
while(q--)
{
fin>>a;
if(a==3){pr();continue;}
fin>>b;
if(a==1)ins(b);
else rmv(b);
}
return 0;
}